Explorar el Código

Make simple classes compact

Billy Barrow hace 2 semanas
padre
commit
e4dbc35f7e

+ 2 - 8
src/lib/Enumerable.vala

@@ -156,10 +156,7 @@ namespace Invercargill {
         }
 
         public virtual Attempts<SelectionContext<T, TOut>> attempt_contextualised_select<TOut>(owned AttemptTransformDelegate<T, TOut> transform) {
-            return attempt_select<SelectionContext<T, TOut>>((i) => new SelectionContext<T, TOut>() {
-                origin = i,
-                result = transform(i)
-            });
+            return attempt_select<SelectionContext<T, TOut>>((i) => new SelectionContext<T, TOut>(i, transform(i)));
         }
 
         public virtual Attempts<TOut> attempt_select_nested<TOut>(owned AttemptTransformDelegate<T, Enumerable<Attempt<TOut>>> transform) {
@@ -229,10 +226,7 @@ namespace Invercargill {
         }
 
         public virtual Enumerable<SelectionContext<T, TOut>> contextualised_select<TOut>(owned TransformDelegate<T, TOut> transform) {
-            return select<SelectionContext<T, TOut>>((i) => new SelectionContext<T, TOut>() {
-                origin = i,
-                result = transform(i)
-            });
+            return select<SelectionContext<T, TOut>>((i) => new SelectionContext<T, TOut>(i, transform(i)));
         }
 
         public virtual TOut aggregate<TOut>(TOut initial, AggregateDelegate<TOut, T> aggregate_func) {

+ 9 - 2
src/lib/Grouping.vala

@@ -1,15 +1,22 @@
 
 namespace Invercargill {
 
+    [Compact]
+    [Immutable]
+    [CCode (copy_function = "invercargill_grouping_copy")]
     public class Grouping<TKey, TItems> {
 
-        public TKey key {get; set;}
-        public Enumerable<TItems> items {get; set;}
+        public TKey key;
+        public Enumerable<TItems> items;
 
         public Grouping(TKey key, Enumerable<TItems> items) {
             this.key = key;
             this.items = items;
         }
+
+        public Grouping<TKey, TItems> copy() {
+            return new Grouping<TKey, TItems>(key, items);
+        }
     }
 
 }

+ 11 - 4
src/lib/Pair.vala

@@ -1,13 +1,16 @@
 
 namespace Invercargill {
 
+    [Compact]
+    [Immutable]
+    [CCode (copy_function = "invercargill_pair_copy")]
     public class Pair<TFirst, TSecond> {
 
-        public TFirst value1 {get; set;}
-        public TSecond value2 {get; set;}
+        public TFirst value1;
+        public TSecond value2;
 
-        public bool value1_is_set {get; set;}
-        public bool value2_is_set {get; set;}
+        public bool value1_is_set;
+        public bool value2_is_set;
 
         public Pair(TFirst v1, bool v1set, TSecond v2, bool v2set) {
             value1 = v1;
@@ -15,6 +18,10 @@ namespace Invercargill {
             value2 = v2;
             value2_is_set = v2set;
         }
+
+        public Pair<TFirst, TSecond> copy() {
+            return new Pair<TFirst, TSecond>(value1, value1_is_set, value2, value2_is_set);
+        }
     }
 
 }

+ 9 - 2
src/lib/PositionItemPair.vala

@@ -1,14 +1,21 @@
 
 namespace Invercargill {
 
+    [Compact]
+    [Immutable]
+    [CCode (copy_function = "invercargill_position_item_pair_copy")]
     public class PositionItemPair<T> {
-        public int position { get; private set; }
-        public T item { get; private set; }
+        public int position;
+        public T item;
 
         public PositionItemPair(int position, T item) {
             this.position = position;
             this.item = item;
         }
+
+        public PositionItemPair<T> copy() {
+            return new PositionItemPair<T>(position, item);
+        }
     }
 
 }

+ 1 - 4
src/lib/Promotions/Attempts.vala

@@ -159,10 +159,7 @@ namespace Invercargill {
         }
     
         public new Attempts<SelectionContext<T, TOut>> contextualised_select<TOut>(owned TransformDelegate<T, TOut> transform) {
-            return select<SelectionContext<T, TOut>>((i) => new SelectionContext<T, TOut>() {
-                origin = i,
-                result = transform(i)
-            });
+            return select<SelectionContext<T, TOut>>((i) => new SelectionContext<T, TOut>(i, transform(i)));
         }
     
         public new TOut aggregate<TOut>(TOut initial, AggregateDelegate<TOut, T> aggregate_func) throws Error {

+ 14 - 2
src/lib/SelectionContext.vala

@@ -1,9 +1,21 @@
 
 namespace Invercargill {
 
+    [Compact]
+    [Immutable]
+    [CCode (copy_function = "invercargill_selection_context_copy")]
     public class SelectionContext<TOrigin, TResult> {
-        public TOrigin origin {get; set;}
-        public TResult result {get; set;}
+        public TOrigin origin;
+        public TResult result;
+
+        public SelectionContext(TOrigin origin, TResult result) {
+            this.origin = origin;
+            this.result = result;
+        }
+
+        public SelectionContext<TOrigin, TResult> copy() {
+            return new SelectionContext<TOrigin, TResult>(origin, result);
+        }
     }
 
 }

+ 3 - 2
src/tests/Integration/Firsts.vala

@@ -6,8 +6,9 @@ void first_tests() {
         var items = Wrap.array(new int[] { 1, 2, 3, 4, 5, 6});
 
         try{
-            var first = items.first();
-            assert_true(first == 1);
+            var first = items.with_positions().first();
+            assert_true(first.item == 1);
+            assert_true(first.position == 0);
         }
         catch (Error e) {
             assert_no_error(e);