Skip to content

Commit

Permalink
Merge pull request #729 from liz3/liz3/set-values
Browse files Browse the repository at this point in the history
feat: set.values()
  • Loading branch information
Jason2605 authored Jan 17, 2024
2 parents 89ced84 + 649f0f7 commit 325f342
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/docs/collections/sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ var mySet = set();
mySet.add("Dictu!");
```

### set.values() -> List

Returns a list of all of the values in the set.

```cs
var mySet = set("foo", "bar", 123);
const values = mySet.values(); // ["foo", "bar", 123]
```

### set.contains(value) -> Boolean

To check if a set contains a value use `.contains()`
Expand Down
20 changes: 20 additions & 0 deletions src/vm/datatypes/sets.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ static Value toStringSet(DictuVM *vm, int argCount, Value *args) {
return OBJ_VAL(string);
}

static Value valuesSet(DictuVM *vm, int argCount, Value *args) {
if (argCount != 0) {
runtimeError(vm, "values() takes no arguments (%d given)", argCount);
return EMPTY_VAL;
}
ObjSet *set = AS_SET(args[0]);
ObjList *list = newList(vm);
push(vm, OBJ_VAL(list));

for (int i = 0; i <= set->capacityMask; ++i) {
SetItem *item = &set->entries[i];
if (IS_EMPTY(item->value) || item->deleted)
continue;
writeValueArray(vm, &list->values, item->value);
}
pop(vm);
return OBJ_VAL(list);
}

static Value lenSet(DictuVM *vm, int argCount, Value *args) {
if (argCount != 0) {
runtimeError(vm, "len() takes no arguments (%d given)", argCount);
Expand Down Expand Up @@ -96,6 +115,7 @@ static Value containsAllSet(DictuVM *vm, int argCount, Value *args) {
void declareSetMethods(DictuVM *vm) {
defineNative(vm, &vm->setMethods, "toString", toStringSet);
defineNative(vm, &vm->setMethods, "len", lenSet);
defineNative(vm, &vm->setMethods, "values", valuesSet);
defineNative(vm, &vm->setMethods, "add", addSetItem);
defineNative(vm, &vm->setMethods, "remove", removeSetItem);
defineNative(vm, &vm->setMethods, "contains", containsSetItem);
Expand Down
1 change: 1 addition & 0 deletions tests/sets/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ import "len.du";
import "toString.du";
import "toBool.du";
import "containsAll.du";
import "values.du";
33 changes: 33 additions & 0 deletions tests/sets/values.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* values.du
*
* Testing the set.values() method
*
* .values() returns a list of all values in the set
*/
from UnitTest import UnitTest;

class TestSetValues < UnitTest {
testSetValues() {
const s = set();
s.add("dictu");
var values = s.values();
this.assertEquals(values.len(), 1);
this.assertEquals(values[0], "dictu");

s.add("!");
values = s.values();
this.assertTruthy(values.contains("dictu"));
this.assertTruthy(values.contains("!"));


s.add(22);
values = s.values();
this.assertTruthy(values.contains("dictu"));
this.assertTruthy(values.contains("!"));
this.assertTruthy(values.contains(22));

}
}

TestSetValues().run();

0 comments on commit 325f342

Please sign in to comment.