| 1 | namespace Cobra.Core |
| 2 | class DefaultDictionary<of TKey, TValue> inherits Dictionary<of TKey, TValue> |
| 3 | """ |
| 4 | A child class of dictionary. When retrieving a value from the dictionary, |
| 5 | if the key is not present, a value generated by defaultFactory will be returned. |
| 6 | If .willRecordDefaults is set to 'true' (the default value), then the returned |
| 7 | value will also be inserted into the dictionary. |
| 8 | """ |
| 9 | cue init(defaultFactory as Func<of TValue>) |
| 10 | base.init |
| 11 | _defaultFactory = defaultFactory |
| 12 | |
| 13 | cue init(dict as IDictionary<of TKey, TValue>, defaultFactory as Func<of TValue>) |
| 14 | base.init(dict) |
| 15 | _defaultFactory = defaultFactory |
| 16 | |
| 17 | cue init(comparer as IEqualityComparer<of TKey>, defaultFactory as Func<of TValue>) |
| 18 | base.init(comparer) |
| 19 | _defaultFactory = defaultFactory |
| 20 | |
| 21 | cue init(capacity as int, defaultFactory as Func<of TValue>) |
| 22 | base.init(capacity) |
| 23 | _defaultFactory = defaultFactory |
| 24 | |
| 25 | cue init(dict as IDictionary<of TKey, TValue>, |
| 26 | comparer as IEqualityComparer<of TKey>, |
| 27 | defaultFactory as Func<of TValue>) |
| 28 | base.init(dict, comparer) |
| 29 | _defaultFactory = defaultFactory |
| 30 | |
| 31 | cue init(capacity as int, comparer as IEqualityComparer<of TKey>, defaultFactory as Func<of TValue>) |
| 32 | base.init(capacity, comparer) |
| 33 | _defaultFactory = defaultFactory |
| 34 | |
| 35 | var _defaultFactory as Func<of TValue> |
| 36 | |
| 37 | var _willRecordDefaults = true |
| 38 | |
| 39 | pro defaultFactory from var |
| 40 | |
| 41 | pro willRecordDefaults from var |
| 42 | |
| 43 | pro [key as TKey] as TValue is new |
| 44 | get |
| 45 | val as TValue |
| 46 | if base.tryGetValue(key, out val), return val |
| 47 | val = _defaultFactory() |
| 48 | if .willRecordDefaults, base[key] = val |
| 49 | return val |
| 50 | set |
| 51 | base[key] = value |
| 52 | |
| 53 | def tryGetValue(key as TKey?, val as out TValue?) as bool is new |
| 54 | require key |
| 55 | val = this[key to !] |
| 56 | return true |
| 57 | |
| 58 | class TestDefaultDictionary |
| 59 | """ |
| 60 | Examples taken from |
| 61 | from http://docs.python.org/library/collections.html#collections.DefaultDictionary.default_factory |
| 62 | |
| 63 | The typing of Pair detracts from the example a little bit, but DefaultDictionary is useful |
| 64 | if you need to make a dictionary of counters or lists. |
| 65 | """ |
| 66 | |
| 67 | test countOccurances |
| 68 | #Count occurance of each letter in 'mississippi' |
| 69 | s = 'mississippi' |
| 70 | dd = DefaultDictionary<of char, int>(do = 0) |
| 71 | for k in s, dd[k] += 1 |
| 72 | |
| 73 | expected = [Pair<of char, int>(c'i', 4), |
| 74 | Pair<of char, int>(c'p', 2), |
| 75 | Pair<of char, int>(c's', 4), |
| 76 | Pair<of char, int>(c'm', 1)] |
| 77 | for key, val in dd |
| 78 | assert expected.contains(Pair<of char, int>(key, val)) |
| 79 | |
| 80 | test dictionaryOfLists |
| 81 | # make a dictionary of lists |
| 82 | lst = [Pair<of String, int>('yellow', 1), |
| 83 | Pair<of String, int>('blue', 2), |
| 84 | Pair<of String, int>('yellow', 3), |
| 85 | Pair<of String, int>('blue', 4), |
| 86 | Pair<of String, int>('red', 1)] |
| 87 | |
| 88 | dd = DefaultDictionary<of String, IList<of int>>(do = List<of int>()) |
| 89 | for k, v in lst, dd[k].add(v) |
| 90 | |
| 91 | expected = [ Pair<of String, List<of int>>('blue', [2, 4]), |
| 92 | Pair<of String, List<of int>>('red', [1]), |
| 93 | Pair<of String, List<of int>>('yellow', [1, 3])] |
| 94 | for key, val in dd |
| 95 | assert expected.contains(Pair<of String, List<of int>>(key, val)) |
| 96 | |
| 97 | test tryGetValue |
| 98 | dd = DefaultDictionary<of int, int>(do = 5) |
| 99 | ans as int |
| 100 | res = dd.tryGetValue(10, out ans) |
| 101 | assert res |
| 102 | assert ans == 5 |
| 103 | dd[6] = 4 |
| 104 | res = dd.tryGetValue(6, out ans) |
| 105 | assert res |
| 106 | assert ans == 4 |
| 107 | |
| 108 | test willRecordDefaults |
| 109 | factory as Func<of int> = do = 5 |
| 110 | dd = DefaultDictionary<of int, int>(factory, willRecordDefaults = false) |
| 111 | assert dd[6] == 5 |
| 112 | assert not dd.containsKey(6) |
| 113 | |
| 114 | |
| 115 | def main |
| 116 | pass |