Commit 7ef23e9e authored by Jürg Billeter's avatar Jürg Billeter

gee: Replace interfaces by abstract classes to improve performance

parent 994935e2
......@@ -14,7 +14,6 @@ noinst_LTLIBRARIES = \
libgee_la_VALASOURCES = \
arraylist.vala \
collection.vala \
collectionobject.vala \
hashmap.vala \
hashset.vala \
iterable.vala \
......
......@@ -27,8 +27,8 @@ using GLib;
/**
* Arrays of arbitrary elements which grow automatically as elements are added.
*/
public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, List<G> {
public int size {
public class Vala.ArrayList<G> : List<G> {
public override int size {
get { return _size; }
}
......@@ -47,19 +47,19 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
this.equal_func = equal_func;
}
public Type get_element_type () {
public override Type get_element_type () {
return typeof (G);
}
public Vala.Iterator<G> iterator () {
public override Vala.Iterator<G> iterator () {
return new Iterator<G> (this);
}
public bool contains (G item) {
public override bool contains (G item) {
return (index_of (item) != -1);
}
public int index_of (G item) {
public override int index_of (G item) {
for (int index = 0; index < _size; index++) {
if (_equal_func (_items[index], item)) {
return index;
......@@ -68,19 +68,19 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
return -1;
}
public G? get (int index) {
public override G? get (int index) {
assert (index >= 0 && index < _size);
return _items[index];
}
public void set (int index, G item) {
public override void set (int index, G item) {
assert (index >= 0 && index < _size);
_items[index] = item;
}
public bool add (G item) {
public override bool add (G item) {
if (_size == _items.length) {
grow_if_needed (1);
}
......@@ -89,7 +89,7 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
return true;
}
public void insert (int index, G item) {
public override void insert (int index, G item) {
assert (index >= 0 && index <= _size);
if (_size == _items.length) {
......@@ -100,7 +100,7 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
_stamp++;
}
public bool remove (G item) {
public override bool remove (G item) {
for (int index = 0; index < _size; index++) {
if (_equal_func (_items[index], item)) {
remove_at (index);
......@@ -110,7 +110,7 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
return false;
}
public void remove_at (int index) {
public override void remove_at (int index) {
assert (index >= 0 && index < _size);
_items[index] = null;
......@@ -120,7 +120,7 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
_stamp++;
}
public void clear () {
public override void clear () {
for (int index = 0; index < _size; index++) {
_items[index] = null;
}
......@@ -152,7 +152,7 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
_items.resize (value);
}
private class Iterator<G> : CollectionObject, Vala.Iterator<G> {
private class Iterator<G> : Vala.Iterator<G> {
public ArrayList<G> list {
set {
_list = value;
......@@ -170,7 +170,7 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
this.list = list;
}
public bool next () {
public override bool next () {
assert (_stamp == _list._stamp);
if (_index < _list._size) {
_index++;
......@@ -178,7 +178,7 @@ public class Vala.ArrayList<G> : CollectionObject, Iterable<G>, Collection<G>, L
return (_index < _list._size);
}
public G? get () {
public override G? get () {
assert (_stamp == _list._stamp);
if (_index < 0 || _index >= _list._size) {
......
......@@ -24,7 +24,7 @@
* Serves as the base interface for implementing collection classes. Defines
* size, iteration, and modification methods.
*/
public interface Vala.Collection<G> : Iterable<G> {
public abstract class Vala.Collection<G> : Iterable<G> {
/**
* The number of items in this collection.
*/
......
/* collectionobject.vala
*
* Copyright (C) 2008 Jürg Billeter
*
* 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:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
/**
* Base class for all collections.
*/
public class Vala.CollectionObject {
}
......@@ -27,8 +27,8 @@ using GLib;
/**
* Hashtable implementation of the Map interface.
*/
public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
public int size {
public class Vala.HashMap<K,V> : Map<K,V> {
public override int size {
get { return _nnodes; }
}
......@@ -66,11 +66,11 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
_nodes = new Node<K,V>[_array_size];
}
public Set<K> get_keys () {
public override Set<K> get_keys () {
return new KeySet<K,V> (this);
}
public Collection<V> get_values () {
public override Collection<V> get_values () {
return new ValueCollection<K,V> (this);
}
......@@ -83,12 +83,12 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
return node;
}
public bool contains (K key) {
public override bool contains (K key) {
Node<K,V>** node = lookup_node (key);
return (*node != null);
}
public V? get (K key) {
public override V? get (K key) {
Node<K,V>* node = (*lookup_node (key));
if (node != null) {
return node->value;
......@@ -97,7 +97,7 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
}
}
public void set (K key, V value) {
public override void set (K key, V value) {
Node<K,V>** node = lookup_node (key);
if (*node != null) {
(*node)->value = value;
......@@ -110,7 +110,7 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
_stamp++;
}
public bool remove (K key) {
public override bool remove (K key) {
Node<K,V>** node = lookup_node (key);
if (*node != null) {
Node<K,V> next = (owned) (*node)->next;
......@@ -129,7 +129,7 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
return false;
}
public void clear () {
public override void clear () {
for (int i = 0; i < _array_size; i++) {
Node<K,V> node = (owned) _nodes[i];
while (node != null) {
......@@ -184,7 +184,7 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
}
}
private class KeySet<K,V> : CollectionObject, Iterable<K>, Collection<K>, Set<K> {
private class KeySet<K,V> : Set<K> {
public HashMap<K,V> map {
set { _map = value; }
}
......@@ -195,36 +195,36 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
this.map = map;
}
public Type get_element_type () {
public override Type get_element_type () {
return typeof (K);
}
public Iterator<K> iterator () {
public override Iterator<K> iterator () {
return new KeyIterator<K,V> (_map);
}
public int size {
public override int size {
get { return _map.size; }
}
public bool add (K key) {
public override bool add (K key) {
assert_not_reached ();
}
public void clear () {
public override void clear () {
assert_not_reached ();
}
public bool remove (K key) {
public override bool remove (K key) {
assert_not_reached ();
}
public bool contains (K key) {
public override bool contains (K key) {
return _map.contains (key);
}
}
private class KeyIterator<K,V> : CollectionObject, Iterator<K> {
private class KeyIterator<K,V> : Iterator<K> {
public HashMap<K,V> map {
set {
_map = value;
......@@ -243,7 +243,7 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
this.map = map;
}
public bool next () {
public override bool next () {
if (_node != null) {
_node = _node.next;
}
......@@ -254,14 +254,14 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
return (_node != null);
}
public K? get () {
public override K? get () {
assert (_stamp == _map._stamp);
assert (_node != null);
return _node.key;
}
}
private class ValueCollection<K,V> : CollectionObject, Iterable<V>, Collection<V> {
private class ValueCollection<K,V> : Collection<V> {
public HashMap<K,V> map {
set { _map = value; }
}
......@@ -272,31 +272,31 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
this.map = map;
}
public Type get_element_type () {
public override Type get_element_type () {
return typeof (V);
}
public Iterator<V> iterator () {
public override Iterator<V> iterator () {
return new ValueIterator<K,V> (_map);
}
public int size {
public override int size {
get { return _map.size; }
}
public bool add (V value) {
public override bool add (V value) {
assert_not_reached ();
}
public void clear () {
public override void clear () {
assert_not_reached ();
}
public bool remove (V value) {
public override bool remove (V value) {
assert_not_reached ();
}
public bool contains (V value) {
public override bool contains (V value) {
Iterator<V> it = iterator ();
while (it.next ()) {
if (_map._value_equal_func (it.get (), value)) {
......@@ -307,7 +307,7 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
}
}
private class ValueIterator<K,V> : CollectionObject, Iterator<V> {
private class ValueIterator<K,V> : Iterator<V> {
public HashMap<K,V> map {
set {
_map = value;
......@@ -326,7 +326,7 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
this.map = map;
}
public bool next () {
public override bool next () {
if (_node != null) {
_node = _node.next;
}
......@@ -337,7 +337,7 @@ public class Vala.HashMap<K,V> : CollectionObject, Map<K,V> {
return (_node != null);
}
public V? get () {
public override V? get () {
assert (_stamp == _map._stamp);
assert (_node != null);
return _node.value;
......
......@@ -27,8 +27,8 @@ using GLib;
/**
* Hashtable implementation of the Set interface.
*/
public class Vala.HashSet<G> : CollectionObject, Iterable<G>, Collection<G>, Set<G> {
public int size {
public class Vala.HashSet<G> : Set<G> {
public override int size {
get { return _nnodes; }
}
......@@ -69,20 +69,20 @@ public class Vala.HashSet<G> : CollectionObject, Iterable<G>, Collection<G>, Set
return node;
}
public bool contains (G key) {
public override bool contains (G key) {
Node<G>** node = lookup_node (key);
return (*node != null);
}
public Type get_element_type () {
public override Type get_element_type () {
return typeof (G);
}
public Vala.Iterator<G> iterator () {
public override Vala.Iterator<G> iterator () {
return new Iterator<G> (this);
}
public bool add (G key) {
public override bool add (G key) {
Node<G>** node = lookup_node (key);
if (*node != null) {
return false;
......@@ -96,7 +96,7 @@ public class Vala.HashSet<G> : CollectionObject, Iterable<G>, Collection<G>, Set
}
}
public bool remove (G key) {
public override bool remove (G key) {
Node<G>** node = lookup_node (key);
if (*node != null) {
Node<G> next = (owned) (*node)->next;
......@@ -114,7 +114,7 @@ public class Vala.HashSet<G> : CollectionObject, Iterable<G>, Collection<G>, Set
return false;
}
public void clear () {
public override void clear () {
for (int i = 0; i < _array_size; i++) {
Node<G> node = (owned) _nodes[i];
while (node != null) {
......@@ -166,7 +166,7 @@ public class Vala.HashSet<G> : CollectionObject, Iterable<G>, Collection<G>, Set
}
}
private class Iterator<G> : CollectionObject, Vala.Iterator<G> {
private class Iterator<G> : Vala.Iterator<G> {
public HashSet<G> set {
set {
_set = value;
......@@ -185,7 +185,7 @@ public class Vala.HashSet<G> : CollectionObject, Iterable<G>, Collection<G>, Set
this.set = set;
}
public bool next () {
public override bool next () {
if (_node != null) {
_node = _node.next;
}
......@@ -196,7 +196,7 @@ public class Vala.HashSet<G> : CollectionObject, Iterable<G>, Collection<G>, Set
return (_node != null);
}
public G? get () {
public override G? get () {
assert (_stamp == _set._stamp);
assert (_node != null);
return _node.key;
......
......@@ -26,7 +26,7 @@ using GLib;
* Implemented by classes that support a simple iteration over instances of the
* collection.
*/
public interface Vala.Iterable<G> : CollectionObject {
public abstract class Vala.Iterable<G> {
public abstract Type get_element_type ();
/**
......
......@@ -24,7 +24,7 @@
* Implemented by classes that support a simple iteration over instances of the
* collection.
*/
public interface Vala.Iterator<G> : CollectionObject {
public abstract class Vala.Iterator<G> {
/**
* Advances to the next element in the iteration.
*
......
......@@ -23,7 +23,7 @@
/**
* Represents a collection of items in a well-defined order.
*/
public interface Vala.List<G> : Collection<G> {
public abstract class Vala.List<G> : Collection<G> {
/**
* Returns the item at the specified index in this list.
*
......
......@@ -23,7 +23,7 @@
/**
* A map is a generic collection of key/value pairs.
*/
public interface Vala.Map<K,V> : CollectionObject {
public abstract class Vala.Map<K,V> {
/**
* The number of items in this map.
*/
......
......@@ -23,6 +23,6 @@
/**
* A set is a collection without duplicates.
*/
public interface Vala.Set<G> : Collection<G> {
public abstract class Vala.Set<G> : Collection<G> {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment