| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.z; |
| 9 | |
| 10 | /** |
| 11 | * A set of integers represented as int values |
| 12 | */ |
| 13 | public interface IntSet { |
| 14 | |
| 15 | /** |
| 16 | * Create a copy of this IntSet that leaves the original unchanged. |
| 17 | * |
| 18 | * @return an IntSet containing the same integers. The result will not necessarily be the |
| 19 | * same class as the original. It will either be an immutable object, or a newly constructed |
| 20 | * object. |
| 21 | */ |
| 22 | |
| 23 | IntSet copy(); |
| 24 | |
| 25 | /** |
| 26 | * Create a copy of this IntSet that contains the same set of integers. |
| 27 | * |
| 28 | * @return an IntSet containing the same integers. The result will not necessarily be the |
| 29 | * same class as the original. It will always be a mutable object |
| 30 | */ |
| 31 | |
| 32 | IntSet mutableCopy(); |
| 33 | |
| 34 | /** |
| 35 | * Clear the contents of the IntSet (making it an empty set) |
| 36 | */ |
| 37 | void clear(); |
| 38 | |
| 39 | /** |
| 40 | * Get the number of integers in the set |
| 41 | * |
| 42 | * @return the size of the set |
| 43 | */ |
| 44 | |
| 45 | int size(); |
| 46 | |
| 47 | /** |
| 48 | * Determine if the set is empty |
| 49 | * |
| 50 | * @return true if the set is empty, false if not |
| 51 | */ |
| 52 | |
| 53 | boolean isEmpty(); |
| 54 | |
| 55 | /** |
| 56 | * Determine whether a particular integer is present in the set |
| 57 | * |
| 58 | * @param value the integer under test |
| 59 | * @return true if value is present in the set, false if not |
| 60 | */ |
| 61 | |
| 62 | boolean contains(int value); |
| 63 | |
| 64 | /** |
| 65 | * Remove an integer from the set |
| 66 | * |
| 67 | * @param value the integer to be removed |
| 68 | * @return true if the integer was present in the set, false if it was not present |
| 69 | */ |
| 70 | |
| 71 | boolean remove(int value); |
| 72 | |
| 73 | /** |
| 74 | * Add an integer to the set |
| 75 | * |
| 76 | * @param value the integer to be added |
| 77 | * @return true if the integer was added, false if it was already present |
| 78 | */ |
| 79 | |
| 80 | boolean add(int value); |
| 81 | |
| 82 | /** |
| 83 | * Get an iterator over the values |
| 84 | * |
| 85 | * @return an iterator over the integers in the set |
| 86 | */ |
| 87 | |
| 88 | IntIterator iterator(); |
| 89 | |
| 90 | /** |
| 91 | * Test if this set is a superset of another set |
| 92 | * |
| 93 | * @param other the other set |
| 94 | * @return true if every item in the other set is also in this set |
| 95 | */ |
| 96 | |
| 97 | default boolean containsAll(IntSet other) { |
| 98 | if (other == IntUniversalSet.getInstance() || (other instanceof IntComplementSet)) { |
| 99 | return false; |
| 100 | } |
| 101 | IntIterator it = other.iterator(); |
| 102 | while (it.hasNext()) { |
| 103 | if (!contains(it.next())) { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Form a new set that is the union of two IntSets. |
| 112 | * |
| 113 | * @param other the second set |
| 114 | * @return the union of the two sets |
| 115 | */ |
| 116 | |
| 117 | default IntSet union(IntSet other) { |
| 118 | if (other == IntUniversalSet.getInstance()) { |
| 119 | return other; |
| 120 | } |
| 121 | if (this.isEmpty()) { |
| 122 | return other.copy(); |
| 123 | } |
| 124 | if (other.isEmpty()) { |
| 125 | return this.copy(); |
| 126 | } |
| 127 | if (other instanceof IntComplementSet) { |
| 128 | return other.union(this); |
| 129 | } |
| 130 | IntHashSet n = new IntHashSet(this.size() + other.size()); |
| 131 | IntIterator it = iterator(); |
| 132 | while (it.hasNext()) { |
| 133 | n.add(it.next()); |
| 134 | } |
| 135 | it = other.iterator(); |
| 136 | while (it.hasNext()) { |
| 137 | n.add(it.next()); |
| 138 | } |
| 139 | return n; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Form a new set that is the intersection of two IntSets. |
| 144 | * |
| 145 | * @param other the second set |
| 146 | * @return the intersection of the two sets |
| 147 | */ |
| 148 | |
| 149 | default IntSet intersect(IntSet other) { |
| 150 | if (this.isEmpty() || other.isEmpty()) { |
| 151 | return IntEmptySet.getInstance(); |
| 152 | } |
| 153 | IntHashSet n = new IntHashSet(size()); |
| 154 | IntIterator it = iterator(); |
| 155 | while (it.hasNext()) { |
| 156 | int v = it.next(); |
| 157 | if (other.contains(v)) { |
| 158 | n.add(v); |
| 159 | } |
| 160 | } |
| 161 | return n; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Form a new set that is the difference of this set and another set. |
| 166 | * The result will either be an immutable object, or a newly constructed object. |
| 167 | * |
| 168 | * @param other the second set |
| 169 | * @return the intersection of the two sets |
| 170 | */ |
| 171 | |
| 172 | |
| 173 | default IntSet except(IntSet other) { |
| 174 | IntHashSet n = new IntHashSet(size()); |
| 175 | IntIterator it = iterator(); |
| 176 | while (it.hasNext()) { |
| 177 | int v = it.next(); |
| 178 | if (!other.contains(v)) { |
| 179 | n.add(v); |
| 180 | } |
| 181 | } |
| 182 | return n; |
| 183 | } |
| 184 | |
| 185 | } |