关键词搜索

源码搜索 ×
×

C# SortedDictionary排序结构测试

发布2018-01-19浏览1942次

详情内容

C# SortedDictionary-Dictionary,SortedList-List 这是两种不一样的数据结构,但是他们比较相似。Sorted*意为排序的,这里以SortedDictionary为例进行测试。

SortedDictionary

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace AVParser.Data
  7. {
  8. /// <summary>
  9. /// GB1078音视频帮助类
  10. /// </summary>
  11. public class GB1078AVHelper
  12. {
  13. /// <summary>
  14. /// 用于存储解析后的AV数据
  15. /// </summary>
  16. SortedDictionary<ulong, AV> sortedDictionary = new SortedDictionary<ulong, AV>();
  17. /// <summary>
  18. /// 设置AV数据
  19. /// </summary>
  20. /// <param name="av"></param>
  21. public void SetData(AV av)
  22. {
  23. sortedDictionary[av.Head.RTTimeSpan] = av;
  24. }
  25. /// <summary>
  26. /// 获取最大时间戳的AV
  27. /// </summary>
  28. /// <returns></returns>
  29. public AV GetMaxAV()
  30. {
  31. ulong maxKey = sortedDictionary.Keys.Max();
  32. AV av = sortedDictionary[maxKey];
  33. // 取走即清除
  34. sortedDictionary.Remove(maxKey);
  35. return av;
  36. }
  37. /// <summary>
  38. /// 获取最小时间戳的AV
  39. /// </summary>
  40. /// <returns></returns>
  41. public AV GetMinAV()
  42. {
  43. ulong minKey = sortedDictionary.Keys.Min();
  44. AV av = sortedDictionary[minKey];
  45. // 取走即清除
  46. sortedDictionary.Remove(minKey);
  47. return av;
  48. }
  49. }
  50. }

Program

  1. using AVParser.Parser;
  2. using FFmpeg.AutoGen;
  3. using JX;
  4. using RTForwardServer;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Media;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace AVParser
  14. {
  15. static class Program
  16. {
  17. /// <summary>
  18. /// 应用程序的主入口点
  19. /// </summary>
  20. [STAThread]
  21. static void Main()
  22. {
  23. Application.EnableVisualStyles();
  24. Application.SetCompatibleTextRenderingDefault(false);
  25. Data.GB1078AVHelper helper = new Data.GB1078AVHelper();
  26. for(var i=10; i >= 1; i--)
  27. {
  28. JTRTHead head = new JTRTHead();
  29. head.RTTimeSpan =(ulong) i;
  30. AV av = new AV(AV.AVTYPE.MEDIA_AUDIO, head, new byte[0]);
  31. helper.SetData(av);
  32. }
  33. for (var i = 10; i >= 1; i--)
  34. {
  35. Console.WriteLine(helper.GetMinAV().Head.RTTimeSpan);
  36. }
  37. Console.ReadLine();
  38. }
  39. }
  40. }

OutputInfo

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
注意事项:C#里面Dictionary的Add键不允许重复,会产生异常。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载