| 1 | """ |
| 2 | Bag - multivalue collection, 2, 3 and 4 items, each item fully and separately typed |
| 3 | """ |
| 4 | |
| 5 | namespace Cobra.Core |
| 6 | |
| 7 | class Bag<of TA, TB> inherits Pair<of TA, TB> |
| 8 | implements System.Collections.IEnumerable, IComparable<of Bag<of TA, TB>> |
| 9 | """ Bag<of,> 2 item bag - Thin wrapper around Pair<T1, T2> """ |
| 10 | |
| 11 | cue init(a as TA, b as TB) |
| 12 | base.init(a, b) |
| 13 | |
| 14 | def toString as String is override |
| 15 | sa = CobraCore.toTechString(.a) |
| 16 | sb = CobraCore.toTechString(.b) |
| 17 | return 'Bag([sa], [sb])' |
| 18 | |
| 19 | def equals(obj as Object?) as bool is override |
| 20 | test |
| 21 | assert Pair<of String, String>('a', 'b') == Pair<of String, String>('a', 'b') |
| 22 | assert Pair<of String, String>('a', 'b') <> Pair<of String, String>('a', 'c') |
| 23 | body |
| 24 | if obj is nil, return false |
| 25 | if not (obj to? Bag<of TA, TB>), return false # not this Type |
| 26 | if obj is this, return true |
| 27 | if obj inherits Pair<of TA, TB> |
| 28 | return obj.a == .a and obj.b == .b |
| 29 | return false |
| 30 | |
| 31 | def equals(b as Bag<of TA, TB>) as bool |
| 32 | # suppress through calls fm superclass so equivalent subset (type and values same) not map equal |
| 33 | # superclasses must do own independent equality checking not using base.equals |
| 34 | if b.getType <> Bag<of TA, TB>, return false |
| 35 | return base.equals(b to Pair<of TA, TB>) |
| 36 | |
| 37 | def compareTo(b as Bag<of TA, TB>) as int |
| 38 | test |
| 39 | assert Bag<of String, int>('a', 3) < Bag<of String, int>('b', 4) |
| 40 | assert Bag<of String, int>('a', 3) < Bag<of String, int>('a', 4) |
| 41 | assert Bag<of String, int>('a', 3) <= Bag<of String, int>('a', 3) |
| 42 | assert Bag<of int, int>(2, 3) > Bag<of int, int>(2, 2) |
| 43 | assert Bag<of int, int>(2, 3) >= Bag<of int, int>(2, 3) |
| 44 | assert Bag<of String, int>('a', 3).compareTo(Bag<of String, int, char>('a', 3, c'b')) == 0 |
| 45 | # on-hold. to-do |
| 46 | # assert Bag<of String?, String?, String?>(nil, 'a', 'a') < Bag<of String?, String?, String?>(nil, 'b', 'a') |
| 47 | # assert Bag<of String?, String?, String?>(nil, 'a', 'a') < Bag<of String?, String?, String?>(nil, 'a', 'b') |
| 48 | # assert Bag<of int?, int?, int?>(nil, nil, nil) < Bag<of int?, int?, int?>(1, 1, 1) |
| 49 | body |
| 50 | return base.compareTo(b) |
| 51 | |
| 52 | |
| 53 | |
| 54 | # |
| 55 | # 3-Bag built on 2-Bag |
| 56 | # |
| 57 | class Bag<of TA, TB, TC> inherits Bag<of TA, TB> |
| 58 | implements System.Collections.IEnumerable, IComparable<of Bag<of TA, TB, TC>> |
| 59 | """ Bag<of,,> - 3 item Bag - values separately typed""" |
| 60 | |
| 61 | test |
| 62 | b = Bag<of int, int, int>(1, 2, 3) |
| 63 | assert b.a == 1 and b.b == 2 and b.c == 3 |
| 64 | assert b[0] == 1 and b[1] == 2 and b[2] == 3 |
| 65 | assert b.toString == 'Bag(1, 2, 3)' |
| 66 | expect IndexOutOfRangeException, assert b[3] == 3 |
| 67 | |
| 68 | b1 = Bag<of int, String, int>(1, 'a', 1) |
| 69 | assert b1.a == 1 and b1.b == 'a' and b1.c == 1 |
| 70 | assert b1[0] == 1 and b1[1] == 'a' and b1[2] == 1 |
| 71 | assert b1.toString == "Bag(1, 'a', 1)" |
| 72 | expect IndexOutOfRangeException, assert b1[3] == 3 |
| 73 | |
| 74 | |
| 75 | cue init(a as TA, b as TB, c as TC ) |
| 76 | base.init(a, b) |
| 77 | _c = c |
| 78 | |
| 79 | get c from var as TC |
| 80 | |
| 81 | get [i as int] as dynamic? is new |
| 82 | if i == 0, return .a |
| 83 | if i == 1, return .b |
| 84 | if i == 2, return _c |
| 85 | throw IndexOutOfRangeException('Index [i] is out of range for a 3 bag (0-2).') |
| 86 | |
| 87 | def toString as String is override |
| 88 | sa = CobraCore.toTechString(.a) |
| 89 | sb = CobraCore.toTechString(.b) |
| 90 | sc = CobraCore.toTechString(.c) |
| 91 | return 'Bag([sa], [sb], [sc])' |
| 92 | |
| 93 | def equals(other as Bag<of TA, TB, TC>) as bool |
| 94 | test |
| 95 | assert Bag<of String, int, char>('a', 99, c'b').equals(Bag<of String,int, char>('a', 99, c'b')) |
| 96 | assert not Bag<of String, int, char>('a', 99, c'b').equals(Bag<of String, int, char>('a', 98, c'b')) |
| 97 | assert not Bag<of String, int, char>('a', 99, c'b').equals(Bag<of String, int, char>('a', 99, c'c')) |
| 98 | body |
| 99 | if other.getType <> Bag<of TA, TB, TC>, return false # disallow superclasses |
| 100 | return other.a ==.a and other.b ==.b and other.c ==.c |
| 101 | |
| 102 | def equals(obj as Object?) as bool is override |
| 103 | test |
| 104 | assert Bag<of String, String, int>('a', 'b', 1) == Bag<of String, String, int>('a', 'b', 1) |
| 105 | assert Bag<of String, String, int>('a', 'b', 1) <> Bag<of String, String, int>('a', 'c', 1) |
| 106 | assert Bag<of String, String, int>('a', 'b', 1) <> Bag<of String, String, int>('a', 'b', 2) |
| 107 | assert Bag<of String, String>('a', 'c') <> Bag<of String, String, int>('a', 'b', 1) |
| 108 | body |
| 109 | if obj inherits Bag<of TA, TB, TC> |
| 110 | return .equals(obj) |
| 111 | return false |
| 112 | |
| 113 | # block equals against base class instances |
| 114 | def equals(p as Pair<of TA, TB>) as bool is override |
| 115 | test |
| 116 | assert Bag<of String, String, int>('a', 'b', 1) <> Pair<of String, String>('a', 'b') |
| 117 | assert not Bag<of String, String, int>('a', 'b', 1) == Pair<of String, String>('a', 'b') |
| 118 | # this fail - probably testing into Pair rather than here |
| 119 | #assert Pair<of String, String>('a', 'b') <> Bag<of String, String, int>('a', 'b', 1) |
| 120 | body |
| 121 | return false |
| 122 | |
| 123 | # block equals against base class instances |
| 124 | def equals(p as Bag<of TA, TB>) as bool is override |
| 125 | test |
| 126 | assert Bag<of String, String, int>('a', 'b', 1) <> Bag<of String, String>('a', 'b') |
| 127 | assert not Bag<of String, String, int>('a', 'b', 1) == Bag<of String, String>('a', 'b') |
| 128 | assert Bag<of String, String>('a', 'b') <> Bag<of String, String, int>('a', 'b', 1) |
| 129 | body |
| 130 | return false |
| 131 | |
| 132 | def compareTo(b as Bag<of TA, TB, TC>) as int |
| 133 | test |
| 134 | assert Bag<of String, int, char>('a', 3, c'b') < Bag<of String, int, char>('b', 4, c'b') |
| 135 | assert Bag<of String, int, char>('a', 3, c'b') < Bag<of String, int, char>('a', 4, c'b') |
| 136 | assert Bag<of String, int, char>('a', 3, c'b') < Bag<of String, int, char>('a', 3, c'c') |
| 137 | assert Bag<of int, int, int>(2, 3, 4) > Bag<of int, int, int>(2, 2, 4) |
| 138 | assert Bag<of int, int, int>(2, 3, 4) > Bag<of int, int, int>(2, 3, 3) |
| 139 | assert Bag<of String, int, char>('a', 3, c'b').compareTo(Bag<of String, int, char>('a', 3, c'b')) == 0 |
| 140 | # on-hold. to-do |
| 141 | # assert Bag<of String?, String?, String?>(nil, 'a', 'a') < Bag<of String?, String?, String?>(nil, 'b', 'a') |
| 142 | # assert Bag<of String?, String?, String?>(nil, 'a', 'a') < Bag<of String?, String?, String?>(nil, 'a', 'b') |
| 143 | # assert Bag<of int?, int?, int?>(nil, nil, nil) < Bag<of int?, int?, int?>(1, 1, 1) |
| 144 | body |
| 145 | diff = base.compareTo(b) |
| 146 | if diff == 0, diff = _compare(.c, b.c) |
| 147 | return diff |
| 148 | |
| 149 | def getHashCode as int is override |
| 150 | test |
| 151 | assert Bag<of int, String, int>(1, 'one', 1).getHashCode == Bag<of int, String, int>(1, 'one', 1).getHashCode |
| 152 | assert Bag<of int, int, int>(2, 2, 2).getHashCode == Bag<of int, int, int>(2, 2, 2).getHashCode |
| 153 | assert Bag<of int, String, int>(1, 'one', 1).getHashCode <> Bag<of int, int, int>(2, 2, 2).getHashCode |
| 154 | assert Bag<of int, int, int>(2, 2, 2).getHashCode <> Bag<of int, int, int>(2, 2, 1).getHashCode |
| 155 | d = { |
| 156 | Bag<of int, String, int>(1, 'one', 1): 'one', |
| 157 | Bag<of int, int, int>(2, 2, 2): 'two', |
| 158 | # to-do Bag<of dynamic, dynamic, dynamic>(nil, nil, nil): 'nil', |
| 159 | } |
| 160 | |
| 161 | assert d[Bag<of int, String, int>(1, 'one', 1)] == 'one' |
| 162 | assert d[Bag<of int, int, int>(2, 2, 2)] == 'two' |
| 163 | # to-do assert d[Bag<of dynamic, dynamic, dynamic>(nil, nil, nil)] == 'nil' |
| 164 | body |
| 165 | return HashCodeUtils.combine(base.getHashCode, if(.c is nil, 0, .c.getHashCode)) |
| 166 | |
| 167 | def getEnumerator as System.Collections.IEnumerator is override |
| 168 | test |
| 169 | assert (for s in Bag<of String, String, String>('a', 'b', 'c') get s) == ['a', 'b', 'c'] |
| 170 | assert (for s in Bag<of String, String, int>('a', 'b', 99) get s) == ['a', 'b', 99] |
| 171 | body |
| 172 | yield .a |
| 173 | yield .b |
| 174 | yield .c |
| 175 | |
| 176 | # |
| 177 | # 4-Bag built on 3-bag |
| 178 | # |
| 179 | class Bag<of TA, TB, TC, TD> inherits Bag<of TA, TB, TC> |
| 180 | implements System.Collections.IEnumerable, IComparable<of Bag<of TA, TB, TC, TD>> |
| 181 | """ Bag<of,,,> - 4 item Bag - values separately typed""" |
| 182 | |
| 183 | test |
| 184 | b = Bag<of int, int, int, int>(1, 2, 3, 4) |
| 185 | assert b.a == 1 and b.b == 2 and b.c == 3 and b.d ==4 |
| 186 | assert b[0] == 1 and b[1] == 2 and b[2] == 3 and b[3] ==4 |
| 187 | assert b.toString == 'Bag(1, 2, 3, 4)' |
| 188 | expect IndexOutOfRangeException, assert b[4] == 3 |
| 189 | |
| 190 | b1 = Bag<of int, String, int, String>(1, 'a', 1, 'a') |
| 191 | assert b1.a == 1 and b1.b == 'a' and b1.c == 1 and b1.d == 'a' |
| 192 | assert b1[0] == 1 and b1[1] == 'a' and b1[2] == 1 and b1[3] == 'a' |
| 193 | assert b1.toString == "Bag(1, 'a', 1, 'a')" |
| 194 | expect IndexOutOfRangeException, assert b1[4] == 3 |
| 195 | |
| 196 | |
| 197 | cue init(a as TA, b as TB, c as TC, d as TD) |
| 198 | base.init(a, b, c) |
| 199 | _d = d |
| 200 | |
| 201 | get d from var as TD |
| 202 | |
| 203 | get [i as int] as dynamic? is new |
| 204 | if i == 0, return .a |
| 205 | if i == 1, return .b |
| 206 | if i == 2, return .c |
| 207 | if i == 3, return _d |
| 208 | throw IndexOutOfRangeException('Index [i] is out of range for a 4 bag (0-3).') |
| 209 | |
| 210 | def toString as String is override |
| 211 | sa = CobraCore.toTechString(.a) |
| 212 | sb = CobraCore.toTechString(.b) |
| 213 | sc = CobraCore.toTechString(.c) |
| 214 | sd = CobraCore.toTechString(.d) |
| 215 | return 'Bag([sa], [sb], [sc], [sd])' |
| 216 | |
| 217 | def equals(other as Bag<of TA, TB, TC, TD>) as bool |
| 218 | test |
| 219 | assert Bag<of String, int, char, int>('a', 99, c'b', 1).equals(Bag<of String,int, char, int>('a', 99, c'b', 1)) |
| 220 | assert not Bag<of String, int, char, int>('a', 99, c'b', 1).equals(Bag<of String, int, char, int>('a', 98, c'b', 1)) |
| 221 | assert not Bag<of String, int, char, int>('a', 99, c'b', 1).equals(Bag<of String, int, char, int>('a', 99, c'c', 1)) |
| 222 | assert not Bag<of String, int, char, int>('a', 99, c'b', 1).equals(Bag<of String, int, char, int>('a', 99, c'b', 2)) |
| 223 | body |
| 224 | if other.getType <> Bag<of TA, TB, TC, TD>, return false # disallow equality on superclasses |
| 225 | return other.a ==.a and other.b ==.b and other.c ==.c and other.d == .d |
| 226 | |
| 227 | def equals(obj as Object?) as bool is override |
| 228 | test |
| 229 | assert Bag<of String, String, int, int>('a', 'b', 1, 1) == Bag<of String, String, int, int>('a', 'b', 1, 1) |
| 230 | assert Bag<of String, String, int, int>('a', 'b', 1, 1) <> Bag<of String, String, int, int>('a', 'c', 1, 1) |
| 231 | assert Bag<of String, String, int, int>('a', 'b', 1, 1) <> Bag<of String, String, int, int>('a', 'b', 2, 1) |
| 232 | assert Bag<of String, String, int, int>('a', 'b', 1, 1) <> Bag<of String, String, int, int>('a', 'b', 1, 2) |
| 233 | assert Bag<of String, String, int>('a', 'b', 1) <> Bag<of String, String, int, int>('a', 'b', 1, 1) |
| 234 | body |
| 235 | if obj inherits Bag<of TA, TB, TC, TD> |
| 236 | return .equals(obj) |
| 237 | return false |
| 238 | |
| 239 | # block equals against base class instances |
| 240 | def equals(p as Bag<of TA, TB, TC>) as bool is override |
| 241 | test |
| 242 | assert Bag<of String, String, int, int>('a', 'b', 1, 1) <> Bag<of String, String, int>('a', 'b', 1) |
| 243 | assert not Bag<of String, String, int, int>('a', 'b', 1, 1) == Bag<of String, String, int>('a', 'b', 1) |
| 244 | assert Bag<of String, String, int>('a', 'b', 1) <> Bag<of String, String, int, int>('a', 'b', 1, 1) |
| 245 | body |
| 246 | return false |
| 247 | |
| 248 | def compareTo(b as Bag<of TA, TB, TC, TD>) as int |
| 249 | test |
| 250 | assert Bag<of String, int, char, int>('a', 3, c'b', 1) < Bag<of String, int, char, int>('b', 4, c'b', 1) |
| 251 | assert Bag<of String, int, char, int>('a', 3, c'b', 1) < Bag<of String, int, char, int>('a', 3, c'c', 1) |
| 252 | assert Bag<of String, int, char, int>('a', 3, c'b', 1) < Bag<of String, int, char, int>('a', 3, c'b', 2) |
| 253 | assert Bag<of int, int, int, int>(2, 3, 4, 5) > Bag<of int, int, int, int>(2, 2, 4, 5) |
| 254 | assert Bag<of int, int, int, int>(2, 3, 4, 5) > Bag<of int, int, int, int>(2, 3, 3, 5) |
| 255 | assert Bag<of int, int, int, int>(2, 3, 4, 5) > Bag<of int, int, int, int>(2, 3, 4, 4) |
| 256 | assert Bag<of String, int, char, int>('a', 3, c'b', 1).compareTo(Bag<of String, int, char, int>('a', 3, c'b', 1)) == 0 |
| 257 | assert Bag<of int, int, int, int>(2, 3, 4, 5).compareTo(Bag<of int, int, int, int>(2, 3, 4, 4)) > 0 |
| 258 | assert Bag<of int, int, int, int>(2, 3, 4, 5).compareTo(Bag<of int, int, int, int>(2, 3, 4, 6)) <0 |
| 259 | # on-hold. to-do |
| 260 | # assert Bag<of String?, String?, String?>(nil, 'a', 'a') < Bag<of String?, String?, String?>(nil, 'b', 'a') |
| 261 | # assert Bag<of String?, String?, String?>(nil, 'a', 'a') < Bag<of String?, String?, String?>(nil, 'a', 'b') |
| 262 | # assert Bag<of int?, int?, int?>(nil, nil, nil) < Bag<of int?, int?, int?>(1, 1, 1) |
| 263 | body |
| 264 | diff = base.compareTo(b) |
| 265 | if diff == 0, diff = _compare(.d, b.d) |
| 266 | return diff |
| 267 | |
| 268 | def getHashCode as int is override |
| 269 | test |
| 270 | assert Bag<of int, String, int, int>(1, 'one', 1, 1).getHashCode == Bag<of int, String, int, int>(1, 'one', 1, 1).getHashCode |
| 271 | assert Bag<of int, int, int, int>(2, 2, 2, 2).getHashCode == Bag<of int, int, int, int>(2, 2, 2, 2).getHashCode |
| 272 | assert Bag<of int, String, int, int>(1, 'one', 1, 1).getHashCode <> Bag<of int, int, int, int>(2, 2, 2, 2).getHashCode |
| 273 | assert Bag<of int, int, int, int>(2, 2, 2, 2).getHashCode <> Bag<of int, int, int, int>(2, 2, 2, 1).getHashCode |
| 274 | d = { |
| 275 | Bag<of int, String, int, int>(1, 'one', 1, 1): 'one', |
| 276 | Bag<of int, int, int, int>(2, 2, 2, 2): 'two', |
| 277 | # to-do Bag<of dynamic, dynamic, dynamic>(nil, nil, nil): 'nil', |
| 278 | } |
| 279 | |
| 280 | assert d[Bag<of int, String, int, int>(1, 'one', 1, 1)] == 'one' |
| 281 | assert d[Bag<of int, int, int, int>(2, 2, 2, 2)] == 'two' |
| 282 | # to-do assert d[Bag<of dynamic, dynamic, dynamic>(nil, nil, nil)] == 'nil' |
| 283 | body |
| 284 | return HashCodeUtils.combine(base.getHashCode, if(.d is nil, 0, .d.getHashCode)) |
| 285 | |
| 286 | def getEnumerator as System.Collections.IEnumerator is override |
| 287 | test |
| 288 | assert (for s in Bag<of String, String, String, String>('a', 'b', 'c', 'd') get s) == ['a', 'b', 'c', 'd'] |
| 289 | assert (for s in Bag<of String, String, int, int>('a', 'b', 99, 1) get s) == ['a', 'b', 99, 1] |
| 290 | body |
| 291 | yield .a |
| 292 | yield .b |
| 293 | yield .c |
| 294 | yield .d |
| 295 | |
| 296 | |
| 297 | class BagExerciser |
| 298 | def main is shared |
| 299 | print 'Test' |
| 300 | b = Bag<of int, String, int>(1, 'xx', 1) |
| 301 | assert b.a == 1 and b.b=='xx' and b.c ==1 |
| 302 | assert b.a.typeOf == Int32 |
| 303 | assert b.b.typeOf == String |
| 304 | |
| 305 | d = Bag<of int, String, int, char>(1, 'xx', 1, c'a') |
| 306 | assert d.a == 1 and d.b=='xx' and d.c ==1 and d.d == c'a' |
| 307 | assert d.a.typeOf == Int32 |
| 308 | assert d.b.typeOf == String |
| 309 | assert d.d.typeOf == Char |
| 310 | |
| 311 | ty = [Int32, String, Int32, Char] |
| 312 | i=0 |
| 313 | for o in d |
| 314 | assert o == d[i] |
| 315 | assert o.getType == ty[i] |
| 316 | i += 1 |
| 317 | |
| 318 | #print b.toString |
| 319 | #print d.toString |