Browse Source

Series compaction

Billy Barrow 3 tuần trước cách đây
mục cha
commit
19be39902a
1 tập tin đã thay đổi với 28 bổ sung25 xóa
  1. 28 25
      src/lib/DataStructures/Series.vala

+ 28 - 25
src/lib/DataStructures/Series.vala

@@ -2,28 +2,31 @@ namespace Invercargill.DataStructures {
 
     public class Series<T> : Enumerable<T>, Lot<T>, ReadOnlyCollection<T>, Collection<T> {
      
+        //  [CCode (ref_function = "invercargill_data_structures_series_series_item_up", unref_function = "invercargill_data_structures_series_series_item_down")]
+        [Compact]
         internal class SeriesItem<T> {
-            public SeriesItem next = null;
+            public SeriesItem<T>* next;
             public T value;
 
-            public SeriesItem(T value) {
-                this.value = value;
+            public SeriesItem(owned T value) {
+                this.value = (owned)value;
             }
 
+
             // Carefully unlink series items when destructed.
             // Very large series sometimes cause a segfault without this
-            ~SeriesItem() {
-                var n = next;
-                while(n != null) {
-                    var c = n;
-                    n = n.next;
-                    c.next = null;
-                }
-            }
+            //  ~SeriesItem() {
+            //      unowned SeriesItem<T> n = next;
+            //      while(n != null) {
+            //          unowned SeriesItem<T> c = n;
+            //          n = n.next;
+            //          c.next = null;
+            //      }
+            //  }
         }
 
-        internal SeriesItem<T> root;
-        internal SeriesItem<T> end;
+        internal SeriesItem<T>* root;
+        internal SeriesItem<T>* end;
         private int n_items = 0;
 
         public Series() {}
@@ -53,13 +56,13 @@ namespace Invercargill.DataStructures {
         public void add(T item) {
             lock(root) {
                 n_items++;
-                var si = new SeriesItem<T>(item);
+                SeriesItem<T>* si = new SeriesItem<T>(item);
                 if(root == null) {
                     root = si;
                     end = si;
                     return;
                 }
-                end.next = si;
+                end->next = si;
                 end = si;
             }
         }
@@ -77,22 +80,22 @@ namespace Invercargill.DataStructures {
 
         private void remove_items(Invercargill.PredicateDelegate<T> predicate, bool first_only) {
             lock(root) {
-                SeriesItem<T> previous = null;
-                var node = root;
+                SeriesItem<T>* previous = null;
+                SeriesItem<T>* node = root;
                 while(node != null) {
-                    if(predicate(node.value)) {
+                    if(predicate(node->value)) {
                         if(previous == null) {
-                            root = node.next;
+                            root = node->next;
                         }
                         else {
-                            previous.next = node.next;
+                            previous->next = node->next;
                         }
                         if(first_only) {
                             break;
                         }
                     }
                     previous = node;
-                    node = node.next;
+                    node = node->next;
                 }
             }
         }
@@ -107,9 +110,9 @@ namespace Invercargill.DataStructures {
 
         private class SeriesTracker<T> : Tracker<T> {
 
-            private SeriesItem<T>? current;
+            private weak SeriesItem<T>* current;
 
-            public SeriesTracker(SeriesItem<T>? root) {
+            public SeriesTracker(SeriesItem<T>* root) {
                 current = root;
             }
 
@@ -117,8 +120,8 @@ namespace Invercargill.DataStructures {
                 return current != null;
             }
             public override T get_next() {
-                var value = current.value;
-                current = current.next;
+                var value = current->value;
+                current = current->next;
                 return value;
             }