2
0
woollybah 10 жил өмнө
parent
commit
c835ea4af6

+ 29 - 0
linkedlist.mod/linkedlist.bmx

@@ -86,7 +86,9 @@ Type TListEnum
 ?
 
 	Field _link:TLink
+?ngcmod
 	Field _expectedModCount:Int
+?
 	Field _list:TList
 
 	Method HasNext()
@@ -99,6 +101,7 @@ Type TListEnum
 
 			_link = Null
 			_list = Null
+?ngcmod
 			_expectedModCount = 0
 ?Threaded
 			UnlockMutex(_mutex)
@@ -108,7 +111,9 @@ Type TListEnum
 	End Method
 
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _list._modCount, "TList Concurrent Modification"
+?
 		Local value:Object=_link._value
 		Assert value<>_link
 		_link=_link._succ
@@ -125,7 +130,9 @@ Type TList
 	Field _head:TLink
 	
 	Field _count:Int
+?ngcmod
 	Field _modCount:Int
+?
 	
 	Method _pad()
 	End Method
@@ -153,7 +160,9 @@ Type TList
 		While _head._succ<>_head
 			_head._succ.Remove
 		Wend
+?ngcmod
 		_modCount :+ 1
+?
 		_count = 0
 	End Method
 
@@ -223,7 +232,9 @@ Type TList
 		Local value:Object=_head._succ._value
 		_head._succ.remove
 		_count :- 1
+?ngcmod
 		_modCount :+ 1
+?
 		Return value
 	End Method
 
@@ -236,7 +247,9 @@ Type TList
 		Local value:Object=_head._pred._value
 		_head._pred.remove
 		_count :- 1
+?ngcmod
 		_modCount :+ 1
+?
 		Return value
 	End Method
 
@@ -266,7 +279,9 @@ Type TList
 		link._pred._succ=link
 		succ._pred=link
 		_count :+ 1
+?ngcmod
 		_modCount :+ 1
+?
 		Return link
 	End Method
 
@@ -282,7 +297,9 @@ Type TList
 		link._succ._pred=link
 		pred._succ=link
 		_count :+ 1
+?ngcmod
 		_modCount :+ 1
+?
 		Return link
 	End Method
 
@@ -329,7 +346,9 @@ Type TList
 		If Not link Return False
 		link.Remove
 		_count :- 1
+?ngcmod
 		_modCount :+ 1
+?
 		Return True
 	End Method
 	
@@ -343,8 +362,10 @@ Type TList
 		Local c:Int = list._count
 		list._count = _count
 		_count = c
+?ngcmod
 		_modCount :+ 1
 		list._modCount :+ 1
+?
 	End Method
 	
 	Rem
@@ -372,7 +393,9 @@ Type TList
 			pred=succ
 			succ=link
 		Until pred=_head
+?ngcmod
 		_modCount :+ 1
+?
 	End Method
 	
 	Rem
@@ -447,18 +470,22 @@ Type TList
 			_head._pred=tail
 
 			If merges<=1 Then
+?ngcmod
 				If modded Then
 					_modCount :+ 1
 				End If
+?
 				Return
 			End If
 
 			insize:*2
 		Forever
 		
+?ngcmod
 		If modded Then
 			_modCount :+ 1
 		End If
+?
 	End Method
 		
 	Method ObjectEnumerator:TListEnum()
@@ -474,7 +501,9 @@ Type TList
 		End If
 		enum._link=_head._succ
 		enum._list = Self
+?ngcmod
 		enum._expectedModCount = _modCount
+?
 		Return enum
 	End Method
 

+ 22 - 0
map.mod/intmap.bmx

@@ -23,9 +23,11 @@ Type TIntMap
 	End Method
 
 	Method Clear()
+?ngcmod
 		If Not IsEmpty() Then
 			_modCount :+ 1
 		End If
+?
 		bmx_map_intmap_clear(Varptr _root)
 	End Method
 	
@@ -35,7 +37,9 @@ Type TIntMap
 	
 	Method Insert( key:Int,value:Object )
 		bmx_map_intmap_insert(key, value, Varptr _root)
+?ngcmod
 		_modCount :+ 1
