monkey2 icon indicating copy to clipboard operation
monkey2 copied to clipboard

Indexers for lists

Open coconcode opened this issue 8 years ago • 2 comments

This is useful for going in the standard library.

code updated below

coconcode avatar Sep 26 '17 08:09 coconcode

I'm fine with something like ValueAtIndex() but do not like the idea of Operator[] for lists, as this is an O(N) operation and I feel that, in general, operator[]'s should be O(1) or at least reasonably fast-ish.

blitz-research avatar Nov 07 '17 22:11 blitz-research

Here is the new version. That O(N) problem can't be dealt however. Perhaps we could make a special point in the documentation that using the list is suitable for non intensive loops.

diff --git a/modules/std/collections/list.monkey2 b/modules/std/collections/list.monkey2
index f69fad78..ba754c13 100644
--- a/modules/std/collections/list.monkey2
+++ b/modules/std/collections/list.monkey2
@@ -705,4 +705,66 @@ Class List<T> Implements IContainer<T>
 		Return Null
 	End
 	
+	
+	#rem monkeydoc Finds the value in a list by it's index number.
+	
+	If the index value is out of range null will be returned.
+	
+	@param value The index number.
+	
+	@return The value, or null if the value was not found.
+	
+	#end
+	
+	Operator []:T(index:Int)
+		Local count := Count()
+		If index < 0 Or index >= count Then Return Null
+		Local node  := _head._succ
+		For Local i := 0 Until count
+			If i = index Then Return node._value
+			node = node._succ
+		Next
+		Return Null
+	End
+	
+	#rem monkeydoc Sets the value of a list element.
+	
+	If the index value is out of range the action won't happen.
+	
+	@param index The index of the element to set.
+	
+	@param value The value to set.
+	
+	#end
+	
+	Operator []=(index:Int, value:T)
+		Local count := Count()
+		If index < 0 Or index >= count Then Return
+		Local node  := _head._succ
+		For Local i := 0 Until count
+			If i = index
+				node._value = value
+				Exit
+			End
+			node = node._succ
+		Next
+	End
+	
+	#rem monkeydoc Finds the list index number with that value.
+	
+	@param value The value to search.
+	
+	@return The index number, or null if the index number was not found.
+	
+	#end
+	
+	Method IndexOf:Int(value:T)
+		Local count := 0
+		For Local item := Eachin Self
+			If item = value Then Return count
+			count += 1
+		End
+		Return Null
+	End
+	
 End
diff --git a/modules/std/tests/collections.monkey2 b/modules/std/tests/collections.monkey2
new file mode 100644
index 00000000..28aaa1cc
--- /dev/null
+++ b/modules/std/tests/collections.monkey2
@@ -0,0 +1,45 @@
+#Import "<std>"
+Using std..
+
+Function Main()
+
+	Local fruits := New List<String>
+	
+	' adding stuff to list
+	fruits.AddAll(New String[] ("Apple", "Strawberry", "Orange", "Banana"))
+	
+	' accessing the contents of the list
+	For Local i := 0 Until fruits.Count()
+		Print(i + " " + fruits[i])
+	Next
+	
+	' changing some contents in the list
+	fruits[0] = "Pear <--"
+	fruits[2] = "Lemon <--"
+	
+	Print("")
+	
+	' seeing the changes
+	For Local i := 0 Until fruits.Count()
+		Print(i + " " + fruits[i])
+	Next
+
+	' trying setting values by going out of bounds
+	fruits[-1] = ""
+	fruits[100] = ""
+	
+	Print("")
+	
+	' seeing the changes
+	For Local i := 0 Until fruits.Count()
+		Print(i + " " + fruits[i])
+	Next
+	
+	Print("")
+	
+	' trying getting values by going out of bounds
+	Print("value of -1  :" + fruits[-1])
+	Print("value of 100 :" + fruits[100])
+	
+
+End

coconcode avatar Nov 12 '18 10:11 coconcode