SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
CoordinateSetLong.java
1package umontreal.ssj.mcqmctools.anova;
2
3import java.util.*;
4
10public class CoordinateSetLong extends CoordinateSet {
11
12 protected long mask;
13
18 public CoordinateSetLong(long mask) {
19 this.mask = mask;
20 }
21
26 public long getMask() {
27 return mask;
28 }
29
30 @Override
31 public boolean equals(Object o) {
32 if (o instanceof CoordinateSetLong)
33 return mask == ((CoordinateSetLong) o).mask;
34 else
35 return super.equals(o);
36 }
37
38 @Override
39 public List<Integer> asList() {
40 List<Integer> list = new ArrayList<Integer>();
41 long x = mask;
42 int coord = 0;
43 while (x != 0) {
44 if ((x & 1) == 1)
45 list.add(coord);
46 coord++;
47 x = x >> 1;
48 }
49 return list;
50 }
51
52 @Override
53 public boolean contains(int coord) {
54 return ((mask >> coord) & 1) == 1;
55 }
56
57 @Override
58 public boolean containsAll(CoordinateSet cs) {
59 if (cs instanceof CoordinateSetLong)
60 return (mask | ((CoordinateSetLong) cs).mask) == mask;
61 else
62 return super.containsAll(cs);
63 }
64
65 @Override
66 public int cardinality() {
67 // count the bits set to 1
68 long x = mask;
69 int count = 0;
70 while (x != 0) {
71 count++;
72 x &= x - 1;
73 }
74 return count;
75 }
76
81 public int maxCoordinate() {
82 int c = 0;
83 while ((mask >> c) != 0)
84 c++;
85 return c - 1;
86 }
87
93 @Override
94 public List<CoordinateSet> subsets(boolean includeEmptySet, int maxOrder) {
95
96 maxOrder = Math.min(maxOrder, maxCoordinate() + 1);
97
98 long maskMax = (1L << (maxCoordinate() + 1));
99
100 List<CoordinateSet> list = new ArrayList<CoordinateSet>();
101
102 for (int order = includeEmptySet ? 0 : 1; order <= maxOrder; order++) {
103
104 // enumeration with Gosper's hack
105 // http://home.pipeline.com/~hbaker1/hakmem/hacks.html#item175
106
107 long mask = (1L << order) - 1;
108 while (mask < maskMax) {
109
110 // add subset to list
111 CoordinateSet cs = new CoordinateSetLong(mask);
112 if (containsAll(cs))
113 list.add(cs);
114
115 // compute next mask with Gosper's hack
116 long u = mask & -mask; // rightmost bit
117 long v = mask + u;
118 if (v == 0)
119 break;
120 mask = v + (((v ^ mask) / u) >> 2);
121 }
122 }
123
124 // ! // inefficient exhaustive enumeration
125 // ! long maskMin = includeEmptySet ? 0 : 1;
126 // ! for (long mask = maskMin; mask < maskMax; mask++) {
127 // ! CoordinateSet cs = new CoordinateSetLong(mask);
128 // ! if (cs.cardinality() <= maxOrder && containsAll(cs))
129 // ! list.add(cs);
130 // ! }
131
132 return list;
133 }
134
139 public static CoordinateSet allCoordinates(int dimension) {
140 return new CoordinateSetLong((1L << dimension) - 1);
141 }
142}
Implementation of CoordinateSet using a long bit-mask internal representation.
int cardinality()
Returns the cardinality of the current coordinate set.
boolean contains(int coord)
Returns true if the current set contains coordinate coord.
int maxCoordinate()
Returns the maximum coordinate index, starting at 0 for the first coordinate.
long getMask()
Returns the bit-mask representation of the current coordinate set.
boolean containsAll(CoordinateSet cs)
Returns true if the current set contains all coordinates in cs.
CoordinateSetLong(long mask)
Constructs a coordinate set with corresponding bit mask mask.
List< CoordinateSet > subsets(boolean includeEmptySet, int maxOrder)
Returns all subsets of the current coordinate set, whose cardinality is at most maxOrder.
static CoordinateSet allCoordinates(int dimension)
Returns a set of all coordinates in a space of dimension dimension.