Function pickRandom
Return an array of unique elements from the input set in a randomly distributed order.
Parameters
Name | Description |
---|---|
T | the element type of the set |
input | the input set |
count | the number of elements to return, if set to zero then input.length is implied |
Returns
a randomly distributed array of $count elements
Example
auto set = Set!uint .from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
auto randoms = set .pickRandom(5);
sort(randoms);
assert(randoms .uniq .count == 5);
auto full = set .pickRandom();
sort(full);
assert(full .uniq .count == set .length);
Example
serialization test for Set!int
auto old_set = Set!uint .from([2, 4, 6, 8]);
auto bytes = old_set .serializeFull();
auto new_set = deserializeFull!(Set!uint)(bytes);
assert(new_set .length == old_set .length);
old_set .each!(value => assert(value in new_set));
Example
serialization test for Set!string
auto old_set = Set!string .from(["foo", "bar", "agora"]);
auto bytes = old_set .serializeFull();
auto new_set = deserializeFull!(Set!string)(bytes);
assert(new_set .length == old_set .length);
old_set .each!(value => assert(value in new_set));
Example
toString and fromString test for Set!int
auto old_set = Set!uint .from([1, 3, 5, 7, 9]);
auto str = old_set .toString();
assert(str == `["7", "5", "3", "1", "9"]`);
auto new_set = Set!uint .fromString(str);
assert(new_set .length == old_set .length);
old_set .each!(value => assert(value in new_set));
Example
toString and fromString test for Set!int when empty
Set!uint empty_set;
assert(empty_set .toString() == `[]`);
auto new_set = Set!uint .fromString(`[]`);
assert(new_set .length == 0);
Example
toString and fromString test for Set!string
auto old_set = Set!string .from(["hello", "world"]);
auto str = old_set .toString();
assert(str == `["world", "hello"]`);
auto new_set = Set!string .fromString(str);
assert(new_set .length == old_set .length);
old_set .each!(value => assert(value in new_set));
Example
uint[] arr = [1, 2, 3];
arr .dropIndex(1);
assert(arr == [1, 3]);