Skip to content

Commit dcc197c

Browse files
committed
refactored Relabel;ByHashing
1 parent 343a496 commit dcc197c

File tree

8 files changed

+788
-410
lines changed

8 files changed

+788
-410
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* JavaPermutationTools: A Java library for computation on permutations and sequences
3+
* Copyright 2005-2025 Vincent A. Cicirello, <https://www.cicirello.org/>.
4+
*
5+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
6+
*
7+
* JavaPermutationTools is free software: you can
8+
* redistribute it and/or modify it under the terms of the GNU
9+
* General Public License as published by the Free Software
10+
* Foundation, either version 3 of the License, or (at your
11+
* option) any later version.
12+
*
13+
* JavaPermutationTools is distributed in the hope
14+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
15+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
* PARTICULAR PURPOSE. See the GNU General Public License for more
17+
* details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
package org.cicirello.sequences.distance.internal;
23+
24+
/** Internal class for hashtable of chars. */
25+
final class CharHT {
26+
27+
private final Node[] table;
28+
private final int mask;
29+
30+
CharHT(int minSize) {
31+
minSize = adjustSize(minSize);
32+
mask = minSize - 1;
33+
table = new Node[minSize];
34+
}
35+
36+
int adjustSize(int minSize) {
37+
final int MAX_SIZE = 0x40000000;
38+
if (minSize > MAX_SIZE) {
39+
return MAX_SIZE;
40+
}
41+
minSize = minSize - 1;
42+
minSize = minSize | (minSize >> 1);
43+
minSize = minSize | (minSize >> 2);
44+
minSize = minSize | (minSize >> 4);
45+
minSize = minSize | (minSize >> 8);
46+
minSize = minSize | (minSize >> 16);
47+
return minSize + 1;
48+
}
49+
50+
int size() {
51+
return table.length;
52+
}
53+
54+
int mask() {
55+
return mask;
56+
}
57+
58+
int populate(char[] s1) {
59+
int current = -1;
60+
for (int i = 0; i < s1.length; i++) {
61+
if (!containsKey(s1[i])) {
62+
current++;
63+
put(s1[i], current);
64+
}
65+
}
66+
return current;
67+
}
68+
69+
int populate(String s1) {
70+
int current = -1;
71+
for (int i = 0; i < s1.length(); i++) {
72+
if (!containsKey(s1.charAt(i))) {
73+
current++;
74+
put(s1.charAt(i), current);
75+
}
76+
}
77+
return current;
78+
}
79+
80+
int index(char key) {
81+
return key & mask;
82+
}
83+
84+
boolean containsKey(char key) {
85+
for (Node current = table[index(key)]; current != null; current = current.next) {
86+
if (current.key == key) return true;
87+
}
88+
return false;
89+
}
90+
91+
int get(char key) {
92+
for (Node current = table[index(key)]; current != null; current = current.next) {
93+
if (current.key == key) return current.value;
94+
}
95+
// NOTE: our internal usage never puts a negative as a value
96+
return -1;
97+
}
98+
99+
void put(char key, int value) {
100+
// warning: assumes key is not already in hash table (only used internally so ok).
101+
int i = index(key);
102+
table[i] = new Node(key, value, table[i]);
103+
}
104+
105+
static final class Node {
106+
char key;
107+
int value;
108+
Node next;
109+
110+
Node(char key, int value, Node next) {
111+
this.key = key;
112+
this.value = value;
113+
this.next = next;
114+
}
115+
}
116+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* JavaPermutationTools: A Java library for computation on permutations and sequences
3+
* Copyright 2005-2025 Vincent A. Cicirello, <https://www.cicirello.org/>.
4+
*
5+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
6+
*
7+
* JavaPermutationTools is free software: you can
8+
* redistribute it and/or modify it under the terms of the GNU
9+
* General Public License as published by the Free Software
10+
* Foundation, either version 3 of the License, or (at your
11+
* option) any later version.
12+
*
13+
* JavaPermutationTools is distributed in the hope
14+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
15+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
* PARTICULAR PURPOSE. See the GNU General Public License for more
17+
* details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
package org.cicirello.sequences.distance.internal;
23+
24+
/** Internal class for hashtable of doubles. */
25+
final class DoubleHT {
26+
27+
private final Node[] table;
28+
private final int mask;
29+
30+
DoubleHT(int minSize) {
31+
minSize = adjustSize(minSize);
32+
mask = minSize - 1;
33+
table = new Node[minSize];
34+
}
35+
36+
int adjustSize(int minSize) {
37+
final int MAX_SIZE = 0x40000000;
38+
if (minSize > MAX_SIZE) {
39+
return MAX_SIZE;
40+
}
41+
minSize = minSize - 1;
42+
minSize = minSize | (minSize >> 1);
43+
minSize = minSize | (minSize >> 2);
44+
minSize = minSize | (minSize >> 4);
45+
minSize = minSize | (minSize >> 8);
46+
minSize = minSize | (minSize >> 16);
47+
return minSize + 1;
48+
}
49+
50+
int size() {
51+
return table.length;
52+
}
53+
54+
int mask() {
55+
return mask;
56+
}
57+
58+
int populate(double[] s1) {
59+
int current = -1;
60+
for (int i = 0; i < s1.length; i++) {
61+
if (!containsKey(s1[i])) {
62+
current++;
63+
put(s1[i], current);
64+
}
65+
}
66+
return current;
67+
}
68+
69+
int index(double key) {
70+
long x = Double.doubleToLongBits(key);
71+
int y = (int) (x ^ (x >>> 32));
72+
return (y ^ (y >>> 16)) & mask;
73+
}
74+
75+
boolean containsKey(double key) {
76+
for (Node current = table[index(key)]; current != null; current = current.next) {
77+
if (current.key == key) return true;
78+
}
79+
return false;
80+
}
81+
82+
int get(double key) {
83+
for (Node current = table[index(key)]; current != null; current = current.next) {
84+
if (current.key == key) return current.value;
85+
}
86+
// NOTE: our internal usage never puts a negative as a value
87+
return -1;
88+
}
89+
90+
void put(double key, int value) {
91+
// warning: assumes key is not already in hash table (only used internally so ok).
92+
int i = index(key);
93+
table[i] = new Node(key, value, table[i]);
94+
}
95+
96+
static final class Node {
97+
double key;
98+
int value;
99+
Node next;
100+
101+
Node(double key, int value, Node next) {
102+
this.key = key;
103+
this.value = value;
104+
this.next = next;
105+
}
106+
}
107+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* JavaPermutationTools: A Java library for computation on permutations and sequences
3+
* Copyright 2005-2025 Vincent A. Cicirello, <https://www.cicirello.org/>.
4+
*
5+
* This file is part of JavaPermutationTools (https://jpt.cicirello.org/).
6+
*
7+
* JavaPermutationTools is free software: you can
8+
* redistribute it and/or modify it under the terms of the GNU
9+
* General Public License as published by the Free Software
10+
* Foundation, either version 3 of the License, or (at your
11+
* option) any later version.
12+
*
13+
* JavaPermutationTools is distributed in the hope
14+
* that it will be useful, but WITHOUT ANY WARRANTY; without even
15+
* the implied warranty of MERCHANTABILITY or FITNESS FOR A
16+
* PARTICULAR PURPOSE. See the GNU General Public License for more
17+
* details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with JavaPermutationTools. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
package org.cicirello.sequences.distance.internal;
23+
24+
/** Internal class for hashtable of floats. */
25+
final class FloatHT {
26+
27+
private final Node[] table;
28+
private final int mask;
29+
30+
FloatHT(int minSize) {
31+
minSize = adjustSize(minSize);
32+
mask = minSize - 1;
33+
table = new Node[minSize];
34+
}
35+
36+
int adjustSize(int minSize) {
37+
final int MAX_SIZE = 0x40000000;
38+
if (minSize > MAX_SIZE) {
39+
return MAX_SIZE;
40+
}
41+
minSize = minSize - 1;
42+
minSize = minSize | (minSize >> 1);
43+
minSize = minSize | (minSize >> 2);
44+
minSize = minSize | (minSize >> 4);
45+
minSize = minSize | (minSize >> 8);
46+
minSize = minSize | (minSize >> 16);
47+
return minSize + 1;
48+
}
49+
50+
int size() {
51+
return table.length;
52+
}
53+
54+
int mask() {
55+
return mask;
56+
}
57+
58+
int populate(float[] s1) {
59+
int current = -1;
60+
for (int i = 0; i < s1.length; i++) {
61+
if (!containsKey(s1[i])) {
62+
current++;
63+
put(s1[i], current);
64+
}
65+
}
66+
return current;
67+
}
68+
69+
int index(float key) {
70+
int x = Float.floatToIntBits(key);
71+
return (x ^ (x >>> 16)) & mask;
72+
}
73+
74+
boolean containsKey(float key) {
75+
for (Node current = table[index(key)]; current != null; current = current.next) {
76+
if (current.key == key) return true;
77+
}
78+
return false;
79+
}
80+
81+
int get(float key) {
82+
for (Node current = table[index(key)]; current != null; current = current.next) {
83+
if (current.key == key) return current.value;
84+
}
85+
// NOTE: our internal usage never puts a negative as a value
86+
return -1;
87+
}
88+
89+
void put(float key, int value) {
90+
// warning: assumes key is not already in hash table (only used internally so ok).
91+
int i = index(key);
92+
table[i] = new Node(key, value, table[i]);
93+
}
94+
95+
static final class Node {
96+
float key;
97+
int value;
98+
Node next;
99+
100+
Node(float key, int value, Node next) {
101+
this.key = key;
102+
this.value = value;
103+
this.next = next;
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)