make keys comparable
This commit is contained in:
parent
aceaac08d6
commit
0483aa0bba
6 changed files with 103 additions and 6 deletions
|
@ -174,7 +174,8 @@ public abstract class IndexedMapBase<K, V> implements IndexedMap<K, V> {
|
|||
return sb.append('}').toString();
|
||||
}
|
||||
|
||||
private class ValuesView extends IndexedCollectionBase<V> implements IndexedCollectionBase.NoAdditiveChange<V> {
|
||||
private class ValuesView extends IndexedCollectionBase<V>
|
||||
implements IndexedCollectionBase.NoAdditiveChange<V> {
|
||||
|
||||
@Override
|
||||
public V getEntryAt(int index) {
|
||||
|
@ -208,7 +209,8 @@ public abstract class IndexedMapBase<K, V> implements IndexedMap<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
private class KeysView extends IndexedSetBase<K> implements IndexedCollectionBase.NoAdditiveChange<K> {
|
||||
private class KeysView extends IndexedSetBase<K>
|
||||
implements IndexedCollectionBase.NoAdditiveChange<K> {
|
||||
|
||||
@Override
|
||||
public int getIndex(Object key) {
|
||||
|
@ -247,7 +249,8 @@ public abstract class IndexedMapBase<K, V> implements IndexedMap<K, V> {
|
|||
}
|
||||
}
|
||||
|
||||
private class EntriesView extends IndexedSetBase<Map.Entry<K, V>> implements IndexedCollectionBase.NoAdditiveChange<Map.Entry<K, V>> {
|
||||
private class EntriesView extends IndexedSetBase<Map.Entry<K, V>>
|
||||
implements IndexedCollectionBase.NoAdditiveChange<Map.Entry<K, V>> {
|
||||
|
||||
@Override
|
||||
public int getIndex(Object key) {
|
||||
|
|
|
@ -2,5 +2,5 @@ package org.xbib.datastructures.tiny;
|
|||
|
||||
import java.util.Set;
|
||||
|
||||
public interface IndexedSet<T> extends Set<T>, IndexedCollection<T> {
|
||||
public interface IndexedSet<T> extends Set<T>, IndexedCollection<T>, Comparable<IndexedSet<T>> {
|
||||
}
|
||||
|
|
|
@ -31,4 +31,9 @@ public abstract class IndexedSetBase<T> extends IndexedCollectionBase<T> impleme
|
|||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(IndexedSet<T> indexedSet) {
|
||||
return Integer.compare(hashCode(), indexedSet.hashCode());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,9 @@ public abstract class TinyMap<K, V> extends IndexedMapBase<K, V> {
|
|||
}
|
||||
};
|
||||
|
||||
public static class Builder<K, V> extends IndexedMapBase<K, V> {
|
||||
public static class Builder<K, V>
|
||||
extends IndexedMapBase<K, V>
|
||||
implements Comparable<Builder<K, V>> {
|
||||
|
||||
private final TinySet.Builder<K> keys;
|
||||
|
||||
|
@ -155,6 +157,32 @@ public abstract class TinyMap<K, V> extends IndexedMapBase<K, V> {
|
|||
Arrays.fill(values, 0, keys.rawSize(), null);
|
||||
keys.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return keys.hashCode() ^ Arrays.hashCode(values);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof Builder)) {
|
||||
return false;
|
||||
}
|
||||
Builder<K, V> other = (Builder<K, V>) obj;
|
||||
if (!keySet().equals(other.keySet())) {
|
||||
return false;
|
||||
}
|
||||
return Arrays.equals(values, other.values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Builder<K, V> o) {
|
||||
return keySet().compareTo(o.keySet());
|
||||
}
|
||||
}
|
||||
|
||||
public static class SizeAny<K, V> extends TinyMap<K, V> {
|
||||
|
|
|
@ -5,13 +5,16 @@ import static org.hamcrest.CoreMatchers.is;
|
|||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
@ -159,6 +162,64 @@ public class TinyMapTest {
|
|||
assertEquals(99999, map.get("aaa99999"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareKeys() {
|
||||
TinyMap.Builder<String, Object> builder1 = TinyMap.builder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
builder1.put("" + i, i);
|
||||
}
|
||||
TinyMap.Builder<String, Object> builder2 = TinyMap.builder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
builder2.put("" + i, i);
|
||||
}
|
||||
assertEquals(0, builder1.keySet().compareTo(builder2.keySet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompareBuilder() {
|
||||
TinyMap.Builder<String, Object> builder1 = TinyMap.builder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
builder1.put("" + i, i);
|
||||
}
|
||||
TinyMap.Builder<String, Object> builder2 = TinyMap.builder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
builder2.put("" + i, i);
|
||||
}
|
||||
assertEquals(0, builder1.compareTo(builder2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailCompareBuilder() {
|
||||
TinyMap.Builder<String, Object> builder1 = TinyMap.builder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
builder1.put("" + i, i);
|
||||
}
|
||||
TinyMap.Builder<String, Object> builder2 = TinyMap.builder();
|
||||
for (int i = 0; i < 101; i++) {
|
||||
builder2.put("" + i, i);
|
||||
}
|
||||
assertNotEquals(0, builder1.compareTo(builder2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortKeys() {
|
||||
TinyMap.Builder<String, Object> builder = TinyMap.builder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
builder.put("" + i, i);
|
||||
}
|
||||
assertEquals(100, builder.keySet().stream().sorted().count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSortEntries() {
|
||||
TinyMap.Builder<String, Object> builder = TinyMap.builder();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
builder.put("" + i, i);
|
||||
}
|
||||
Set<Map.Entry<String, Object>> set = new HashSet<>(builder.entrySet());
|
||||
assertEquals(100, set.size());
|
||||
}
|
||||
|
||||
private void testCount(int count, boolean withNull) {
|
||||
TinyMap.Builder<String, Object> builder = TinyMap.builder();
|
||||
LinkedHashMap<String, Object> expectedMap = new LinkedHashMap<>();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
group = org.xbib
|
||||
name = datastructures
|
||||
version = 0.0.1
|
||||
version = 0.0.2
|
||||
|
||||
gradle.wrapper.version = 6.6.1
|
||||
|
|
Loading…
Reference in a new issue