SortedSeries.vala 9.1 KB

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