Debian Codegraph @

net/sf/saxon/z/IntComplementSet.java

raw symbols SCIP
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 * An immutable integer set containing all int values except those in an excluded set
12 */
13 public class IntComplementSet implements IntSet {
14
15 private IntSet exclusions;
16
17 public IntComplementSet(IntSet exclusions) {
18 this.exclusions = exclusions.copy();
19 }
20
21 public IntSet getExclusions() {
22 return exclusions;
23 }
24
25 public IntSet copy() {
26 return new IntComplementSet(exclusions.copy());
27 }
28
29 public IntSet mutableCopy() {
30 return copy();
31 }
32
33 public void clear() {
34 throw new UnsupportedOperationException("IntComplementSet cannot be emptied");
35 }
36
37 public int size() {
38 return Integer.MAX_VALUE - exclusions.size();
39 }
40
41 public boolean isEmpty() {
42 return size() == 0;
43 }
44
45 public boolean contains(int value) {
46 return !exclusions.contains(value);
47 }
48
49 public boolean remove(int value) {
50 boolean b = contains(value);
51 if (b) {
52 exclusions.add(value);
53 }
54 return b;
55 }
56
57 public boolean add(int value) {
58 boolean b = contains(value);
59 if (!b) {
60 exclusions.remove(value);
61 }
62 return b;
63 }
64
65 public IntIterator iterator() {
66 throw new UnsupportedOperationException("Cannot enumerate an infinite set");
67 }
68
69 public IntSet union(IntSet other) {
70 return new IntComplementSet(exclusions.except(other));
71 }
72
73 public IntSet intersect(IntSet other) {
74 if (other.isEmpty()) {
75 return IntEmptySet.getInstance();
76 } else if (other == IntUniversalSet.getInstance()) {
77 return copy();
78 } else if (other instanceof IntComplementSet) {
79 return new IntComplementSet(exclusions.union(((IntComplementSet) other).exclusions));
80 } else {
81 return other.intersect(this);
82 }
83 }
84
85 public IntSet except(IntSet other) {
86 return new IntComplementSet(exclusions.union(other));
87 }
88
89 public boolean containsAll(/*@NotNull*/ IntSet other) {
90 if (other instanceof IntComplementSet) {
91 return ((IntComplementSet) other).exclusions.containsAll(exclusions);
92 } else if (other instanceof IntUniversalSet) {
93 return (!exclusions.isEmpty());
94 } else {
95 IntIterator ii = other.iterator();
96 while (ii.hasNext()) {
97 if (exclusions.contains(ii.next())) {
98 return false;
99 }
100 }
101 return true;
102 }
103 }
104 }