| 1 | {{{ |
| 2 | #!cobra |
| 3 | |
| 4 | |
| 5 | interface ISet |
| 6 | |
| 7 | def addRange(items as IEnumerable<of T>) |
| 8 | |
| 9 | def clone |
| 10 | |
| 11 | def difference(s as ISet<of T>) |
| 12 | |
| 13 | def intersection(s as ISet<of T>) |
| 14 | |
| 15 | def isSubsetOf(s as ISet<of T>) |
| 16 | |
| 17 | def isSupersetOf(s as ISet<of T>) |
| 18 | |
| 19 | def symmetricDifference(s as ISet<of T>) |
| 20 | |
| 21 | def toList |
| 22 | |
| 23 | def union(s as ISet<of T>) |
| 24 | |
| 25 | class Set |
| 26 | """ |
| 27 | A Set holds a hashed, unordered collections of items. |
| 28 | Membership testing and set operations (intersection, union, etc.) are faster than with lists, but order is not preserved. |
| 29 | Set implements ICollection, so it has the usual collection operations such as `add`, `count` and `contains`. |
| 30 | It also has set specific operations including `union`, `intersection` and `isSubsetOf`. |
| 31 | |
| 32 | As with other collections, to create a copy of a set, create a new one passing the original to the initializer: |
| 33 | s = Set<of int>(s) |
| 34 | |
| 35 | TODO: |
| 36 | [-] List style items |
| 37 | [ ] Can the methods use .getType instead of hardcoding Set? Then subclasses would get created for example .intersection |
| 38 | [ ] could use an indexer: s[key]. Does it return bool or T? like NSSet? |
| 39 | [ ] Test without args: s = Set(s) (test in C# too) |
| 40 | [ ] yielded versions of Set operators? |
| 41 | [ ] unit test CobraCore.toTechString() results |
| 42 | [ ] ReadOnlySet? |
| 43 | [ ] Move some method implementations up to ISet extension |
| 44 | """ |
| 45 | |
| 46 | |
| 47 | shared |
| 48 | |
| 49 | const defaultCapacity = 8 (as int) |
| 50 | |
| 51 | |
| 52 | |
| 53 | var _data as Dictionary<of T, T> |
| 54 | |
| 55 | |
| 56 | cue init |
| 57 | |
| 58 | cue init(comparer as IEqualityComparer<of T>) |
| 59 | |
| 60 | cue init(items as IEnumerable<of T>) |
| 61 | |
| 62 | cue init(capacity as int) |
| 63 | |
| 64 | cue init(items as IEnumerable<of T>?, comparer as IEqualityComparer<of T>?) |
| 65 | |
| 66 | cue init(capacity as int, items as IEnumerable<of T>?, comparer as IEqualityComparer<of T>?) |
| 67 | |
| 68 | |
| 69 | get count as int |
| 70 | |
| 71 | get countOfISet as int |
| 72 | |
| 73 | get isReadOnly as bool |
| 74 | |
| 75 | |
| 76 | def add(item as T) |
| 77 | |
| 78 | def addRange(items as ISet<of T>) |
| 79 | |
| 80 | def addRange(items as IEnumerable<of T>) |
| 81 | |
| 82 | def clear |
| 83 | |
| 84 | def clone |
| 85 | """ |
| 86 | Returns a new set with the contents of this set. |
| 87 | Does not clone the contents themselves. |
| 88 | This is sometimes known as a "shallow copy". |
| 89 | """ |
| 90 | |
| 91 | def contains(item as Object) |
| 92 | |
| 93 | def contains(item as T) |
| 94 | |
| 95 | def copyTo(array as T[], arrayIndex as int) |
| 96 | |
| 97 | def difference(s as ISet<of T>) |
| 98 | """ |
| 99 | Returns a new set with elements in this set that are not in `s`. |
| 100 | """ |
| 101 | |
| 102 | def equals(other as Object?) |
| 103 | |
| 104 | def getEnumerator |
| 105 | |
| 106 | def getEnumerator |
| 107 | |
| 108 | def getHashCode |
| 109 | |
| 110 | def intersection(s as ISet<of T>) |
| 111 | """ |
| 112 | Returns a new set containing only the elements that are in this set and `s`. (AND) |
| 113 | """ |
| 114 | |
| 115 | def isSubsetOf(s as ISet<of T>) |
| 116 | |
| 117 | def isSupersetOf(s as ISet<of T>) |
| 118 | |
| 119 | def remove(item as T) |
| 120 | |
| 121 | def symmetricDifference(s as ISet<of T>) |
| 122 | """ |
| 123 | Returns a new set with elements in either s or t but not both. (XOR) |
| 124 | """ |
| 125 | |
| 126 | def toList |
| 127 | |
| 128 | def union(s as ISet<of T>) |
| 129 | """ |
| 130 | Returns a new set containing the elements of that are in this set or `s`. (OR) |
| 131 | """ |
| 132 | |
| 133 | interface ISet |
| 134 | """ |
| 135 | This interface is to facilitate Set<of>.equals when the generic types are different (which |
| 136 | does not necessarily restrict the two sets from having the same contents). |
| 137 | |
| 138 | Use ISet<of T> instead. |
| 139 | """ |
| 140 | |
| 141 | |
| 142 | get countOfISet as int |
| 143 | |
| 144 | |
| 145 | def contains(item as Object) |
| 146 | |
| 147 | }}} |
| 148 | Browse source - [source:cobra/trunk/Source/Cobra.Core/Set.cobra Set.cobra] |