You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectIdSubclassMap.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.lib;
  46. import java.util.Iterator;
  47. import java.util.NoSuchElementException;
  48. /**
  49. * Fast, efficient map specifically for {@link ObjectId} subclasses.
  50. * <p>
  51. * This map provides an efficient translation from any ObjectId instance to a
  52. * cached subclass of ObjectId that has the same value.
  53. * <p>
  54. * If object instances are stored in only one map, {@link ObjectIdOwnerMap} is a
  55. * more efficient implementation.
  56. *
  57. * @param <V>
  58. * type of subclass of ObjectId that will be stored in the map.
  59. */
  60. public class ObjectIdSubclassMap<V extends ObjectId>
  61. implements Iterable<V>, ObjectIdSet {
  62. private static final int INITIAL_TABLE_SIZE = 2048;
  63. int size;
  64. private int grow;
  65. private int mask;
  66. V[] table;
  67. /** Create an empty map. */
  68. public ObjectIdSubclassMap() {
  69. initTable(INITIAL_TABLE_SIZE);
  70. }
  71. /** Remove all entries from this map. */
  72. public void clear() {
  73. size = 0;
  74. initTable(INITIAL_TABLE_SIZE);
  75. }
  76. /**
  77. * Lookup an existing mapping.
  78. *
  79. * @param toFind
  80. * the object identifier to find.
  81. * @return the instance mapped to toFind, or null if no mapping exists.
  82. */
  83. public V get(final AnyObjectId toFind) {
  84. final int msk = mask;
  85. int i = toFind.w1 & msk;
  86. final V[] tbl = table;
  87. V obj;
  88. while ((obj = tbl[i]) != null) {
  89. if (AnyObjectId.equals(obj, toFind))
  90. return obj;
  91. i = (i + 1) & msk;
  92. }
  93. return null;
  94. }
  95. /**
  96. * Returns true if this map contains the specified object.
  97. *
  98. * @param toFind
  99. * object to find.
  100. * @return true if the mapping exists for this object; false otherwise.
  101. */
  102. public boolean contains(final AnyObjectId toFind) {
  103. return get(toFind) != null;
  104. }
  105. /**
  106. * Store an object for future lookup.
  107. * <p>
  108. * An existing mapping for <b>must not</b> be in this map. Callers must
  109. * first call {@link #get(AnyObjectId)} to verify there is no current
  110. * mapping prior to adding a new mapping, or use
  111. * {@link #addIfAbsent(ObjectId)}.
  112. *
  113. * @param newValue
  114. * the object to store.
  115. * @param <Q>
  116. * type of instance to store.
  117. */
  118. public <Q extends V> void add(final Q newValue) {
  119. if (++size == grow)
  120. grow();
  121. insert(newValue);
  122. }
  123. /**
  124. * Store an object for future lookup.
  125. * <p>
  126. * Stores {@code newValue}, but only if there is not already an object for
  127. * the same object name. Callers can tell if the value is new by checking
  128. * the return value with reference equality:
  129. *
  130. * <pre>
  131. * V obj = ...;
  132. * boolean wasNew = map.addIfAbsent(obj) == obj;
  133. * </pre>
  134. *
  135. * @param newValue
  136. * the object to store.
  137. * @return {@code newValue} if stored, or the prior value already stored and
  138. * that would have been returned had the caller used
  139. * {@code get(newValue)} first.
  140. * @param <Q>
  141. * type of instance to store.
  142. */
  143. public <Q extends V> V addIfAbsent(final Q newValue) {
  144. final int msk = mask;
  145. int i = newValue.w1 & msk;
  146. final V[] tbl = table;
  147. V obj;
  148. while ((obj = tbl[i]) != null) {
  149. if (AnyObjectId.equals(obj, newValue))
  150. return obj;
  151. i = (i + 1) & msk;
  152. }
  153. if (++size == grow) {
  154. grow();
  155. insert(newValue);
  156. } else {
  157. tbl[i] = newValue;
  158. }
  159. return newValue;
  160. }
  161. /**
  162. * @return number of objects in map
  163. */
  164. public int size() {
  165. return size;
  166. }
  167. /** @return true if {@link #size()} is 0. */
  168. public boolean isEmpty() {
  169. return size == 0;
  170. }
  171. public Iterator<V> iterator() {
  172. return new Iterator<V>() {
  173. private int found;
  174. private int i;
  175. public boolean hasNext() {
  176. return found < size;
  177. }
  178. public V next() {
  179. while (i < table.length) {
  180. final V v = table[i++];
  181. if (v != null) {
  182. found++;
  183. return v;
  184. }
  185. }
  186. throw new NoSuchElementException();
  187. }
  188. public void remove() {
  189. throw new UnsupportedOperationException();
  190. }
  191. };
  192. }
  193. private void insert(final V newValue) {
  194. final int msk = mask;
  195. int j = newValue.w1 & msk;
  196. final V[] tbl = table;
  197. while (tbl[j] != null)
  198. j = (j + 1) & msk;
  199. tbl[j] = newValue;
  200. }
  201. private void grow() {
  202. final V[] oldTable = table;
  203. final int oldSize = table.length;
  204. initTable(oldSize << 1);
  205. for (int i = 0; i < oldSize; i++) {
  206. final V obj = oldTable[i];
  207. if (obj != null)
  208. insert(obj);
  209. }
  210. }
  211. private void initTable(int sz) {
  212. grow = sz >> 1;
  213. mask = sz - 1;
  214. table = createArray(sz);
  215. }
  216. @SuppressWarnings("unchecked")
  217. private final V[] createArray(final int sz) {
  218. return (V[]) new ObjectId[sz];
  219. }
  220. }