+?
 	End Method
 
 	Method Contains:Int( key:Int )
@@ -47,7 +51,9 @@ Type TIntMap
 	End Method
 	
 	Method Remove( key:Int )
+?ngcmod
 		_modCount :+ 1
+?
 		Return bmx_map_intmap_remove(key, Varptr _root)
 	End Method
 
@@ -72,7 +78,9 @@ Type TIntMap
 		Local mapenum:TIntMapEnumerator=New TIntMapEnumerator
 		mapenum._enumerator=nodeenum
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return mapenum
 	End Method
 	
@@ -87,7 +95,9 @@ Type TIntMap
 		Local mapenum:TIntMapEnumerator=New TIntMapEnumerator
 		mapenum._enumerator=nodeenum
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return mapenum
 	End Method
 	
@@ -101,13 +111,17 @@ Type TIntMap
 		Local nodeenum:TIntNodeEnumerator=New TIntNodeEnumerator
 		nodeenum._node=_FirstNode()
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return nodeenum
 	End Method
 
 	Field _root:Byte Ptr
 
+?ngcmod
 	Field _modCount:Int
+?
 
 End Type
 
@@ -156,7 +170,9 @@ Type TIntNodeEnumerator
 	End Method
 	
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TIntMap Concurrent Modification"
+?
 		Local node:TIntNode=_node
 		_node=_node.NextNode()
 		Return node
@@ -167,13 +183,17 @@ Type TIntNodeEnumerator
 	Field _node:TIntNode	
 
 	Field _map:TIntMap
+?ngcmod
 	Field _expectedModCount:Int
+?
 End Type
 
 Type TIntKeyEnumerator Extends TIntNodeEnumerator
 	Field _key:TIntKey = New TIntKey
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TIntMap Concurrent Modification"
+?
 		Local node:TIntNode=_node
 		_node=_node.NextNode()
 		_key.value = node.Key()
@@ -183,7 +203,9 @@ End Type
 
 Type TIntValueEnumerator Extends TIntNodeEnumerator
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TIntMap Concurrent Modification"
+?
 		Local node:TIntNode=_node
 		_node=_node.NextNode()
 		Return node.Value()

+ 22 - 0
map.mod/map.bmx

@@ -13,6 +13,7 @@ ModuleInfo "Copyright: Blitz Research Ltd"
 ModuleInfo "Modserver: BRL"
 
 ModuleInfo "History: 1.08"
+ModuleInfo "History: Added TStringMap."
 ModuleInfo "History: (Debug) Assertion on modification during iteration."
 ModuleInfo "History: 1.07 Release"
 ModuleInfo "History: Fixed MapKeys/MapValues functions to return enumerators"
@@ -30,6 +31,7 @@ ModuleInfo "History: Fixed TMap.Remove:TNode not returning node"
 
 Import "intmap.bmx"
 Import "ptrmap.bmx"
+Import "stringmap.bmx"
 Import "map.c"
 
 Private
@@ -136,7 +138,9 @@ Type TNodeEnumerator
 	End Method
 	
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TMap Concurrent Modification"
+?
 		Local node:TNode=_node
 		_node=_node.NextNode()
 		Return node
@@ -152,7 +156,9 @@ End Type
 
 Type TKeyEnumerator Extends TNodeEnumerator
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TMap Concurrent Modification"
+?
 		Local node:TNode=_node
 		_node=_node.NextNode()
 		Return node._key
@@ -161,7 +167,9 @@ End Type
 
 Type TValueEnumerator Extends TNodeEnumerator
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TMap Concurrent Modification"
+?
 		Local node:TNode=_node
 		_node=_node.NextNode()
 		Return node._value
@@ -188,7 +196,9 @@ Type TMap
 		If _root=nil Return
 		_root.Clear
 		_root=nil
+?ngcmod
 		_modCount :+ 1
+?
 	End Method
 	
 	Method IsEmpty()
@@ -220,7 +230,9 @@ Type TMap
 		node._color=RED
 		node._parent=parent
 
+?ngcmod
 		_modCount :+ 1
+?
 		
 		If parent=nil
 			_root=node
@@ -248,7 +260,9 @@ Type TMap
 		Local node:TNode=_FindNode( key )
 		If node=nil Return 0
 		 _RemoveNode node
