| 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 | * Set of int values. This immutable implementation of IntSet represents a dense monotonic |
| 12 | * range of integers from A to B. |
| 13 | * |
| 14 | * @author Michael Kay |
| 15 | */ |
| 16 | public class IntBlockSet implements IntSet { |
| 17 | |
| 18 | private int startPoint; |
| 19 | private int endPoint; |
| 20 | |
| 21 | // Hashcode, evaluated lazily |
| 22 | private int hashCode = -1; |
| 23 | |
| 24 | /** |
| 25 | * Create an IntRangeSet given the start point and end point of the integer range. |
| 26 | * |
| 27 | * @param startPoint the start point of the integer range |
| 28 | * @param endPoint the end point of the integer range |
| 29 | * @throws IllegalArgumentException if the two arrays are different lengths. Other error conditions |
| 30 | * in the input are not currently detected. |
| 31 | */ |
| 32 | |
| 33 | public IntBlockSet(int startPoint, int endPoint) { |
| 34 | this.startPoint = startPoint; |
| 35 | this.endPoint = endPoint; |
| 36 | } |
| 37 | |
| 38 | public IntSet copy() { |
| 39 | return this; |
| 40 | } |
| 41 | |
| 42 | public IntSet mutableCopy() { |
| 43 | return new IntRangeSet(new int[]{startPoint}, new int[]{endPoint}); |
| 44 | } |
| 45 | |
| 46 | public int size() { |
| 47 | return endPoint - startPoint; |
| 48 | } |
| 49 | |
| 50 | public boolean isEmpty() { |
| 51 | return size() == 0; |
| 52 | } |
| 53 | |
| 54 | public boolean contains(int value) { |
| 55 | return value >= startPoint && value <= endPoint; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Remove an integer from the set |
| 60 | * @throws UnsupportedOperationException (always) |
| 61 | */ |
| 62 | |
| 63 | public boolean remove(int value) { |
| 64 | throw new UnsupportedOperationException("remove"); |
| 65 | } |
| 66 | |
| 67 | public void clear() { |
| 68 | throw new UnsupportedOperationException("clear"); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Add an integer to the set. Always throws UnsupportedOperationException |
| 73 | * @param value the integer to be added |
| 74 | * @return true if the integer was added, false if it was already present |
| 75 | * @throws UnsupportedOperationException (always) |
| 76 | */ |
| 77 | |
| 78 | public boolean add(int value) { |
| 79 | throw new UnsupportedOperationException("add"); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get an iterator over the values |
| 84 | */ |
| 85 | |
| 86 | public IntIterator iterator() { |
| 87 | return mutableCopy().iterator(); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | public String toString() { |
| 92 | return startPoint + " - " + endPoint; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Test whether this set has exactly the same members as another set. Note that |
| 97 | * IntBlockSet values are <b>NOT</b> comparable with other implementations of IntSet |
| 98 | */ |
| 99 | |
| 100 | public boolean equals(Object other) { |
| 101 | return mutableCopy().equals(other); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Construct a hash key that supports the equals() test |
| 106 | */ |
| 107 | |
| 108 | public int hashCode() { |
| 109 | // Note, hashcodes are NOT the same as those used by IntHashSet and IntArraySet |
| 110 | if (hashCode == -1) { |
| 111 | hashCode = 0x836a89f1 ^ (startPoint + (endPoint << 3)); |
| 112 | } |
| 113 | return hashCode; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get the start point of the range |
| 118 | */ |
| 119 | |
| 120 | public int getStartPoint() { |
| 121 | return startPoint; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get the end point of the range |
| 126 | */ |
| 127 | |
| 128 | public int getEndPoint() { |
| 129 | return endPoint; |
| 130 | } |
| 131 | |
| 132 | } |
| 133 |