|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| package edu.rice.cs.plt.object; |
|
36 |
| |
|
37 |
| import java.util.Comparator; |
|
38 |
| |
|
39 |
| import edu.rice.cs.plt.iter.IterUtil; |
|
40 |
| |
|
41 |
| |
|
42 |
| public final class ObjectUtil { |
|
43 |
| |
|
44 |
0
| private ObjectUtil() {}
|
|
45 |
| |
|
46 |
| |
|
47 |
0
| public static boolean equal(Object o1, Object o2) {
|
|
48 |
0
| return (o1 == null) ? (o2 == null) : o1.equals(o2);
|
|
49 |
| } |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| private static final int KNUTH_CONST = -1640531535; |
|
54 |
| |
|
55 |
| |
|
56 |
0
| public static int hash() { return 1; }
|
|
57 |
| |
|
58 |
| |
|
59 |
0
| public static int hash(int a) { return KNUTH_CONST ^ a; }
|
|
60 |
| |
|
61 |
| |
|
62 |
0
| public static int hash(int a, int b) { return ((KNUTH_CONST ^ a) * KNUTH_CONST) ^ b; }
|
|
63 |
| |
|
64 |
| |
|
65 |
0
| public static int hash(int a, int b, int c) {
|
|
66 |
0
| return ((((KNUTH_CONST ^ a) * KNUTH_CONST) ^ b) * KNUTH_CONST) ^ c;
|
|
67 |
| } |
|
68 |
| |
|
69 |
| |
|
70 |
0
| public static int hash(int a, int b, int c, int d) {
|
|
71 |
0
| return ((((((KNUTH_CONST ^ a) * KNUTH_CONST) ^ b) * KNUTH_CONST) ^ c) * KNUTH_CONST) ^ d;
|
|
72 |
| } |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
0
| public static int hash(int... keys) {
|
|
81 |
0
| int len = keys.length;
|
|
82 |
0
| int result = 1;
|
|
83 |
0
| for (int i = 0; i < len; i++) { result = (result * KNUTH_CONST) ^ keys[i]; }
|
|
84 |
0
| return result;
|
|
85 |
| } |
|
86 |
| |
|
87 |
| |
|
88 |
0
| public static int hash(Object a) { return hash((a == null) ? 0 : a.hashCode()); }
|
|
89 |
| |
|
90 |
| |
|
91 |
0
| public static int hash(Object a, Object b) {
|
|
92 |
0
| return hash((a == null) ? 0 : a.hashCode(), (b == null) ? 0 : b.hashCode());
|
|
93 |
| } |
|
94 |
| |
|
95 |
| |
|
96 |
0
| public static int hash(Object a, Object b, Object c) {
|
|
97 |
0
| return hash((a == null) ? 0 : a.hashCode(), (b == null) ? 0 : b.hashCode(),
|
|
98 |
0
| (c == null) ? 0 : c.hashCode());
|
|
99 |
| } |
|
100 |
| |
|
101 |
| |
|
102 |
0
| public static int hash(Object a, Object b, Object c, Object d) {
|
|
103 |
0
| return hash((a == null) ? 0 : a.hashCode(), (b == null) ? 0 : b.hashCode(),
|
|
104 |
0
| (c == null) ? 0 : c.hashCode(), (d == null) ? 0 : d.hashCode());
|
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
0
| public static int hash(Object... objs) {
|
|
109 |
0
| int len = objs.length;
|
|
110 |
0
| int[] keys = new int[len];
|
|
111 |
0
| for (int i = 0; i < len; i++) {
|
|
112 |
0
| Object obj = objs[i];
|
|
113 |
0
| keys[i] = (obj == null) ? 0 : obj.hashCode();
|
|
114 |
| } |
|
115 |
0
| return hash(keys);
|
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
0
| public static int hash(Iterable<?> iter) {
|
|
120 |
0
| int result = 1;
|
|
121 |
0
| for (Object obj : iter) {
|
|
122 |
0
| int key = (obj == null) ? 0 : obj.hashCode();
|
|
123 |
0
| result = (result * KNUTH_CONST) ^ key;
|
|
124 |
| } |
|
125 |
0
| return result;
|
|
126 |
| } |
|
127 |
| |
|
128 |
0
| public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>>
|
|
129 |
| int compare(T1 x1, T1 y1, T2 x2, T2 y2) { |
|
130 |
0
| int result = x1.compareTo(y1);
|
|
131 |
0
| if (result == 0) { result = x2.compareTo(y2); }
|
|
132 |
0
| return result;
|
|
133 |
| } |
|
134 |
| |
|
135 |
36
| public static <T1, T2> int compare(Comparator<? super T1> comp1, T1 x1, T1 y1,
|
|
136 |
| Comparator<? super T2> comp2, T2 x2, T2 y2) { |
|
137 |
36
| int result = comp1.compare(x1, y1);
|
|
138 |
20
| if (result == 0) { result = comp2.compare(x2, y2); }
|
|
139 |
36
| return result;
|
|
140 |
| } |
|
141 |
| |
|
142 |
0
| public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>,
|
|
143 |
| T3 extends Comparable<? super T3>> |
|
144 |
| int compare(T1 x1, T1 y1, T2 x2, T2 y2, T3 x3, T3 y3) { |
|
145 |
0
| int result = x1.compareTo(y1);
|
|
146 |
0
| if (result == 0) { result = x2.compareTo(y2); }
|
|
147 |
0
| if (result == 0) { result = x3.compareTo(y3); }
|
|
148 |
0
| return result;
|
|
149 |
| } |
|
150 |
| |
|
151 |
0
| public static <T1, T2, T3> int compare(Comparator<? super T1> comp1, T1 x1, T1 y1,
|
|
152 |
| Comparator<? super T2> comp2, T2 x2, T2 y2, |
|
153 |
| Comparator<? super T3> comp3, T3 x3, T3 y3) { |
|
154 |
0
| int result = comp1.compare(x1, y1);
|
|
155 |
0
| if (result == 0) { result = comp2.compare(x2, y2); }
|
|
156 |
0
| if (result == 0) { result = comp3.compare(x3, y3); }
|
|
157 |
0
| return result;
|
|
158 |
| } |
|
159 |
| |
|
160 |
0
| public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>,
|
|
161 |
| T3 extends Comparable<? super T3>, T4 extends Comparable<? super T4>> |
|
162 |
| int compare(T1 x1, T1 y1, T2 x2, T2 y2, T3 x3, T3 y3, T4 x4, T4 y4) { |
|
163 |
0
| int result = x1.compareTo(y1);
|
|
164 |
0
| if (result == 0) { result = x2.compareTo(y2); }
|
|
165 |
0
| if (result == 0) { result = x3.compareTo(y3); }
|
|
166 |
0
| if (result == 0) { result = x4.compareTo(y4); }
|
|
167 |
0
| return result;
|
|
168 |
| } |
|
169 |
| |
|
170 |
0
| public static <T1, T2, T3, T4> int compare(Comparator<? super T1> comp1, T1 x1, T1 y1,
|
|
171 |
| Comparator<? super T2> comp2, T2 x2, T2 y2, |
|
172 |
| Comparator<? super T3> comp3, T3 x3, T3 y3, |
|
173 |
| Comparator<? super T4> comp4, T4 x4, T4 y4) { |
|
174 |
0
| int result = comp1.compare(x1, y1);
|
|
175 |
0
| if (result == 0) { result = comp2.compare(x2, y2); }
|
|
176 |
0
| if (result == 0) { result = comp3.compare(x3, y3); }
|
|
177 |
0
| if (result == 0) { result = comp4.compare(x4, y4); }
|
|
178 |
0
| return result;
|
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
0
| public static int compositeHeight(Object obj) {
|
|
187 |
0
| if (obj instanceof Composite) { return ((Composite) obj).compositeHeight(); }
|
|
188 |
0
| else { return 0; }
|
|
189 |
| } |
|
190 |
| |
|
191 |
| |
|
192 |
0
| public static int compositeHeight(Object... objs) { return compositeHeight(IterUtil.asIterable(objs)); }
|
|
193 |
| |
|
194 |
| |
|
195 |
0
| public static int compositeHeight(Iterable<?> objs) {
|
|
196 |
0
| int result = 0;
|
|
197 |
0
| for (Object obj : objs) {
|
|
198 |
0
| int height = compositeHeight(obj);
|
|
199 |
0
| if (result < height) { result = height; }
|
|
200 |
| } |
|
201 |
0
| return result;
|
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
0
| public static int compositeSize(Object obj) {
|
|
209 |
0
| if (obj instanceof Composite) { return ((Composite) obj).compositeSize(); }
|
|
210 |
0
| else { return 1; }
|
|
211 |
| } |
|
212 |
| |
|
213 |
| |
|
214 |
0
| public static int compositeSize(Object... objs) { return compositeSize(IterUtil.asIterable(objs)); }
|
|
215 |
| |
|
216 |
| |
|
217 |
0
| public static int compositeSize(Iterable<?> objs) {
|
|
218 |
0
| int result = 0;
|
|
219 |
0
| for (Object obj : objs) { result += compositeSize(obj); }
|
|
220 |
0
| return result;
|
|
221 |
| } |
|
222 |
| |
|
223 |
| } |