| 1 |
# ------------------------------------------------------------------------------ |
| 2 |
# This datastructure represents a simple one-to-many relationship. It manages |
| 3 |
# a dictionary of value/array pairs. It ignores duplicates of both the "one" |
| 4 |
# and the "many". |
| 5 |
# ------------------------------------------------------------------------------ |
| 6 |
var _items = {} |
| 7 |
|
| 8 |
# return the size of _items or the size of an element in _items if "one" was |
| 9 |
# specified. |
| 10 |
func size(one=null): |
| 11 |
var to_return = 0 |
| 12 |
if(one == null): |
| 13 |
to_return = _items.size() |
| 14 |
elif(_items.has(one)): |
| 15 |
to_return = _items[one].size() |
| 16 |
return to_return |
| 17 |
|
| 18 |
# Add an element to "one" if it does not already exist |
| 19 |
func add(one, many_item): |
| 20 |
if(_items.has(one) and !_items[one].has(many_item)): |
| 21 |
_items[one].append(many_item) |
| 22 |
else: |
| 23 |
_items[one] = [many_item] |
| 24 |
|
| 25 |
func clear(): |
| 26 |
_items.clear() |
| 27 |
|
| 28 |
func has(one, many_item): |
| 29 |
var to_return = false |
| 30 |
if(_items.has(one)): |
| 31 |
to_return = _items[one].has(many_item) |
| 32 |
return to_return |
| 33 |
|
| 34 |
func to_s(): |
| 35 |
var to_return = '' |
| 36 |
for key in _items: |
| 37 |
to_return += str(key, ": ", _items[key], "\n") |
| 38 |
return to_return |