Skip to content

Commit 4513643

Browse files
committed
more tests for custom array
1 parent b22f744 commit 4513643

File tree

5 files changed

+233
-51
lines changed

5 files changed

+233
-51
lines changed

src/main/java/org/apache/sysds/runtime/frame/data/columns/Array.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public abstract class Array<T> implements Writable {
5353
public static int ROW_PARALLELIZATION_THRESHOLD = 10000;
5454

5555
/** A soft reference to a memorization of this arrays mapping, used in transformEncode */
56-
protected SoftReference<Map<T,Integer>> _rcdMapCache = null;
56+
protected SoftReference<Map<T, Integer>> _rcdMapCache = null;
5757

5858
/** The current allocated number of elements in this Array */
5959
protected int _size;
@@ -73,7 +73,7 @@ protected int newSize() {
7373
*
7474
* @return The cached recode map
7575
*/
76-
public final SoftReference<Map<T,Integer>> getCache() {
76+
public final SoftReference<Map<T, Integer>> getCache() {
7777
return _rcdMapCache;
7878
}
7979

@@ -82,7 +82,7 @@ public final SoftReference<Map<T,Integer>> getCache() {
8282
*
8383
* @param m The element to cache.
8484
*/
85-
public final void setCache(SoftReference<Map<T,Integer>> m) {
85+
public final void setCache(SoftReference<Map<T, Integer>> m) {
8686
_rcdMapCache = m;
8787
}
8888

@@ -126,11 +126,11 @@ public synchronized final Map<T, Integer> getRecodeMap(int estimate) {
126126
* @throws ExecutionException if the parallel execution fails
127127
* @throws InterruptedException if the parallel execution fails
128128
*/
129-
public synchronized final Map<T,Integer> getRecodeMap(int estimate, ExecutorService pool, int k)
129+
public synchronized final Map<T, Integer> getRecodeMap(int estimate, ExecutorService pool, int k)
130130
throws InterruptedException, ExecutionException {
131131
// probe cache for existing map
132-
Map<T,Integer> map;
133-
SoftReference<Map<T,Integer>> tmp = getCache();
132+
Map<T, Integer> map;
133+
SoftReference<Map<T, Integer>> tmp = getCache();
134134
map = (tmp != null) ? tmp.get() : null;
135135
if(map != null)
136136
return map;
@@ -207,12 +207,12 @@ private HashMapToInt<T> parallelCreateRecodeMap(int estimate, ExecutorService po
207207
*/
208208
protected static <T> void mergeRecodeMaps(HashMapToInt<T> target, HashMapToInt<T> from) {
209209
final List<T> fromEntriesOrdered = new ArrayList<>(Collections.nCopies(from.size(), null));
210-
from.forEach((k,v) -> {
210+
from.forEach((k, v) -> {
211211
fromEntriesOrdered.set(v - 1, k);
212212
});
213213
int id = target.size();
214214
for(T e : fromEntriesOrdered) {
215-
if(target.putIfAbsent(e, id) == null)
215+
if(target.putIfAbsentI(e, id) == -1)
216216
id++;
217217
}
218218
}

src/main/java/org/apache/sysds/runtime/frame/data/columns/HashMapToInt.java

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,13 @@ public class HashMapToInt<K> implements Map<K, Integer>, Serializable, Cloneable
3131

3232
private static final long serialVersionUID = 3624988207265L;
3333
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;
34-
static final int MAXIMUM_CAPACITY = 1 << 30;
3534
static final float DEFAULT_LOAD_FACTOR = 0.75f;
3635

37-
static class Node<K> implements Entry<K, Integer> {
38-
final K key;
39-
int value;
40-
Node<K> next;
41-
42-
Node(K key, int value, Node<K> next) {
43-
this.key = key;
44-
this.value = value;
45-
this.next = next;
46-
}
47-
48-
public final void setNext(Node<K> n) {
49-
next = n;
50-
}
51-
52-
@Override
53-
public K getKey() {
54-
return key;
55-
}
56-
57-
@Override
58-
public Integer getValue() {
59-
return value;
60-
}
61-
62-
@Override
63-
public Integer setValue(Integer value) {
64-
return this.value = value;
65-
}
66-
}
67-
6836
protected Node<K>[] buckets;
69-
int size;
70-
// protected List<List<K>> keys;
71-
// protected int[][] values;
37+
protected int size;
7238

7339
public HashMapToInt(int capacity) {
74-
alloc(Math.max(capacity, 16));
40+
alloc(Math.max(capacity, DEFAULT_INITIAL_CAPACITY));
7541
}
7642

7743
@SuppressWarnings({"unchecked"})
@@ -98,7 +64,14 @@ public boolean containsKey(Object key) {
9864

9965
@Override
10066
public boolean containsValue(Object value) {
101-
throw new UnsupportedOperationException("Unimplemented method 'containsValue'");
67+
if(value instanceof Integer) {
68+
for(Entry<K, Integer> v : this.entrySet()) {
69+
if(v.getValue().equals(value))
70+
return true;
71+
}
72+
}
73+
return false;
74+
10275
}
10376

10477
@Override
@@ -161,7 +134,7 @@ private int putIfAbsentBucket(int ix, K key, int value) {
161134
if(b.key.equals(key))
162135
return b.value;
163136
if(b.next == null) {
164-
b.next = new Node<>(key, value, null);
137+
b.setNext(new Node<>(key, value, null));
165138
size++;
166139
return -1;
167140
}
@@ -189,12 +162,12 @@ private int addToBucket(int ix, K key, int value) {
189162
while(true) {
190163

191164
if(b.key.equals(key)) {
192-
int tmp = b.value;
193-
b.value = value;
165+
int tmp = b.getValue();
166+
b.setValue(value);
194167
return tmp;
195168
}
196169
if(b.next == null) {
197-
b.next = new Node<>(key, value, null);
170+
b.setNext(new Node<>(key, value, null));
198171
size++;
199172
return -1;
200173
}
@@ -246,13 +219,44 @@ public void forEach(BiConsumer<? super K, ? super Integer> action) {
246219

247220
@Override
248221
public String toString() {
249-
StringBuilder sb = new StringBuilder();
222+
StringBuilder sb = new StringBuilder(size()*3);
250223
this.forEach((k, v) -> {
251224
sb.append("(" + k + "→" + v + ")");
252225
});
253226
return sb.toString();
254227
}
255228

229+
private static class Node<K> implements Entry<K, Integer> {
230+
final K key;
231+
int value;
232+
Node<K> next;
233+
234+
Node(K key, int value, Node<K> next) {
235+
this.key = key;
236+
this.value = value;
237+
this.next = next;
238+
}
239+
240+
public final void setNext(Node<K> n) {
241+
next = n;
242+
}
243+
244+
@Override
245+
public K getKey() {
246+
return key;
247+
}
248+
249+
@Override
250+
public Integer getValue() {
251+
return value;
252+
}
253+
254+
@Override
255+
public Integer setValue(Integer value) {
256+
return this.value = value;
257+
}
258+
}
259+
256260
private final class EntrySet extends AbstractSet<Map.Entry<K, Integer>> {
257261

258262
@Override
@@ -299,7 +303,7 @@ public Entry<K, Integer> next() {
299303
break;
300304
}
301305
}
302-
if(bucketId == buckets.length)
306+
if(bucketId >= buckets.length)
303307
next = null;
304308
}
305309

src/main/java/org/apache/sysds/runtime/transform/encode/ColumnEncoderBagOfWords.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ public void writeExternal(ObjectOutput out) throws IOException {
383383
out.writeInt(_tokenDictionary == null ? 0 : _tokenDictionary.size());
384384
if(_tokenDictionary != null)
385385
for(Map.Entry<Object, Integer> e : _tokenDictionary.entrySet()) {
386+
System.out.println(e);
386387
out.writeUTF((String) e.getKey());
387388
out.writeInt(e.getValue());
388389
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.sysds.test.component.frame.array;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertNull;
24+
import static org.junit.Assert.assertTrue;
25+
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
import java.util.Map.Entry;
29+
import java.util.Random;
30+
31+
import org.apache.sysds.runtime.frame.data.columns.HashMapToInt;
32+
import org.junit.Test;
33+
34+
public class HashMapToIntTest {
35+
36+
@Test
37+
public void insert() {
38+
Map<Integer, Integer> m = new HashMapToInt<>(10);
39+
m.put(1, 1);
40+
assertTrue(m.containsKey(1));
41+
assertTrue(m.containsValue(1));
42+
}
43+
44+
@Test
45+
public void isEmpty() {
46+
Map<Integer, Integer> m = new HashMapToInt<>(10);
47+
assertTrue(m.isEmpty());
48+
m.put(1, 1);
49+
assertFalse(m.isEmpty());
50+
}
51+
52+
@Test
53+
public void insert10() {
54+
55+
Map<Integer, Integer> m = new HashMapToInt<>(10);
56+
for(int i = 0; i < 10; i++) {
57+
m.put(i, i);
58+
assertFalse(m.isEmpty());
59+
assertTrue(m.containsKey(i));
60+
assertTrue(m.containsValue(i));
61+
}
62+
63+
for(int i = 0; i < 10; i++) {
64+
assertTrue(m.containsKey(i));
65+
assertTrue(m.containsValue(i));
66+
}
67+
}
68+
69+
@Test
70+
public void forEach() {
71+
72+
Map<Integer, Integer> m = new HashMapToInt<>(10);
73+
Map<Integer, Integer> m2 = new HashMap<>();
74+
Random r = new Random(32);
75+
for(int i = 0; i < 100; i++) {
76+
int v1 = r.nextInt();
77+
int v2 = r.nextInt();
78+
m.put(v1, v2);
79+
m2.put(v1, v2);
80+
}
81+
82+
assertEquals(m.size(), m2.size());
83+
for(Entry<Integer, Integer> e : m2.entrySet()) {
84+
assertTrue(m.containsKey(e.getKey()));
85+
}
86+
87+
assertEquals(m.size(), m2.size());
88+
for(Entry<Integer, Integer> e : m.entrySet()) {
89+
assertTrue(m2.containsKey(e.getKey()));
90+
assertEquals(m.get(e.getKey()), m2.get(e.getKey()));
91+
}
92+
93+
}
94+
95+
@Test
96+
public void doNotContainKey() {
97+
Map<Integer, Integer> m = new HashMapToInt<>(10);
98+
for(int i = 0; i < 100; i++) {
99+
assertFalse(m.containsKey(i));
100+
assertFalse(m.containsValue(i * 10000));
101+
m.put(i, i * 10000);
102+
assertTrue(m.containsKey(i));
103+
assertTrue(m.containsValue(i * 10000));
104+
assertEquals(m.get(i), Integer.valueOf(i * 10000));
105+
}
106+
107+
}
108+
109+
@Test
110+
public void doNotContainValue() {
111+
Map<Integer, Integer> m = new HashMapToInt<>(10);
112+
113+
assertFalse(m.containsValue(new Object()));
114+
115+
}
116+
117+
@Test
118+
public void overwriteKey() {
119+
Map<Integer, Integer> m = new HashMapToInt<>(10);
120+
121+
Integer v;
122+
v = m.put(1, 10);
123+
assertEquals(Integer.valueOf(10), m.get(Integer.valueOf(1)));
124+
assertEquals(v, null);
125+
v = m.put(1, 11);
126+
assertEquals(v, Integer.valueOf(10));
127+
assertEquals(Integer.valueOf(11), m.get(Integer.valueOf(1)));
128+
v = m.put(1, 12);
129+
assertEquals(v, Integer.valueOf(11));
130+
assertEquals(Integer.valueOf(12), m.get(Integer.valueOf(1)));
131+
}
132+
133+
@Test
134+
public void forEach2() {
135+
Map<Integer, Integer> m = new HashMapToInt<>(10);
136+
for(int i = 900; i < 1000; i++) {
137+
m.put(i, i * 32121523);
138+
}
139+
final Map<Integer, Integer> m2 = new HashMap<>();
140+
m.forEach((k, v) -> m2.put(k, v));
141+
m2.forEach((k, v) -> assertTrue("key missing: " + k, m.containsKey(k)));
142+
}
143+
144+
@Test
145+
public void testToString() {
146+
Map<Integer, Integer> m = new HashMapToInt<>(10);
147+
148+
assertEquals(0, m.toString().length());
149+
m.put(555, 321);
150+
String s = m.toString();
151+
assertTrue(s.contains("555"));
152+
assertTrue(s.contains("321"));
153+
}
154+
155+
@Test
156+
public void testSizeOfKeySet() {
157+
Map<Integer, Integer> m = new HashMapToInt<>(10);
158+
for(int i = 0; i < 10; i++) {
159+
m.put(i * 321, i * 3222);
160+
assertEquals(m.size(), m.entrySet().size());
161+
}
162+
}
163+
164+
@Test
165+
public void putIfAbsent(){
166+
Map<Integer, Integer> m = new HashMapToInt<>(10);
167+
for(int i = 0; i < 1000; i++) {
168+
assertNull(m.putIfAbsent(i * 321, i * 3222));
169+
170+
}
171+
172+
for(int i = 0; i < 1000; i++) {
173+
assertEquals(i*3222,(int)m.putIfAbsent(i * 321, i * 3222));
174+
}
175+
}
176+
}

src/test/java/org/apache/sysds/test/functions/transform/TransformFrameEncodeBagOfWords.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ private void runTransformTest(String testname, ExecMode rt, boolean recode, bool
266266

267267
}
268268
catch(Exception ex) {
269+
ex.printStackTrace();
269270
throw new RuntimeException(ex);
270271
}
271272
finally {

0 commit comments

Comments
 (0)