PrioritizedList.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Implements a list sorted by a numeric priority
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. /*****************************************************************/
  23. /**
  24. * The PrioritizedListItem<DataClass> interface
  25. *
  26. * @template DataClass The class of data stored in the item
  27. */
  28. export interface PrioritizedListItem<DataClass> {
  29. /**
  30. * The priority of this item
  31. */
  32. priority: number;
  33. /**
  34. * The data for the list item
  35. */
  36. item: DataClass;
  37. }
  38. /*****************************************************************/
  39. /**
  40. * Implements the PrioritizedList<DataClass> class
  41. *
  42. * @template DataClass The class of data stored in the list
  43. */
  44. export class PrioritizedList<DataClass> {
  45. /**
  46. * The default priority for items added to the list
  47. */
  48. public static DEFAULTPRIORITY: number = 5;
  49. /**
  50. * The list of items, sorted by priority (smallest number first)
  51. */
  52. protected items: PrioritizedListItem<DataClass>[] = [];
  53. /**
  54. * @constructor
  55. */
  56. constructor() {
  57. this.items = [];
  58. }
  59. /**
  60. * Make the list iterable, and return the data for the items in the list
  61. *
  62. * @return {{next: Function}} The object containing the iterator's next() function
  63. */
  64. public [Symbol.iterator](): Iterator<PrioritizedListItem<DataClass>> {
  65. let i = 0;
  66. let items = this.items;
  67. return {
  68. /* tslint:disable-next-line:jsdoc-require */
  69. next(): IteratorResult<PrioritizedListItem<DataClass>> {
  70. return {value: items[i++], done: (i > items.length)};
  71. }
  72. };
  73. }
  74. /**
  75. * Add an item to the list
  76. *
  77. * @param {DataClass} item The data for the item to be added
  78. * @param {number} priority The priority for the item
  79. * @return {DataClass} The data itself
  80. */
  81. public add(item: DataClass, priority: number = PrioritizedList.DEFAULTPRIORITY): DataClass {
  82. let i = this.items.length;
  83. do {
  84. i--;
  85. } while (i >= 0 && priority < this.items[i].priority);
  86. this.items.splice(i + 1, 0, {item: item, priority: priority});
  87. return item;
  88. }
  89. /**
  90. * Remove an item from the list
  91. *
  92. * @param {DataClass} item The data for the item to be removed
  93. */
  94. public remove(item: DataClass) {
  95. let i = this.items.length;
  96. do {
  97. i--;
  98. } while (i >= 0 && this.items[i].item !== item);
  99. if (i >= 0) {
  100. this.items.splice(i, 1);
  101. }
  102. }
  103. }