SortedIntegerList.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ARMeilleure.Common
  4. {
  5. public class SortedIntegerList
  6. {
  7. private List<int> _items;
  8. public int Count => _items.Count;
  9. public int this[int index]
  10. {
  11. get
  12. {
  13. return _items[index];
  14. }
  15. set
  16. {
  17. _items[index] = value;
  18. }
  19. }
  20. public SortedIntegerList()
  21. {
  22. _items = new List<int>();
  23. }
  24. public bool Add(int value)
  25. {
  26. if (_items.Count == 0 || value > Last())
  27. {
  28. _items.Add(value);
  29. return true;
  30. }
  31. else
  32. {
  33. int index = _items.BinarySearch(value);
  34. if (index >= 0)
  35. {
  36. return false;
  37. }
  38. _items.Insert(-1 - index, value);
  39. return true;
  40. }
  41. }
  42. public int FindLessEqualIndex(int value)
  43. {
  44. int index = _items.BinarySearch(value);
  45. return (index < 0) ? (-2 - index) : index;
  46. }
  47. public void RemoveRange(int index, int count)
  48. {
  49. if (count > 0)
  50. {
  51. _items.RemoveRange(index, count);
  52. }
  53. }
  54. public int Last()
  55. {
  56. return _items[Count - 1];
  57. }
  58. public List<int> GetList()
  59. {
  60. return _items;
  61. }
  62. }
  63. }