changeset 127:1997315125c0

more collection impls
author Frank Benoit <benoit@tionex.de>
date Sun, 17 Aug 2008 01:30:26 +0200
parents 3d684126a966
children 8df1d4193877
files dwtx/dwtxhelper/Collection.d
diffstat 1 files changed, 75 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/dwtx/dwtxhelper/Collection.d	Sun Aug 17 01:30:09 2008 +0200
+++ b/dwtx/dwtxhelper/Collection.d	Sun Aug 17 01:30:26 2008 +0200
@@ -389,9 +389,12 @@
     abstract  int   size();
 }
 
+
+// no nulls
+// synchronized
 class Hashtable : Dictionary, Map {
 
-    public Object        get(String key){
+    public Object get(String key){
         return super.get(key);
     }
     public Object put(String key, Object value){
@@ -407,9 +410,10 @@
         return super.remove(key);
     }
 
+    Object[Object] map;
+
     // The HashMap  class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
     public this(){
-        implMissing( __FILE__, __LINE__ );
     }
     public this(int initialCapacity){
         implMissing( __FILE__, __LINE__ );
@@ -425,23 +429,43 @@
         implMissing( __FILE__, __LINE__ );
         return null;
     }
-    Enumeration        keys() {
-        implMissing( __FILE__, __LINE__ );
-        return null;
+    class KeysEnumeration : Enumeration {
+        Object[] keys;
+        int index = 0;
+        this( Object[] keys ){
+            this.keys = keys;
+        }
+        public bool hasMoreElements(){
+            return index < keys.length;
+        }
+        public Object nextElement(){
+            Object res = keys[index];
+            index++;
+            return res;
+        }
     }
-    public void clear(){
-        implMissing( __FILE__, __LINE__ );
+
+    Enumeration        keys() {
+        return new KeysEnumeration( map.keys );
     }
-    public bool containsKey(Object key){
-        implMissing( __FILE__, __LINE__ );
+    public synchronized void clear(){
+        map = null;
+    }
+    public synchronized bool containsKey(Object key){
+        if( auto v = key in map ){
+            return true;
+        }
         return false;
     }
-    public bool containsKey(String key){
-        implMissing( __FILE__, __LINE__ );
-        return false;
+    public synchronized bool containsKey(String key){
+        return containsKey(stringcast(key));
     }
-    public bool containsValue(Object value){
-        implMissing( __FILE__, __LINE__ );
+    public synchronized bool containsValue(Object value){
+        foreach( k, v; map ){
+            if( v == value ){
+                return true;
+            }
+        }
         return false;
     }
     public Set  entrySet(){
@@ -452,38 +476,43 @@
         implMissing( __FILE__, __LINE__ );
         return 0;
     }
-    public Object get(Object key){
-        implMissing( __FILE__, __LINE__ );
+    public synchronized Object get(Object key){
+        if( auto v = key in map ){
+            return *v;
+        }
         return null;
     }
     public hash_t toHash(){
         implMissing( __FILE__, __LINE__ );
         return 0;
     }
-    public bool isEmpty(){
-        implMissing( __FILE__, __LINE__ );
-        return false;
+    public synchronized bool isEmpty(){
+        return map.length is 0;
     }
     public Set    keySet(){
         implMissing( __FILE__, __LINE__ );
         return null;
     }
-    public Object put(Object key, Object value){
-        implMissing( __FILE__, __LINE__ );
-        return null;
+    public synchronized Object put(Object key, Object value){
+        Object res = null;
+        if( auto v = key in map ){
+            res = *v;
+        }
+        map[ key ] = value;
+        return res;
     }
 //     public Object put(String key, Object value)
 //     public Object put(Object key, String value)
 //     public Object put(String key, String value)
-    public void   putAll(Map t){
+    public synchronized void   putAll(Map t){
         implMissing( __FILE__, __LINE__ );
     }
-    public Object remove(Object key){
+    public synchronized Object remove(Object key){
         implMissing( __FILE__, __LINE__ );
         return null;
     }
 //     public Object remove(String key)
-    public int    size(){
+    public synchronized int    size(){
         implMissing( __FILE__, __LINE__ );
         return 0;
     }
@@ -1120,14 +1149,16 @@
 }
 
 class Vector : AbstractList, List {
+    Object[] vect;
+    int used;
+    int capacityIncrement = 32;
     public this(){
-        implMissing( __FILE__, __LINE__ );
     }
     public this(Collection c){
         implMissing( __FILE__, __LINE__ );
     }
     public this(int initialCapacity){
-        implMissing( __FILE__, __LINE__ );
+        vect.length = initialCapacity;
     }
     public this(int initialCapacity, int capacityIncrement){
         implMissing( __FILE__, __LINE__ );
@@ -1136,8 +1167,12 @@
         implMissing( __FILE__, __LINE__ );
     }
     public bool    add(Object o){
-        implMissing( __FILE__, __LINE__ );
-        return false;
+        if( used + 1 >= vect.length ){
+            vect.length = vect.length + capacityIncrement;
+        }
+        vect[used] = o;
+        used++;
+        return true;
     }
     public bool    add(String o){
         return add(stringcast(o));
@@ -1151,14 +1186,13 @@
         return false;
     }
     public void   addElement(Object obj){
-        implMissing( __FILE__, __LINE__ );
+        add(obj);
     }
     public int    capacity(){
-        implMissing( __FILE__, __LINE__ );
-        return 0;
+        return vect.length;
     }
     public void   clear(){
-        implMissing( __FILE__, __LINE__ );
+        used = 0;
     }
     public Object     clone(){
         implMissing( __FILE__, __LINE__ );
@@ -1176,8 +1210,7 @@
         implMissing( __FILE__, __LINE__ );
     }
     public Object     elementAt(int index){
-        implMissing( __FILE__, __LINE__ );
-        return null;
+        return get(index);
     }
     public Enumeration    elements(){
         implMissing( __FILE__, __LINE__ );
@@ -1195,8 +1228,10 @@
         return null;
     }
     public Object     get(int index){
-        implMissing( __FILE__, __LINE__ );
-        return null;
+        if( index >= used || index < 0 ){
+            throw new tango.core.Exception.ArrayBoundsException( __FILE__, __LINE__ );
+        }
+        return vect[index];
     }
     public hash_t    toHash(){
         implMissing( __FILE__, __LINE__ );
@@ -1214,8 +1249,7 @@
         implMissing( __FILE__, __LINE__ );
     }
     public bool    isEmpty(){
-        implMissing( __FILE__, __LINE__ );
-        return false;
+        return used is 0;
     }
     public Iterator   iterator(){
         implMissing( __FILE__, __LINE__ );
@@ -1284,16 +1318,14 @@
         implMissing( __FILE__, __LINE__ );
     }
     public int    size(){
-        implMissing( __FILE__, __LINE__ );
-        return 0;
+        return used;
     }
     public List   subList(int fromIndex, int toIndex){
         implMissing( __FILE__, __LINE__ );
         return null;
     }
     public Object[]   toArray(){
-        implMissing( __FILE__, __LINE__ );
-        return null;
+        return vect[ 0 .. used ].dup;
     }
     public Object[]   toArray(Object[] a){
         implMissing( __FILE__, __LINE__ );