Skip to content

Commit

Permalink
List; add indexOf(value)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruby0x1 committed Dec 3, 2020
1 parent 3d5e68f commit 6200987
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/site/modules/core/list.markdown
Expand Up @@ -32,6 +32,16 @@ Removes all elements from 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)

Inserts the `item` at `index` in the list.
Expand Down
7 changes: 7 additions & 0 deletions src/vm/wren_core.c
Expand Up @@ -394,6 +394,12 @@ DEF_PRIMITIVE(list_removeAt)
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)
{
ObjList* list = AS_LIST(args[0]);
Expand Down Expand Up @@ -1360,6 +1366,7 @@ void wrenInitializeCore(WrenVM* vm)
PRIMITIVE(vm->listClass, "iterate(_)", list_iterate);
PRIMITIVE(vm->listClass, "iteratorValue(_)", list_iteratorValue);
PRIMITIVE(vm->listClass, "removeAt(_)", list_removeAt);
PRIMITIVE(vm->listClass, "indexOf(_)", list_indexOf);

vm->mapClass = AS_CLASS(wrenFindVariable(vm, coreModule, "Map"));
PRIMITIVE(vm->mapClass->obj.classObj, "new()", map_new);
Expand Down
13 changes: 13 additions & 0 deletions src/vm/wren_value.c
Expand Up @@ -319,6 +319,19 @@ void wrenListInsert(WrenVM* vm, ObjList* list, Value value, uint32_t index)
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 removed = list->elements.data[index];
Expand Down
3 changes: 3 additions & 0 deletions src/vm/wren_value.h
Expand Up @@ -680,6 +680,9 @@ void wrenListInsert(WrenVM* vm, ObjList* list, Value value, uint32_t index);
// Removes and returns the item at [index] from [list].
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.
ObjMap* wrenNewMap(WrenVM* vm);

Expand Down
8 changes: 8 additions & 0 deletions test/core/list/index_of.wren
@@ -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

0 comments on commit 6200987

Please sign in to comment.