SortedSeries.vala 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. namespace Invercargill.DataStructures {
  2. public class SortedSeries<T> : Enumerable<T>, Lot<T>, ReadOnlyCollection<T>, Collection<T> {
  3. private class SeriesNode<T> {
  4. public bool is_red;
  5. public Series<T> values { get; private set; }
  6. public SeriesNode<T> left_child { get; set; }
  7. public SeriesNode<T> right_child { get; set; }
  8. public SeriesNode<T> parent { get; set; }
  9. public SeriesNode(T item, SeriesNode<T> nil) {
  10. is_red = true;
  11. left_child = nil;
  12. right_child = nil;
  13. values = new Series<T>();
  14. values.add(item);
  15. }
  16. public SeriesNode.nil() {
  17. is_red = false;
  18. left_child = this;
  19. right_child = this;
  20. values = new Series<T>();
  21. }
  22. public T sample() {
  23. return values.first_or_default();
  24. }
  25. public void add_value(T value) {
  26. values.add(value);
  27. }
  28. // public void remove_all_values_where (PredicateDelegate<T> predicate) {
  29. // values = values.where(v => !predicate(v)).to_buffer();
  30. // }
  31. }
  32. private SeriesNode<T> root;
  33. private SeriesNode<T> nil;
  34. private RWLock rw_lock;
  35. private Series<SeriesNode<T>> node_refs;
  36. private CompareDelegate<T> comparitor;
  37. private int n_items;
  38. public SortedSeries(owned CompareDelegate<T>? comparitor = null) {
  39. nil = new SeriesNode<T>.nil();
  40. node_refs = new Series<SeriesNode<T>>();
  41. node_refs.add(nil);
  42. root = nil;
  43. rw_lock = RWLock();
  44. n_items = 0;
  45. this.comparitor = (owned)comparitor ?? Operators.comparison<T>();
  46. }
  47. public override Tracker<T> get_tracker () {
  48. if(root == nil) {
  49. return empty<T>().get_tracker();
  50. }
  51. return new TreeTracker<T>(this);
  52. }
  53. public override int? peek_count () {
  54. return n_items;
  55. }
  56. public override EnumerableInfo get_info () {
  57. return new EnumerableInfo.infer_ultimate (this, EnumerableCategory.IN_MEMORY);
  58. }
  59. public void add (T item) {
  60. // Get lock and update count of items
  61. rw_lock.writer_lock();
  62. n_items++;
  63. SeriesNode<T>? parent = null;
  64. SeriesNode<T> current = root;
  65. int cmp = 0;
  66. while(current != nil) {
  67. parent = current;
  68. cmp = comparitor(item, current.sample());
  69. if(cmp == 0) {
  70. // Add to node, has no effect on tree
  71. current.add_value(item);
  72. rw_lock.writer_unlock();
  73. return;
  74. }
  75. if(cmp < 0) {
  76. current = current.left_child;
  77. }
  78. else {
  79. current = current.right_child;
  80. }
  81. }
  82. var new_node = new SeriesNode<T> (item, nil);
  83. node_refs.add(new_node);
  84. new_node.parent = parent;
  85. if(parent == null) {
  86. root = new_node;
  87. }
  88. else {
  89. if(cmp == 0) {
  90. print("Does this actually happen?\n");
  91. assert_not_reached ();
  92. }
  93. else if(cmp < 0) {
  94. parent.left_child = new_node;
  95. }
  96. else {
  97. parent.right_child = new_node;
  98. }
  99. }
  100. if(new_node.parent == null) {
  101. new_node.is_red = false;
  102. }
  103. else if(new_node.parent.parent != null) {
  104. fix_insert(new_node);
  105. }
  106. rw_lock.writer_unlock();
  107. }
  108. private void fix_insert(SeriesNode<T> new_node) {
  109. var node = new_node;
  110. while(node != root && node.parent.is_red) {
  111. if(node.parent == node.parent.parent.left_child) {
  112. var uncle = node.parent.parent.right_child;
  113. if(uncle.is_red) {
  114. node.parent.is_red = false;
  115. uncle.is_red = false;
  116. node.parent.parent.is_red = true;
  117. node = node.parent.parent;
  118. continue;
  119. }
  120. if(node == node.parent.right_child) {
  121. node = node.parent;
  122. rotate_left(node);
  123. }
  124. node.parent.is_red = false;
  125. node.parent.parent.is_red = true;
  126. rotate_right (node.parent.parent);
  127. }
  128. else {
  129. var uncle = node.parent.parent.left_child;
  130. if(uncle.is_red) {
  131. node.parent.is_red = false;
  132. uncle.is_red = false;
  133. node.parent.parent.is_red = true;
  134. node = node.parent.parent;
  135. continue;
  136. }
  137. if(node == node.parent.left_child) {
  138. node = node.parent;
  139. rotate_right(node);
  140. }
  141. node.parent.is_red = false;
  142. node.parent.parent.is_red = true;
  143. rotate_left(node.parent.parent);
  144. }
  145. }
  146. root.is_red = false;
  147. }
  148. public void remove_first_where (PredicateDelegate<T> predicate) {
  149. assert_not_reached ();
  150. }
  151. public void remove_all_where (PredicateDelegate<T> predicate) {
  152. assert_not_reached ();
  153. }
  154. public void clear () {
  155. assert_not_reached ();
  156. }
  157. private void rotate_left(SeriesNode<T> x) {
  158. var y = x.right_child;
  159. x.right_child = y.left_child;
  160. if(y.left_child != nil)
  161. y.left_child.parent = x;
  162. y.parent = x.parent;
  163. if(x.parent == null)
  164. root = y;
  165. else if(x.parent.left_child == x)
  166. x.parent.left_child = y;
  167. else
  168. x.parent.right_child = y;
  169. y.left_child = x;
  170. x.parent = y;
  171. }
  172. private void rotate_right(SeriesNode<T> x) {
  173. var y = x.left_child;
  174. x.left_child = y.right_child;
  175. if(y.right_child != nil)
  176. y.right_child.parent = x;
  177. y.parent = x.parent;
  178. if(x.parent == null)
  179. root = y;
  180. else if(x.parent.right_child == x)
  181. x.parent.right_child = y;
  182. else
  183. x.parent.left_child = y;
  184. y.right_child = x;
  185. x.parent = y;
  186. }
  187. private class TreeTracker<T> : Tracker<T> {
  188. SeriesNode<T> node;
  189. SeriesNode<T>? next_node;
  190. SortedSeries<T> series;
  191. Tracker<T> values;
  192. public TreeTracker(SortedSeries<T> series) {
  193. print("New tracker!\n");
  194. this.series = series;
  195. node = series.root;
  196. while(node.left_child != series.nil) {
  197. node = node.left_child;
  198. }
  199. values = node.values.get_tracker();
  200. find_next_node();
  201. }
  202. public override bool has_next () {
  203. return values.has_next() || next_node != null;
  204. }
  205. public override T get_next () {
  206. if(values.has_next()) {
  207. return values.get_next();
  208. }
  209. node = next_node;
  210. values = node.values.get_tracker();
  211. var result = values.get_next();
  212. find_next_node();
  213. return result;
  214. }
  215. private void find_next_node() {
  216. // next_node = null;
  217. // if(node.right_child != series.nil) {
  218. // next_node = node.right_child;
  219. // while(next_node.left_child != series.nil) {
  220. // next_node = next_node.left_child;
  221. // }
  222. // }
  223. // else if(node.parent == null) {
  224. // next_node = null;
  225. // }
  226. // else if(node.parent.left_child == node) {
  227. // next_node = node.parent;
  228. // }
  229. // else if(node.parent.parent.right_child == node.parent) {
  230. // next_node = null;
  231. // }
  232. // else {
  233. // next_node = node.parent.parent;
  234. // }
  235. next_node = null;
  236. if (node == null) return;
  237. // standard inorder successor:
  238. if (node.right_child != series.nil) {
  239. var n = node.right_child;
  240. while (n.left_child != series.nil)
  241. n = n.left_child;
  242. next_node = n;
  243. return;
  244. }
  245. var n = node;
  246. var p = n.parent;
  247. while (p != null && n == p.right_child) {
  248. n = p;
  249. p = p.parent;
  250. }
  251. next_node = p; // could be null (end)
  252. }
  253. }
  254. }
  255. }