浏览代码

Added index operator overloads to maps.

woollybah 6 年之前
父节点
当前提交
5386a9a0ff
共有 4 个文件被更改,包括 72 次插入2 次删除
  1. 17 0
      map.mod/intmap.bmx
  2. 21 2
      map.mod/map.bmx
  3. 17 0
      map.mod/ptrmap.bmx
  4. 17 0
      map.mod/stringmap.bmx

+ 17 - 0
map.mod/intmap.bmx

@@ -162,6 +162,23 @@ Type TIntMap
 		Return nodeenum
 	End Method
 
+	Rem
+	bbdoc: Finds a value given a @key using index syntax.
+	returns: The value associated with @key.
+	about: If the map does not contain @key, a #Null object is returned.
+	End Rem
+	Method Operator[]:Object(key:Int)
+		Return bmx_map_intmap_valueforkey(key, Varptr _root)
+	End Method
+	
+	Rem
+	bbdoc: Inserts a key/value pair into the map using index syntax.
+	about: If the map already contains @key, its value is overwritten with @value. 
+	End Rem
+	Method Operator[]=(key:Int, value:Object)
+		bmx_map_intmap_insert(key, value, Varptr _root)
+	End Method
+
 	Field _root:Byte Ptr
 
 ?ngcmod

+ 21 - 2
map.mod/map.bmx

@@ -6,12 +6,14 @@ bbdoc: Data structures/Maps
 End Rem
 Module BRL.Map
 
-ModuleInfo "Version: 1.08"
+ModuleInfo "Version: 1.09"
 ModuleInfo "Author: Mark Sibly"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "Copyright: Blitz Research Ltd"
 ModuleInfo "Modserver: BRL"
 
+ModuleInfo "History: 1.09"
+ModuleInfo "History: Added index operator overloads to maps."
 ModuleInfo "History: 1.08"
 ModuleInfo "History: Added TStringMap."
 ModuleInfo "History: (Debug) Assertion on modification during iteration."
@@ -342,7 +344,7 @@ Type TMap
 	End Method
 
 	Rem
-	bbdoc: Returns a node enumeration object.
+	bbdoc: Returns a node enumeration Object.
 	about: The object returned by #ObjectEnumerator can be used with #EachIn to iterate through the nodes in the map.
 	End Rem
 	Method ObjectEnumerator:TNodeEnumerator()
@@ -564,7 +566,24 @@ Type TMap
 		Wend
 		node._color=BLACK
 	End Method
+
+	Rem
+	bbdoc: Finds a value given a @key using index syntax.
+	returns: The value associated with @key.
+	about: If the map does not contain @key, a #Null object is returned.
+	End Rem
+	Method Operator[]:Object(key:Object)
+		Return ValueForKey(key)
+	End Method
 	
+	Rem
+	bbdoc: Inserts a key/value pair into the map using index syntax.
+	about: If the map already contains @key, its value is overwritten with @value. 
+	End Rem
+	Method Operator[]=(key:Object, value:Object)
+		Insert(key, value)
+	End Method
+
 	Const RED=-1,BLACK=1
 	
 	Field _root:TNode=nil

+ 17 - 0
map.mod/ptrmap.bmx

@@ -162,6 +162,23 @@ Type TPtrMap
 		Return nodeenum
 	End Method
 
+	Rem
+	bbdoc: Finds a value given a @key using index syntax.
+	returns: The value associated with @key.
+	about: If the map does not contain @key, a #Null object is returned.
+	End Rem
+	Method Operator[]:Object(key:Byte Ptr)
+		Return bmx_map_ptrmap_valueforkey(key, Varptr _root)
+	End Method
+	
+	Rem
+	bbdoc: Inserts a key/value pair into the map using index syntax.
+	about: If the map already contains @key, its value is overwritten with @value. 
+	End Rem
+	Method Operator[]=(key:Byte Ptr, value:Object)
+		bmx_map_ptrmap_insert(key, value, Varptr _root)
+	End Method
+
 	Field _root:Byte Ptr
 	
 ?ngcmod

+ 17 - 0
map.mod/stringmap.bmx

@@ -161,6 +161,23 @@ Type TStringMap
 ?
 		Return nodeenum
 	End Method
+	
+	Rem
+	bbdoc: Finds a value given a @key using index syntax.
+	returns: The value associated with @key.
+	about: If the map does not contain @key, a #Null object is returned.
+	End Rem
+	Method Operator[]:Object(key:String)
+		Return bmx_map_stringmap_valueforkey(key, Varptr _root)
+	End Method
+	
+	Rem
+	bbdoc: Inserts a key/value pair into the map using index syntax.
+	about: If the map already contains @key, its value is overwritten with @value. 
+	End Rem
+	Method Operator[]=(key:String, value:Object)
+		bmx_map_stringmap_insert(key, value, Varptr _root)
+	End Method
 
 	Field _root:Byte Ptr