diff options
| author | Sophia Pearson <codergal89@gmail.com> | 2022-05-20 00:45:25 +0200 |
|---|---|---|
| committer | Sophia Pearson <codergal89@gmail.com> | 2022-05-20 18:56:04 +0200 |
| commit | 05d29ccce1898ed89c0b650c77242c2fa2805128 (patch) | |
| tree | e8ee3bcb570fa6f3d9d96273c2bf4d4c8618d08b /addons/gut/one_to_many.gd | |
| download | texty-05d29ccce1898ed89c0b650c77242c2fa2805128.tar.xz texty-05d29ccce1898ed89c0b650c77242c2fa2805128.zip | |
texty: initial commit
Diffstat (limited to 'addons/gut/one_to_many.gd')
| -rw-r--r-- | addons/gut/one_to_many.gd | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/addons/gut/one_to_many.gd b/addons/gut/one_to_many.gd new file mode 100644 index 0000000..6a0f818 --- /dev/null +++ b/addons/gut/one_to_many.gd @@ -0,0 +1,38 @@ +# ------------------------------------------------------------------------------ +# This datastructure represents a simple one-to-many relationship. It manages +# a dictionary of value/array pairs. It ignores duplicates of both the "one" +# and the "many". +# ------------------------------------------------------------------------------ +var _items = {} + +# return the size of _items or the size of an element in _items if "one" was +# specified. +func size(one=null): + var to_return = 0 + if(one == null): + to_return = _items.size() + elif(_items.has(one)): + to_return = _items[one].size() + return to_return + +# Add an element to "one" if it does not already exist +func add(one, many_item): + if(_items.has(one) and !_items[one].has(many_item)): + _items[one].append(many_item) + else: + _items[one] = [many_item] + +func clear(): + _items.clear() + +func has(one, many_item): + var to_return = false + if(_items.has(one)): + to_return = _items[one].has(many_item) + return to_return + +func to_s(): + var to_return = '' + for key in _items: + to_return += str(key, ": ", _items[key], "\n") + return to_return |
