Selaa lähdekoodia

Add bespoke functionality to Series

Billy Barrow 2 viikkoa sitten
vanhempi
sitoutus
4569bb12b9
1 muutettua tiedostoa jossa 82 lisäystä ja 1 poistoa
  1. 82 1
      src/lib/DataStructures/Series.vala

+ 82 - 1
src/lib/DataStructures/Series.vala

@@ -57,7 +57,88 @@ namespace Invercargill.DataStructures {
         }
 
         public void add_all(Enumerable<T> items) {
-            items.iterate(i => add(i));
+            lock(root) {
+                SeriesItem<T>* chain_start = null;
+                SeriesItem<T>* chain_end = null;
+                foreach (var item in items) {
+                    n_items++;
+                    SeriesItem<T>* si = new SeriesItem<T>(item);
+                    if(chain_start == null) {
+                        chain_start = si;
+                        chain_end = si;
+                        continue;
+                    }
+                    chain_end->next = si;
+                    chain_end = si;
+                }
+
+                if(root == null) {
+                    root = chain_start;
+                    end = chain_end;
+                    return;
+                }
+                end->next = chain_start;
+                end = chain_end;
+            }
+        }
+
+        public void add_start(T item) {
+            lock(root) {
+                n_items++;
+                SeriesItem<T>* si = new SeriesItem<T>(item);
+                if(root == null) {
+                    end = si;
+                }
+                si->next = root;
+                root = si;
+            }
+        }
+
+        public void add_all_start(Enumerable<T> items) {
+            lock(root) {
+                SeriesItem<T>* chain_start = null;
+                SeriesItem<T>* chain_end = null;
+                foreach (var item in items) {
+                    n_items++;
+                    SeriesItem<T>* si = new SeriesItem<T>(item);
+                    if(chain_start == null) {
+                        chain_start = si;
+                        chain_end = si;
+                        continue;
+                    }
+                    chain_end->next = si;
+                    chain_end = si;
+                }
+
+                if(root == null) {
+                    end = chain_end;
+                }
+                chain_end->next = root;
+                root = chain_start;
+            }
+        }
+
+        public T pop_start() throws SequenceError {
+            SequenceError? e = null;
+            T? item = null;
+            lock(root) {
+                if(root == null) {
+                    e = new SequenceError.NO_ELEMENTS("Series contains no elements");
+                }
+                else {
+                    var old_root = root;
+                    root = root->next;
+                    item = old_root->value;
+                    delete old_root;
+                    if(root == null) {
+                        end = null;
+                    }
+                }
+            }
+            if(e != null) {
+                throw e;
+            }
+            return item;
         }
 
         public void remove_first_where(Invercargill.PredicateDelegate<T> predicate) {