[libgee/reversed] Initial reversed



commit 60828166f4697e5ee05082b84439e076cae37135
Author: Maciej Piechotka <uzytkownik2 gmail com>
Date:   Tue Oct 2 09:35:31 2012 +0100

    Initial reversed

 gee/Makefile.am       |    1 +
 gee/reversedlist.vala |  175 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 176 insertions(+), 0 deletions(-)
---
diff --git a/gee/Makefile.am b/gee/Makefile.am
index be270bf..b4ec9f1 100644
--- a/gee/Makefile.am
+++ b/gee/Makefile.am
@@ -58,6 +58,7 @@ libgee_0_8_la_SOURCES = \
        readonlyset.vala \
        readonlysortedmap.vala \
        readonlysortedset.vala \
+       reversedlist.vala \
        set.vala \
        sortedmap.vala \
        sortedset.vala \
diff --git a/gee/reversedlist.vala b/gee/reversedlist.vala
new file mode 100644
index 0000000..d26b1d8
--- /dev/null
+++ b/gee/reversedlist.vala
@@ -0,0 +1,175 @@
+/* reversedlist.vala
+ *
+ * Copyright (C) 2012  Maciej Piechotka
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Maciej Piechotka <uzytkownik2 gmail com>
+ */
+
+private class Gee.ReversedList<G> : Object, AbstractCollection<G> {
+
+       public override bool foreach (ForallFunc<G> f) {
+               BidirIterator<G> iter = list.bidir_list_iterator();
+               if (!iter.last ()) {
+                       return true;
+               }
+               while (iter.previous ()) {
+                       if (!f (iter.get ())) {
+                               return false;
+                       }
+               }
+               return true;
+       }
+
+       public override Gee.Iterator<G> iterator () {
+               BidirListIterator<G> iter = list.bidir_list_iterator();
+               return new Iterator<G> (list, iter, !iter.last ());
+       }
+
+       public override int size { get { return list.size; } }
+
+       public override bool is_empty { get { return list.is_empty; } }
+
+       public override bool contains (G item) {
+               return list.contains (item);
+       }
+
+       public override bool add (G item) {
+               return list.insert (0, item);
+       }
+
+       public override bool remove (G item) {
+               // TODO: Remove from end
+               list.remove (item);
+       }
+
+       public override void clear () {
+               list.clear ();
+       }
+
+       public override ListIterator<G> list_iterator () {
+               BidirListIterator<G> iter = list.bidir_list_iterator();
+               return new Iterator<G> (list, iter, !iter.last ());
+       }
+
+       public override G get (int index) {
+               return list.get (list.size - index - 1);
+       }
+
+       public override void set (int index, G item) {
+               return list.set (list.size - index - 1, item);
+       }
+
+       private BidirList<G> list;
+
+       private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G>, BidirIterator<G>, 
ListIterator<G>, BidirListIterator<G> {
+               public Iterator (BidirList<G> list, BidirListIterator<G> iter, bool started) {
+                       this.list = list;
+                       this.iter = iter;
+                       this.started = started;
+               }
+
+               public bool foreach (ForallFunc<G> f) {
+                       if (iter.valid) {
+                               if (!f (iter.get ())) {
+                                       return false;
+                               }
+                       }
+                       while (iter.previous ()) {
+                               if (!f (iter.get ())) {
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+
+               public bool next () {
+                       if (!started) {
+                               started = true;
+                               return started;
+                       } else {
+                               return iter.previous ();
+                       }
+               }
+
+               public bool has_next () {
+                       return !started || iter.has_previous ();
+               }
+
+               public void remove () {
+                       assert (started);
+                       iter.remove ();
+               }
+
+               public bool valid { get { return started && iter.valid; } }
+
+               public bool read_only { get { return iter.read_only; } }
+
+               public bool previous () {
+                       if (!started) {
+                               return false;
+                       } else {
+                               return iter.next ();
+                       }
+               }
+
+               public bool has_previous () {
+                       if (!started) {
+                               return false;
+                       } else {
+                               return iter.has_next ();
+                       }
+               }
+
+               public bool first () {
+                       if (iter == null) {
+                               return false;
+                       } else {
+                               return iter.last ();
+                       }
+               }
+
+               public bool last () {
+                       if (iter == null) {
+                               return false;
+                       } else {
+                               return iter.first ();
+                       }
+               }
+
+               public void set (G item) {
+                       ((ListIterator<G>)iter).set (item);
+               }
+
+               public void add (G item) {
+                       insert (item);
+               }
+
+               public int index () {
+                       return list.size - iter.index () - 1;
+               }
+
+               public void insert (G item) {
+                       add (item);
+               }
+
+               private BidirList<G> list;
+               private BidirListIterator<G> iter;
+               private bool started;
+       }
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]