mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 14:18:42 +01:00
List; add indexOf(value)
This commit is contained in:
@ -32,6 +32,16 @@ Removes all elements from the list.
|
|||||||
|
|
||||||
The number of elements in the list.
|
The number of elements in the list.
|
||||||
|
|
||||||
|
### **indexOf(value)**
|
||||||
|
|
||||||
|
Returns the index of `value` in the list, if found. If not found, returns -1.
|
||||||
|
|
||||||
|
<pre class="snippet">
|
||||||
|
var list = [0, 1, 2, 3, 4]
|
||||||
|
System.print(list.indexOf(3)) //> 3
|
||||||
|
System.print(list.indexOf(20)) //> -1
|
||||||
|
</pre>
|
||||||
|
|
||||||
### **insert**(index, item)
|
### **insert**(index, item)
|
||||||
|
|
||||||
Inserts the `item` at `index` in the list.
|
Inserts the `item` at `index` in the list.
|
||||||
|
|||||||
@ -394,6 +394,12 @@ DEF_PRIMITIVE(list_removeAt)
|
|||||||
RETURN_VAL(wrenListRemoveAt(vm, list, index));
|
RETURN_VAL(wrenListRemoveAt(vm, list, index));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEF_PRIMITIVE(list_indexOf)
|
||||||
|
{
|
||||||
|
ObjList* list = AS_LIST(args[0]);
|
||||||
|
RETURN_NUM(wrenListIndexOf(vm, list, args[1]));
|
||||||
|
}
|
||||||
|
|
||||||
DEF_PRIMITIVE(list_subscript)
|
DEF_PRIMITIVE(list_subscript)
|
||||||
{
|
{
|
||||||
ObjList* list = AS_LIST(args[0]);
|
ObjList* list = AS_LIST(args[0]);
|
||||||
@ -1360,6 +1366,7 @@ void wrenInitializeCore(WrenVM* vm)
|
|||||||
PRIMITIVE(vm->listClass, "iterate(_)", list_iterate);
|
PRIMITIVE(vm->listClass, "iterate(_)", list_iterate);
|
||||||
PRIMITIVE(vm->listClass, "iteratorValue(_)", list_iteratorValue);
|
PRIMITIVE(vm->listClass, "iteratorValue(_)", list_iteratorValue);
|
||||||
PRIMITIVE(vm->listClass, "removeAt(_)", list_removeAt);
|
PRIMITIVE(vm->listClass, "removeAt(_)", list_removeAt);
|
||||||
|
PRIMITIVE(vm->listClass, "indexOf(_)", list_indexOf);
|
||||||
|
|
||||||
vm->mapClass = AS_CLASS(wrenFindVariable(vm, coreModule, "Map"));
|
vm->mapClass = AS_CLASS(wrenFindVariable(vm, coreModule, "Map"));
|
||||||
PRIMITIVE(vm->mapClass->obj.classObj, "new()", map_new);
|
PRIMITIVE(vm->mapClass->obj.classObj, "new()", map_new);
|
||||||
|
|||||||
@ -319,6 +319,19 @@ void wrenListInsert(WrenVM* vm, ObjList* list, Value value, uint32_t index)
|
|||||||
list->elements.data[index] = value;
|
list->elements.data[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int wrenListIndexOf(WrenVM* vm, ObjList* list, Value value)
|
||||||
|
{
|
||||||
|
int count = list->elements.count;
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
Value item = list->elements.data[i];
|
||||||
|
if(wrenValuesEqual(item, value)) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
Value wrenListRemoveAt(WrenVM* vm, ObjList* list, uint32_t index)
|
Value wrenListRemoveAt(WrenVM* vm, ObjList* list, uint32_t index)
|
||||||
{
|
{
|
||||||
Value removed = list->elements.data[index];
|
Value removed = list->elements.data[index];
|
||||||
|
|||||||
@ -680,6 +680,9 @@ void wrenListInsert(WrenVM* vm, ObjList* list, Value value, uint32_t index);
|
|||||||
// Removes and returns the item at [index] from [list].
|
// Removes and returns the item at [index] from [list].
|
||||||
Value wrenListRemoveAt(WrenVM* vm, ObjList* list, uint32_t index);
|
Value wrenListRemoveAt(WrenVM* vm, ObjList* list, uint32_t index);
|
||||||
|
|
||||||
|
// Searches for [value] in [list], returns the index or -1 if not found.
|
||||||
|
int wrenListIndexOf(WrenVM* vm, ObjList* list, Value value);
|
||||||
|
|
||||||
// Creates a new empty map.
|
// Creates a new empty map.
|
||||||
ObjMap* wrenNewMap(WrenVM* vm);
|
ObjMap* wrenNewMap(WrenVM* vm);
|
||||||
|
|
||||||
|
|||||||
8
test/core/list/index_of.wren
Normal file
8
test/core/list/index_of.wren
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
var list = [0, 1, 2, 3, 4]
|
||||||
|
System.print(list.indexOf(4)) // expect: 4
|
||||||
|
System.print(list.indexOf(2)) // expect: 2
|
||||||
|
System.print(list.indexOf(3)) // expect: 3
|
||||||
|
System.print(list.indexOf(0)) // expect: 0
|
||||||
|
System.print(list.indexOf(100)) // expect: -1
|
||||||
|
System.print(list.indexOf(-1)) // expect: -1
|
||||||
Reference in New Issue
Block a user