|
| 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 | +} |
0 commit comments