Skip to content

Commit 06f1b8d

Browse files
Merge pull request #15 from leostrakosch/orderstatisticmap
OrderStatisticMap/Set
2 parents 4cd2382 + 5de3d70 commit 06f1b8d

8 files changed

+2127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*
2+
* SoSy-Lab Common is a library of useful utilities.
3+
* This file is part of SoSy-Lab Common.
4+
*
5+
* Copyright (C) 2007-2017 Dirk Beyer
6+
* All rights reserved.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package org.sosy_lab.common.collect;
21+
22+
import static com.google.common.base.Preconditions.checkNotNull;
23+
24+
import com.google.common.collect.ForwardingNavigableMap;
25+
import com.google.common.collect.Iterables;
26+
import com.google.common.collect.Maps;
27+
import com.google.errorprone.annotations.CanIgnoreReturnValue;
28+
import java.io.Serializable;
29+
import java.util.Comparator;
30+
import java.util.Map;
31+
import java.util.NavigableMap;
32+
import java.util.SortedMap;
33+
import java.util.TreeMap;
34+
35+
/**
36+
* An {@link OrderStatisticMap} with naive implementations of its functions.
37+
*
38+
* <p>The class wraps a {@link NavigableMap} object and delegates all methods inherited from the
39+
* <code>NavigableMap</code> interface to that. For the methods particular to the <code>
40+
* OrderStatisticMap</code> interface, it provides naive implementations that do guarantee
41+
* performance only in O(n).
42+
*
43+
* @param <K> type of the keys of this map. See the Javadoc of {@link OrderStatisticMap} for
44+
* possible constraints on this type
45+
* @param <V> type of the values of this map
46+
* @see OrderStatisticMap
47+
*/
48+
final class NaiveOrderStatisticMap<K, V> extends ForwardingNavigableMap<K, V>
49+
implements OrderStatisticMap<K, V>, Serializable {
50+
51+
private static final long serialVersionUID = -3542217590830996599L;
52+
53+
private final NavigableMap<K, V> delegate;
54+
55+
private NaiveOrderStatisticMap(NavigableMap<K, V> pNavigableMap) {
56+
delegate = pNavigableMap;
57+
}
58+
59+
/** Creates a new empty OrderStatisticMap using natural ordering. */
60+
static <K, V> NaiveOrderStatisticMap<K, V> createMap() {
61+
return new NaiveOrderStatisticMap<>(new TreeMap<>());
62+
}
63+
64+
/** Creates a new empty OrderStatisticMap using the given comparator over its keys. */
65+
static <K, V> NaiveOrderStatisticMap<K, V> createMap(Comparator<? super K> pComparator) {
66+
return new NaiveOrderStatisticMap<>(new TreeMap<>(checkNotNull(pComparator)));
67+
}
68+
69+
/**
70+
* Creates a new OrderStatisticSet containing the same entries as the given map, using natural
71+
* ordering over its keys.
72+
*/
73+
static <K, V> NaiveOrderStatisticMap<K, V> createMapWithNaturalOrder(
74+
Map<? extends K, ? extends V> pMap) {
75+
return new NaiveOrderStatisticMap<>(new TreeMap<>(checkNotNull(pMap)));
76+
}
77+
78+
/**
79+
* Creates a new OrderStatisticMap containing the sames entries as the given map and using the
80+
* same ordering over its keys as the given map.
81+
*/
82+
static <K, V> NaiveOrderStatisticMap<K, V> createMapWithSameOrder(
83+
SortedMap<K, ? extends V> pSortedMap) {
84+
return new NaiveOrderStatisticMap<>(new TreeMap<>(checkNotNull(pSortedMap)));
85+
}
86+
87+
@Override
88+
protected NavigableMap<K, V> delegate() {
89+
return delegate;
90+
}
91+
92+
@Override
93+
public K getKeyByRank(int pIndex) {
94+
return Iterables.get(delegate.navigableKeySet(), pIndex);
95+
}
96+
97+
@Override
98+
public Entry<K, V> getEntryByRank(int pIndex) {
99+
K key = getKeyByRank(pIndex);
100+
return Maps.immutableEntry(key, get(key));
101+
}
102+
103+
@Override
104+
@CanIgnoreReturnValue
105+
public K removeByRank(int pIndex) {
106+
K key = getKeyByRank(pIndex);
107+
V val = remove(key);
108+
assert val != null : "Key could be retrieved by rank, but no (or null) value associated";
109+
return key;
110+
}
111+
112+
@Override
113+
public int rankOf(K pObj) {
114+
checkNotNull(pObj);
115+
return Iterables.indexOf(delegate.navigableKeySet(), o -> compareKey(o, pObj) == 0);
116+
}
117+
118+
@SuppressWarnings("unchecked")
119+
private int compareKey(K pFirst, K pSnd) {
120+
Comparator<? super K> comparator = comparator();
121+
if (comparator != null) {
122+
return comparator.compare(pFirst, pSnd);
123+
} else {
124+
return ((Comparable<K>) pFirst).compareTo(pSnd);
125+
}
126+
}
127+
128+
@Override
129+
public OrderStatisticSet<K> navigableKeySet() {
130+
return NaiveOrderStatisticSet.createView(super.navigableKeySet());
131+
}
132+
133+
@Override
134+
public OrderStatisticSet<K> descendingKeySet() {
135+
return NaiveOrderStatisticSet.createView(super.descendingKeySet());
136+
}
137+
138+
@Override
139+
public OrderStatisticMap<K, V> descendingMap() {
140+
return new NaiveOrderStatisticMap<>(super.descendingMap());
141+
}
142+
143+
@Override
144+
public OrderStatisticMap<K, V> subMap(
145+
K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
146+
return new NaiveOrderStatisticMap<>(super.subMap(fromKey, fromInclusive, toKey, toInclusive));
147+
}
148+
149+
@Override
150+
public OrderStatisticMap<K, V> headMap(K toKey, boolean inclusive) {
151+
return new NaiveOrderStatisticMap<>(super.headMap(toKey, inclusive));
152+
}
153+
154+
@Override
155+
public OrderStatisticMap<K, V> tailMap(K fromKey, boolean inclusive) {
156+
return new NaiveOrderStatisticMap<>(super.tailMap(fromKey, inclusive));
157+
}
158+
159+
@Override
160+
public OrderStatisticMap<K, V> headMap(K toKey) {
161+
return headMap(toKey, /* inclusive= */ false);
162+
}
163+
164+
@Override
165+
public OrderStatisticMap<K, V> subMap(K fromKey, K toKey) {
166+
return subMap(fromKey, /* fromInclusive= */ true, toKey, /* toInclusive= */ false);
167+
}
168+
169+
@Override
170+
public OrderStatisticMap<K, V> tailMap(K fromKey) {
171+
return tailMap(fromKey, /* inclusive= */ true);
172+
}
173+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* SoSy-Lab Common is a library of useful utilities.
3+
* This file is part of SoSy-Lab Common.
4+
*
5+
* Copyright (C) 2007-2017 Dirk Beyer
6+
* All rights reserved.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package org.sosy_lab.common.collect;
21+
22+
import static com.google.common.truth.Truth.assertThat;
23+
24+
import com.google.common.collect.ImmutableMap;
25+
import com.google.common.collect.testing.NavigableMapTestSuiteBuilder;
26+
import com.google.common.collect.testing.TestSortedMapGenerator;
27+
import com.google.common.collect.testing.features.CollectionFeature;
28+
import com.google.common.collect.testing.features.CollectionSize;
29+
import com.google.common.collect.testing.features.MapFeature;
30+
import java.util.Arrays;
31+
import java.util.List;
32+
import java.util.Map.Entry;
33+
import java.util.NavigableMap;
34+
import java.util.TreeMap;
35+
import junit.framework.JUnit4TestAdapter;
36+
import junit.framework.TestSuite;
37+
import org.junit.Test;
38+
39+
public class NaiveOrderStatisticMapTest extends OrderStatisticMapTestSuite {
40+
41+
private static class OrderStatisticMapProxyFactory extends OrderStatisticMapFactory {
42+
43+
@Override
44+
protected OrderStatisticMap<String, String> create(Entry<String, String>[] pEntries) {
45+
return create(Arrays.asList(pEntries));
46+
}
47+
48+
@Override
49+
protected OrderStatisticMap<String, String> create(List<Entry<String, String>> pEntries) {
50+
NaiveOrderStatisticMap<String, String> map = createMap();
51+
for (Entry<String, String> e : pEntries) {
52+
map.put(e.getKey(), e.getValue());
53+
}
54+
return map;
55+
}
56+
57+
private static <K, V> NaiveOrderStatisticMap<K, V> createMap() {
58+
return NaiveOrderStatisticMap.createMap();
59+
}
60+
}
61+
62+
public NaiveOrderStatisticMapTest() {
63+
super(new OrderStatisticMapProxyFactory());
64+
}
65+
66+
public static junit.framework.Test suite() {
67+
TestSortedMapGenerator<String, String> testSetGenerator = new OrderStatisticMapProxyFactory();
68+
69+
TestSuite suite =
70+
NavigableMapTestSuiteBuilder.using(testSetGenerator)
71+
.named("NaiveOrderStatisticMap")
72+
.withFeatures(
73+
CollectionSize.ANY,
74+
CollectionFeature.KNOWN_ORDER,
75+
CollectionFeature.SERIALIZABLE,
76+
CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
77+
CollectionFeature.SUBSET_VIEW,
78+
CollectionFeature.DESCENDING_VIEW,
79+
MapFeature.GENERAL_PURPOSE,
80+
MapFeature.SUPPORTS_REMOVE,
81+
MapFeature.SUPPORTS_PUT,
82+
MapFeature.ALLOWS_NULL_VALUES)
83+
.createTestSuite();
84+
85+
suite.addTest(new JUnit4TestAdapter(NaiveOrderStatisticMapTest.class));
86+
87+
return suite;
88+
}
89+
90+
@Test
91+
public void testNoReference() {
92+
NavigableMap<String, String> testMap =
93+
new TreeMap<>(ImmutableMap.of("a", "Va", "b", "Vb", "bc", "Vbc", "d", "Vd"));
94+
OrderStatisticMap<String, String> map = NaiveOrderStatisticMap.createMapWithSameOrder(testMap);
95+
96+
testMap.remove("a");
97+
assertThat(testMap).doesNotContainKey("a");
98+
assertThat(map).containsKey("a");
99+
map.remove("bc");
100+
assertThat(testMap).containsKey("bc");
101+
assertThat(map).doesNotContainKey("bc");
102+
}
103+
}

0 commit comments

Comments
 (0)