+?ngcmod
 		_modCount :+ 1
+?
 		Return 1
 	End Method
 	
@@ -258,7 +272,9 @@ Type TMap
 		Local mapenum:TMapEnumerator=New TMapEnumerator
 		mapenum._enumerator=nodeenum
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return mapenum
 	End Method
 	
@@ -268,7 +284,9 @@ Type TMap
 		Local mapenum:TMapEnumerator=New TMapEnumerator
 		mapenum._enumerator=nodeenum
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return mapenum
 	End Method
 	
@@ -282,7 +300,9 @@ Type TMap
 		Local nodeenum:TNodeEnumerator=New TNodeEnumerator
 		nodeenum._node=_FirstNode()
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return nodeenum
 	End Method
 	
@@ -500,7 +520,9 @@ Type TMap
 	
 	Field _root:TNode=nil
 	
+?ngcmod
 	Field _modCount:Int
+?
 End Type
 
 Rem

+ 127 - 0
map.mod/map.c

@@ -250,3 +250,130 @@ void bmx_map_ptrmap_copy(struct tree_root ** dst_root, struct tree_root * src_ro
 		bmx_map_ptrmap_insert(src_node->key, src_node->value, dst_root);
 	}
 }
+
+/* +++++++++++++++++++++++++++++++++++++++++++++++++++++ */
+
+struct stringmap_node {
+	struct tree_root link;
+	BBString * key;
+	BBOBJECT value;
+};
+
+static int compare_stringmap_nodes(const void *x, const void *y) {
+        struct stringmap_node * node_x = (struct stringmap_node *)x;
+        struct stringmap_node * node_y = (struct stringmap_node *)y;
+
+        return bbStringCompare(node_x->key, node_y->key);
+}
+
+void bmx_map_stringmap_clear(struct tree_root ** root) {
+	struct stringmap_node *node;
+	struct stringmap_node *tmp;
+	tree_for_each_entry_safe(node, tmp, *root, link) {
+		BBRELEASE(node->key);
+		BBRELEASE(node->value);
+		tree_del(&node->link, root);
+		free(node);
+	}
+}
+
+int bmx_map_stringmap_isempty(struct tree_root ** root) {
+	return *root == 0;
+}
+
+void bmx_map_stringmap_insert( BBString * key, BBObject *value, struct tree_root ** root) {
+	struct stringmap_node * node = (struct stringmap_node *)malloc(sizeof(struct stringmap_node));
+	node->key = key;
+	BBRETAIN(key);
+	node->value = value;
+	BBRETAIN(value);
+	
+	struct stringmap_node * old_node = (struct stringmap_node *)tree_map(&node->link, compare_intmap_nodes, root);
+
+	if (&node->link != &old_node->link) {
+		BBRELEASE(old_node->key);
+		BBRELEASE(old_node->value);
+		// key already exists. Store the value in this node.
+		old_node->value = value;
+		// delete the new node, since we don't need it
+		free(node);
+	}
+}
+
+int bmx_map_stringmap_contains(BBString * key, struct tree_root ** root) {
+	struct stringmap_node node;
+	node.key = key;
+	
+	struct stringmap_node * found = (struct stringmap_node *)tree_search(&node, compare_intmap_nodes, *root);
+	if (found) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+BBObject * bmx_map_stringmap_valueforkey(int key, struct tree_root ** root) {
+	struct stringmap_node node;
+	node.key = key;
+	
+	struct stringmap_node * found = (struct stringmap_node *)tree_search(&node, compare_intmap_nodes, *root);
+	
+	if (found) {
+		return found->value;
+	}
+	
+	return &bbNullObject;
+}
+
+int bmx_map_stringmap_remove(int key, struct tree_root ** root) {
+	struct stringmap_node node;
+	node.key = key;
+	
+	struct stringmap_node * found = (struct stringmap_node *)tree_search(&node, compare_intmap_nodes, *root);
+	
+	if (found) {
+		BBRELEASE(found->key);
+		BBRELEASE(found->value);
+		tree_del(&found->link, root);
+		free(found);
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+struct stringmap_node * bmx_map_stringmap_nextnode(struct stringmap_node * node) {
+	return tree_successor(node);
+}
+
+struct stringmap_node * bmx_map_stringmap_firstnode(struct tree_root * root) {
+	return tree_min(root);
+}
+
+BBString * bmx_map_stringmap_key(struct stringmap_node * node) {
+	return node->key;
+}
+
+BBObject * bmx_map_stringmap_value(struct stringmap_node * node) {
+	return node->value;
+}
+
+int bmx_map_stringmap_hasnext(struct stringmap_node * node, struct tree_root * root) {
+	if (!root) {
+		return 0;
+	}
+	
+	if (!node) {
+		return 1;
+	}
+	
+	return (tree_successor(node) != 0) ? 1 : 0;
+}
+
+void bmx_map_stringmap_copy(struct tree_root ** dst_root, struct tree_root * src_root) {
+	struct stringmap_node *src_node;
+	struct stringmap_node *tmp;
+	tree_for_each_entry_safe(src_node, tmp, src_root, link) {
+		bmx_map_stringmap_insert(src_node->key, src_node->value, dst_root);
+	}
+}

+ 22 - 0
map.mod/ptrmap.bmx

@@ -23,9 +23,11 @@ Type TPtrMap
 	End Method
 
 	Method Clear()
+?ngcmod
 		If Not IsEmpty() Then
 			_modCount :+ 1
 		End If
+?
 		bmx_map_ptrmap_clear(Varptr _root)
 	End Method
 	
@@ -35,7 +37,9 @@ Type TPtrMap
 	
 	Method Insert( key:Byte Ptr,value:Object )
 		bmx_map_ptrmap_insert(key, value, Varptr _root)
+?ngcmod
 		_modCount :+ 1
+?
 	End Method
 
 	Method Contains:Int( key:Byte Ptr )
@@ -47,7 +51,9 @@ Type TPtrMap
 	End Method
 	
 	Method Remove( key:Byte Ptr )
+?ngcmod
 		_modCount :+ 1
+?
 		Return bmx_map_ptrmap_remove(key, Varptr _root)
 	End Method
 
@@ -72,7 +78,9 @@ Type TPtrMap
 		Local mapenum:TPtrMapEnumerator=New TPtrMapEnumerator
 		mapenum._enumerator=nodeenum
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return mapenum
 	End Method
 	
@@ -87,7 +95,9 @@ Type TPtrMap
 		Local mapenum:TPtrMapEnumerator=New TPtrMapEnumerator
 		mapenum._enumerator=nodeenum
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return mapenum
 	End Method
 	
@@ -101,13 +111,17 @@ Type TPtrMap
 		Local nodeenum:TPtrNodeEnumerator=New TPtrNodeEnumerator
 		nodeenum._node=_FirstNode()
 		nodeenum._map = Self
+?ngcmod
 		nodeenum._expectedModCount = _modCount
+?
 		Return nodeenum
 	End Method
 
 	Field _root:Byte Ptr
 	
+?ngcmod
 	Field _modCount:Int
+?
 	
 End Type
 
@@ -156,7 +170,9 @@ Type TPtrNodeEnumerator
 	End Method
 	
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TPtrMap Concurrent Modification"
+?
 		Local node:TPtrNode=_node
 		_node=_node.NextNode()
 		Return node
@@ -167,13 +183,17 @@ Type TPtrNodeEnumerator
 	Field _node:TPtrNode	
 
 	Field _map:TPtrMap
+?ngcmod
 	Field _expectedModCount:Int
+?
 End Type
 
 Type TPtrKeyEnumerator Extends TPtrNodeEnumerator
 	Field _key:TPtrKey = New TPtrKey
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TPtrMap Concurrent Modification"
+?
 		Local node:TPtrNode=_node
 		_node=_node.NextNode()
 		_key.value = node.Key()
@@ -183,7 +203,9 @@ End Type
 
 Type TPtrValueEnumerator Extends TPtrNodeEnumerator
 	Method NextObject:Object()
+?ngcmod
 		Assert _expectedModCount = _map._modCount, "TPtrMap Concurrent Modification"
+?
 		Local node:TPtrNode=_node
 		_node=_node.NextNode()
 		Return node.Value()