$(NULL)
libgee_0_8_la_SOURCES = \
+ abstractbidirlist.vala \
abstractcollection.vala \
abstractlist.vala \
abstractmap.vala \
abstractsortedset.vala \
arraylist.vala \
bidiriterator.vala \
+ bidirlist.vala \
+ bidirlistiterator.vala \
bidirmapiterator.vala \
collection.vala \
comparable.vala \
multiset.vala \
priorityqueue.vala \
queue.vala \
+ readonlybidirlist.vala \
readonlycollection.vala \
readonlylist.vala \
readonlymap.vala \
--- /dev/null
+/* bidirlistiterator.vala
+ *
+ * Copyright (C) 2011 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>
+ */
+
+public abstract class Gee.AbstractBidirList<G> : AbstractList<G>, BidirList<G> {
+
+ /**
+ * {@inheritDoc}
+ */
+ public abstract BidirListIterator<G> bidir_list_iterator ();
+
+ private weak BidirList<G> _read_only_view;
+
+ /**
+ * {@inheritDoc}
+ */
+ public virtual new BidirList<G> read_only_view {
+ owned get {
+ BidirList<G> instance = _read_only_view;
+ if (_read_only_view == null) {
+ instance = new ReadOnlyBidirList<G> (this);
+ _read_only_view = instance;
+ instance.add_weak_pointer ((void**) (&_read_only_view));
+ }
+ return instance;
+ }
+ }
+}
+
*
* @see LinkedList
*/
-public class Gee.ArrayList<G> : AbstractList<G> {
+public class Gee.ArrayList<G> : AbstractBidirList<G> {
/**
* {@inheritDoc}
*/
/**
* {@inheritDoc}
*/
+ public override BidirListIterator<G> bidir_list_iterator () {
+ return new Iterator<G> (this);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public override bool contains (G item) {
return (index_of (item) != -1);
}
_items.resize (value);
}
- private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G>, BidirIterator<G>, ListIterator<G> {
+ private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G>, BidirIterator<G>, ListIterator<G>, BidirListIterator<G> {
private ArrayList<G> _list;
private int _index = -1;
private bool _removed = false;
--- /dev/null
+/* bidirlist.vala
+ *
+ * Copyright (C) 2011 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>
+ */
+
+public interface Gee.BidirList<G> : Gee.List<G> {
+ /**
+ * Returns a BidirListIterator that can be used for iteration over this list.
+ *
+ * @return a BidirListIterator that can be used for iteration over this list
+ */
+ public abstract new BidirListIterator<G> bidir_list_iterator ();
+
+ /**
+ * The read-only view of this list.
+ */
+ public abstract new BidirList<G> read_only_view { owned get; }
+}
--- /dev/null
+/* bidirlistiterator.vala
+ *
+ * Copyright (C) 2011 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>
+ */
+public interface Gee.BidirListIterator<G> : Gee.BidirIterator<G>, Gee.ListIterator<G> {
+ /**
+ * Inserts the specified item before the current item in the iteration. The
+ * cursor is let to point to the current item.
+ */
+ public abstract void insert (G item);
+}
+
*
* @see ArrayList
*/
-public class Gee.LinkedList<G> : AbstractList<G>, Queue<G>, Deque<G> {
+public class Gee.LinkedList<G> : AbstractBidirList<G>, Queue<G>, Deque<G> {
private int _size = 0;
private int _stamp = 0;
private Node<G>? _head = null;
/**
* {@inheritDoc}
*/
+ public override BidirListIterator<G> bidir_list_iterator () {
+ return new Iterator<G> (this);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
public override int size {
get { return this._size; }
}
}
}
- private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G>, BidirIterator<G>, ListIterator<G> {
+ private class Iterator<G> : Object, Traversable<G>, Gee.Iterator<G>, BidirIterator<G>, ListIterator<G>, BidirListIterator<G> {
private bool started = false;
private bool removed = false;
private unowned Node<G>? position;
/**
* A list iterator. This supports bi-directionnal and index-based iteration.
*/
-public interface Gee.ListIterator<G> : Gee.BidirIterator<G> {
+public interface Gee.ListIterator<G> : Gee.Iterator<G> {
/**
* Sets the current item in the iteration to the specified new item.
*/
public abstract void set (G item);
/**
- * Inserts the specified item before the current item in the iteration. The
- * cursor is let to point to the current item.
- */
- public abstract void insert (G item);
-
- /**
* Adds the specified item after the current item in the iteration. The
* cursor is moved to point to the new added item.
*/
--- /dev/null
+/* readonlybidirlist.vala
+ *
+ * Copyright (C) 2011 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>
+ */
+
+internal class Gee.ReadOnlyBidirList<G> : Gee.ReadOnlyList<G>, BidirList<G> {
+
+ /**
+ * Constructs a read-only list that mirrors the content of the specified
+ * list.
+ *
+ * @param list the list to decorate.
+ */
+ public ReadOnlyBidirList (BidirList<G> list) {
+ base (list);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public BidirListIterator<G> bidir_list_iterator () {
+ return new Iterator<G> (((Gee.BidirList<G>) _collection).bidir_list_iterator ());
+ }
+
+ /**
+ * The read-only view of this list.
+ */
+ public virtual new BidirList<G> read_only_view { owned get { return this; } }
+
+ private class Iterator<G> : ReadOnlyList.Iterator<G>, BidirIterator<G>, BidirListIterator<G> {
+ public Iterator (ListIterator<G> iterator) {
+ base (iterator);
+ }
+
+ public bool previous () {
+ return ((BidirIterator<G>) _iter).previous ();
+ }
+
+ public bool has_previous () {
+ return ((BidirIterator<G>) _iter).has_previous ();
+ }
+
+ public bool first () {
+ return ((BidirIterator<G>) _iter).first ();
+ }
+
+ public bool last () {
+ return ((BidirIterator<G>) _iter).last ();
+ }
+
+ public void insert (G item) {
+ assert_not_reached ();
+ }
+ }
+}
+
}
- private class Iterator<G> : ReadOnlyCollection.Iterator<G>, BidirIterator<G>, ListIterator<G> {
+ protected class Iterator<G> : ReadOnlyCollection.Iterator<G>, ListIterator<G> {
public Iterator (ListIterator<G> iterator) {
base (iterator);
}
- public bool previous () {
- return ((ListIterator<G>) _iter).previous ();
- }
-
- public bool has_previous () {
- return ((ListIterator<G>) _iter).has_previous ();
- }
-
- public bool first () {
- return ((ListIterator<G>) _iter).first ();
- }
-
- public bool last () {
- return ((ListIterator<G>) _iter).last ();
- }
-
public new void set (G item) {
assert_not_reached ();
}
- public void insert (G item) {
- assert_not_reached ();
- }
-
public void add (G item) {
assert_not_reached ();
}
tests_SOURCES = \
testarraylist.vala \
+ testbidirlist.vala \
testcase.vala \
testcollection.vala \
testdeque.vala \
testmultiset.vala \
testpriorityqueue.vala \
testqueue.vala \
+ testreadonlybidirlist.vala \
testreadonlycollection.vala \
testreadonlylist.vala \
testreadonlymap.vala \
using Gee;
-public class ArrayListTests : ListTests {
+public class ArrayListTests : BidirListTests {
public ArrayListTests () {
base ("ArrayList");
--- /dev/null
+/* testbidirlist.vala
+ *
+ * Copyright (C) 2011 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>
+ */
+
+using GLib;
+using Gee;
+
+public abstract class BidirListTests : ListTests {
+
+ public BidirListTests (string name) {
+ base (name);
+ add_test ("[List] bi-directional list iterator", test_bidir_list_iterator);
+ }
+
+ public void test_bidir_list_iterator () {
+ var test_list = test_collection as Gee.BidirList<string>;
+
+ // Check the test list is not null
+ assert (test_list != null);
+
+ // Check iterate empty list
+ var iterator = test_list.bidir_list_iterator ();
+ assert (! iterator.has_next ());
+ assert (! iterator.next ());
+ assert (! iterator.has_previous ());
+ assert (! iterator.previous ());
+ assert (! iterator.first ());
+ assert (! iterator.last ());
+
+ // Check iterate list
+ assert (test_list.add ("one"));
+ assert (test_list.add ("two"));
+ assert (test_list.add ("three"));
+
+ iterator = test_list.bidir_list_iterator ();
+ assert (iterator.next());
+ assert (iterator.get () == "one");
+ assert (iterator.index () == 0);
+ ((ListIterator<string>)iterator).set ("new one");
+ assert (iterator.next());
+ assert (iterator.get () == "two");
+ assert (iterator.index () == 1);
+ ((ListIterator<string>)iterator).set ("new two");
+ assert (test_list.size == 3);
+ assert (iterator.index () == 1);
+ iterator.insert ("before two");
+ assert (test_list.size == 4);
+ assert (iterator.index () == 2);
+ iterator.add ("after two");
+ assert (test_list.size == 5);
+ assert (iterator.index () == 3);
+ assert (iterator.next());
+ assert (iterator.get () == "three");
+ assert (iterator.index () == 4);
+ ((ListIterator<string>)iterator).set ("new three");
+ assert (! iterator.has_next ());
+ assert (! iterator.next ());
+
+ assert (iterator.first ());
+ assert (iterator.get () == "new one");
+ assert (iterator.index () == 0);
+ assert (! iterator.has_previous ());
+ assert (! iterator.previous ());
+
+ assert (iterator.last ());
+ assert (iterator.get () == "new three");
+ assert (iterator.index () == 4);
+ assert (! iterator.has_next ());
+ assert (! iterator.next ());
+
+ assert (iterator.has_previous ());
+ assert (iterator.previous ());
+ assert (iterator.get () == "after two");
+ assert (iterator.index () == 3);
+ assert (iterator.has_previous ());
+ assert (iterator.previous ());
+ assert (iterator.get () == "new two");
+ assert (iterator.index () == 2);
+ assert (iterator.has_previous ());
+ assert (iterator.previous ());
+ assert (iterator.get () == "before two");
+ assert (iterator.index () == 1);
+ assert (iterator.has_previous ());
+ assert (iterator.previous ());
+ assert (iterator.get () == "new one");
+ assert (iterator.index () == 0);
+ }
+}
+
using Gee;
-public class LinkedListTests : ListTests {
+public class LinkedListTests : BidirListTests {
public LinkedListTests () {
base ("LinkedList");
var iterator = test_list.list_iterator ();
assert (! iterator.has_next ());
assert (! iterator.next ());
- assert (! iterator.has_previous ());
- assert (! iterator.previous ());
- assert (! iterator.first ());
- assert (! iterator.last ());
// Check iterate list
assert (test_list.add ("one"));
iterator.set ("new two");
assert (test_list.size == 3);
assert (iterator.index () == 1);
- iterator.insert ("before two");
+ iterator.add ("after two");
assert (test_list.size == 4);
assert (iterator.index () == 2);
- iterator.add ("after two");
- assert (test_list.size == 5);
- assert (iterator.index () == 3);
assert (iterator.next());
assert (iterator.get () == "three");
- assert (iterator.index () == 4);
+ assert (iterator.index () == 3);
iterator.set ("new three");
assert (! iterator.has_next ());
assert (! iterator.next ());
- assert (iterator.first ());
- assert (iterator.get () == "new one");
- assert (iterator.index () == 0);
- assert (! iterator.has_previous ());
- assert (! iterator.previous ());
-
- assert (iterator.last ());
- assert (iterator.get () == "new three");
- assert (iterator.index () == 4);
- assert (! iterator.has_next ());
- assert (! iterator.next ());
-
- assert (iterator.has_previous ());
- assert (iterator.previous ());
- assert (iterator.get () == "after two");
- assert (iterator.index () == 3);
- assert (iterator.has_previous ());
- assert (iterator.previous ());
- assert (iterator.get () == "new two");
- assert (iterator.index () == 2);
- assert (iterator.has_previous ());
- assert (iterator.previous ());
- assert (iterator.get () == "before two");
- assert (iterator.index () == 1);
- assert (iterator.has_previous ());
- assert (iterator.previous ());
+ iterator = test_list.list_iterator ();
+ assert (iterator.next ());
assert (iterator.get () == "new one");
assert (iterator.index () == 0);
}
TestSuite.get_root ().add_suite (new LinkedListTests ().get_suite ());
TestSuite.get_root ().add_suite (new LinkedListAsDequeTests ().get_suite ());
TestSuite.get_root ().add_suite (new PriorityQueueTests ().get_suite ());
+ TestSuite.get_root ().add_suite (new ReadOnlyBidirListTests ().get_suite ());
TestSuite.get_root ().add_suite (new ReadOnlyCollectionTests ().get_suite ());
TestSuite.get_root ().add_suite (new ReadOnlyListTests ().get_suite ());
TestSuite.get_root ().add_suite (new ReadOnlyMapTests ().get_suite ());
--- /dev/null
+/* testreadonlybidirlist.vala
+ *
+ * Copyright (C) 2011 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>
+ */
+
+using Gee;
+
+public class ReadOnlyBidirListTests : ReadOnlyListTests {
+
+ public ReadOnlyBidirListTests () {
+ base.with_name ("ReadOnlyBidirList");
+ add_test ("[ReadOnlyBidirList] immutable iterator", test_immutable_iterator);
+ }
+
+ protected override Collection<string> get_ro_view (Collection<string> collection) {
+ return ((Gee.BidirList) collection).read_only_view;
+ }
+
+ public new void test_immutable_iterator () {
+ var test_list = test_collection as Gee.BidirList<string>;
+ var ro_list = ro_collection as Gee.BidirList<string>;
+
+ assert (test_list.add ("one"));
+ assert (test_list.add ("two"));
+
+ assert (ro_list.size == 2);
+ assert (ro_list.get (0) == "one");
+ assert (ro_list.get (1) == "two");
+
+ Gee.BidirListIterator<string> iterator = ro_list.bidir_list_iterator ();
+
+ assert (iterator.has_next ());
+ assert (iterator.next ());
+ assert (iterator.get () == "one");
+ assert (iterator.index () == 0);
+
+ assert (iterator.has_next ());
+ assert (iterator.next ());
+ assert (iterator.get () == "two");
+ assert (iterator.index () == 1);
+
+ assert (! iterator.has_next ());
+ assert (! iterator.next ());
+
+ assert (iterator.has_previous ());
+ assert (iterator.previous ());
+ assert (iterator.get () == "one");
+ assert (iterator.index () == 0);
+
+ assert (iterator.last ());
+ assert (iterator.get () == "two");
+ assert (iterator.index () == 1);
+
+ assert (iterator.first ());
+ assert (iterator.get () == "one");
+ assert (iterator.index () == 0);
+
+ if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
+ TestTrapFlags.SILENCE_STDERR)) {
+ iterator.remove ();
+ Posix.exit (0);
+ }
+ Test.trap_assert_failed ();
+ assert (ro_list.size == 2);
+ assert (ro_list.get (0) == "one");
+ assert (ro_list.get (1) == "two");
+ assert (iterator.index () == 0);
+
+ if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
+ TestTrapFlags.SILENCE_STDERR)) {
+ iterator.set ("three");
+ Posix.exit (0);
+ }
+ Test.trap_assert_failed ();
+ assert (ro_list.size == 2);
+ assert (ro_list.get (0) == "one");
+ assert (ro_list.get (1) == "two");
+ assert (iterator.index () == 0);
+
+ if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
+ TestTrapFlags.SILENCE_STDERR)) {
+ iterator.insert ("three");
+ Posix.exit (0);
+ }
+ Test.trap_assert_failed ();
+ assert (ro_list.size == 2);
+ assert (ro_list.get (0) == "one");
+ assert (ro_list.get (1) == "two");
+ assert (iterator.index () == 0);
+
+ if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
+ TestTrapFlags.SILENCE_STDERR)) {
+ iterator.add ("three");
+ Posix.exit (0);
+ }
+ Test.trap_assert_failed ();
+ assert (ro_list.size == 2);
+ assert (ro_list.get (0) == "one");
+ assert (ro_list.get (1) == "two");
+ assert (iterator.index () == 0);
+ }
+}
public class ReadOnlyListTests : ReadOnlyCollectionTests {
public ReadOnlyListTests () {
- base.with_name ("ReadOnlyList");
+ this.with_name ("ReadOnlyList");
+ }
+
+ public ReadOnlyListTests.with_name (string name) {
+ base.with_name (name);
add_test ("[ReadOnlyList] immutable iterator", test_immutable_iterator);
add_test ("[ReadOnlyList] immutable", test_immutable);
add_test ("[ReadOnlyList] accurate view", test_accurate_view);
assert (! iterator.has_next ());
assert (! iterator.next ());
- assert (iterator.has_previous ());
- assert (iterator.previous ());
- assert (iterator.get () == "one");
- assert (iterator.index () == 0);
-
- assert (iterator.last ());
- assert (iterator.get () == "two");
- assert (iterator.index () == 1);
-
- assert (iterator.first ());
- assert (iterator.get () == "one");
- assert (iterator.index () == 0);
+ iterator = ro_list.list_iterator ();
+ assert (iterator.next ());
if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
TestTrapFlags.SILENCE_STDERR)) {
assert (ro_list.get (1) == "two");
assert (iterator.index () == 0);
- if (Test.trap_fork (0, TestTrapFlags.SILENCE_STDOUT |
- TestTrapFlags.SILENCE_STDERR)) {
- iterator.insert ("three");
- Posix.exit (0);
- }
- Test.trap_assert_failed ();
assert (ro_list.size == 2);
assert (ro_list.get (0) == "one");
assert (ro_list.get (1) == "two");