diff --git a/docs/docs/collections/sets.md b/docs/docs/collections/sets.md index 46d3967d..ee593b54 100644 --- a/docs/docs/collections/sets.md +++ b/docs/docs/collections/sets.md @@ -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()` diff --git a/src/vm/datatypes/sets.c b/src/vm/datatypes/sets.c index f898a21f..0b3a3f08 100644 --- a/src/vm/datatypes/sets.c +++ b/src/vm/datatypes/sets.c @@ -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); @@ -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); diff --git a/tests/sets/import.du b/tests/sets/import.du index 523e86d5..d936e64e 100644 --- a/tests/sets/import.du +++ b/tests/sets/import.du @@ -14,3 +14,4 @@ import "len.du"; import "toString.du"; import "toBool.du"; import "containsAll.du"; +import "values.du"; \ No newline at end of file diff --git a/tests/sets/values.du b/tests/sets/values.du new file mode 100644 index 00000000..a8e771b7 --- /dev/null +++ b/tests/sets/values.du @@ -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();