Add List.slice method, patch by Zeeshan Ali Khattak, fixes bug 569188
authorJürg Billeter <j@bitron.ch>
Tue, 27 Jan 2009 19:22:12 +0000 (19:22 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Tue, 27 Jan 2009 19:22:12 +0000 (19:22 +0000)
2009-01-27  Jürg Billeter  <j@bitron.ch>

* gee/arraylist.vala:
* gee/list.vala:
* gee/readonlylist.vala:

Add List.slice method, patch by Zeeshan Ali Khattak,
fixes bug 569188

svn path=/trunk/; revision=40

ChangeLog
gee/arraylist.vala
gee/list.vala
gee/readonlylist.vala

index 26b2391..d3683a4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2009-01-27  Jürg Billeter  <j@bitron.ch>
+
+       * gee/arraylist.vala:
+       * gee/list.vala:
+       * gee/readonlylist.vala:
+
+       Add List.slice method, patch by Zeeshan Ali Khattak,
+       fixes bug 569188
+
 2008-11-01  Jürg Billeter  <j@bitron.ch>
 
        * configure.ac: Post-release version bump
index 97ce929..0135b84 100644 (file)
@@ -128,6 +128,19 @@ public class Gee.ArrayList<G> : Object, Iterable<G>, Collection<G>, List<G> {
                _stamp++;
        }
 
+       public List<G>? slice (int start, int stop) {
+               return_val_if_fail (start <= stop, null);
+               return_val_if_fail (start >= 0, null);
+               return_val_if_fail (stop <= this.size, null);
+
+               var slice = new ArrayList<G> (this._equal_func);
+               for (int i = start; i < stop; i++) {
+                       slice.add (this[i]);
+               }
+
+               return slice;
+       }
+
        private void shift (int start, int delta) {
                assert (start >= 0 && start <= _size && start >= -delta);
 
index d034a91..451e149 100644 (file)
@@ -63,5 +63,15 @@ public interface Gee.List<G> : Collection<G> {
         * @param index zero-based index of the item to be removed
         */
        public abstract void remove_at (int index);
+
+       /**
+        * Returns a slice of this list.
+        *
+        * @param start zero-based index of the begin of the slice
+        * @param stop  zero-based index after the end of the slice
+        *
+        * @return A list containing a slice of this list
+        */
+       public abstract List<G>? slice (int start, int stop);
 }
 
index 1ea988d..55465f0 100644 (file)
@@ -100,6 +100,10 @@ public class Gee.ReadOnlyList<G> : Object, Iterable<G>, Collection<G>, List<G> {
                assert_not_reached ();
        }
 
+       public List<G>? slice (int start, int stop) {
+               assert_not_reached ();
+       }
+
        class Iterator<G> : Object, Gee.Iterator<G> {
                public bool next () {
                        return false;