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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 org.eclipse.jgit.lib.ObjectId}
  50. * subclasses.
  51. * <p>
  52. * This map provides an efficient translation from any ObjectId instance to a
  53. * cached subclass of ObjectId that has the same value.
  54. * <p>
  55. * If object instances are stored in only one map,
  56. * {@link org.eclipse.jgit.lib.ObjectIdOwnerMap} is a more efficient
  57. * implementation.
  58. *
  59. * @param <V>
  60. * type of subclass of ObjectId that will be stored in the map.
  61. */
  62. public class ObjectIdSubclassMap<V extends ObjectId>
  63. implements Iterable<V>, ObjectIdSet {
  64. private static final int INITIAL_TABLE_SIZE = 2048;
  65. int size;
  66. private int grow;
  67. private int mask;
  68. V[] table;
  69. /**
  70. * Create an empty map.
  71. */
  72. public ObjectIdSubclassMap() {
  73. initTable(INITIAL_TABLE_SIZE);
  74. }
  75. /**
  76. * Remove all entries from this map.
  77. */
  78. public void clear() {
  79. size = 0;
  80. initTable(INITIAL_TABLE_SIZE);
  81. }
  82. /**
  83. * Lookup an existing mapping.
  84. *
  85. * @param toFind
  86. * the object identifier to find.
  87. * @return the instance mapped to toFind, or null if no mapping exists.
  88. */
  89. public V get(AnyObjectId toFind) {
  90. final int msk = mask;
  91. int i = toFind.w1 & msk;
  92. final V[] tbl = table;
  93. V obj;
  94. while ((obj = tbl[i]) != null) {
  95. if (AnyObjectId.equals(obj, toFind))
  96. return obj;
  97. i = (i + 1) & msk;
  98. }
  99. return null;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. * <p>
  104. * Returns true if this map contains the specified object.
  105. */
  106. @Override
  107. public boolean contains(AnyObjectId toFind) {
  108. return get(toFind) != null;
  109. }
  110. /**
  111. * Store an object for future lookup.
  112. * <p>
  113. * An existing mapping for <b>must not</b> be in this map. Callers must
  114. * first call {@link #get(AnyObjectId)} to verify there is no current
  115. * mapping prior to adding a new mapping, or use
  116. * {@link #addIfAbsent(ObjectId)}.
  117. *
  118. * @param newValue
  119. * the object to store.
  120. */
  121. public <Q extends V> void add(Q newValue) {
  122. if (++size == grow)
  123. grow();
  124. insert(newValue);
  125. }
  126. /**
  127. * Store an object for future lookup.
  128. * <p>
  129. * Stores {@code newValue}, but only if there is not already an object for
  130. * the same object name. Callers can tell if the value is new by checking
  131. * the return value with reference equality:
  132. *
  133. * <pre>
  134. * V obj = ...;
  135. * boolean wasNew = map.addIfAbsent(obj) == obj;
  136. * </pre>
  137. *
  138. * @param newValue
  139. * the object to store.
  140. * @return {@code newValue} if stored, or the prior value already stored and
  141. * that would have been returned had the caller used
  142. * {@code get(newValue)} first.
  143. */
  144. public <Q extends V> V addIfAbsent(Q newValue) {
  145. final int msk = mask;
  146. int i = newValue.w1 & msk;
  147. final V[] tbl = table;
  148. V obj;
  149. while ((obj = tbl[i]) != null) {
  150. if (AnyObjectId.equals(obj, newValue))
  151. return obj;
  152. i = (i + 1) & msk;
  153. }
  154. if (++size == grow) {
  155. grow();
  156. insert(newValue);
  157. } else {
  158. tbl[i] = newValue;
  159. }
  160. return newValue;
  161. }
  162. /**
  163. * Get number of objects in map
  164. *
  165. * @return number of objects in map
  166. */
  167. public int size() {
  168. return size;
  169. }
  170. /**
  171. * Whether {@link #size()} is 0.
  172. *
  173. * @return true if {@link #size()} is 0.
  174. */
  175. public boolean isEmpty() {
  176. return size == 0;
  177. }
  178. /** {@inheritDoc} */
  179. @Override
  180. public Iterator<V> iterator() {
  181. return new Iterator<V>() {
  182. private int found;
  183. private int i;
  184. @Override
  185. public boolean hasNext() {
  186. return found < size;
  187. }
  188. @Override
  189. public V next() {
  190. while (i < table.length) {
  191. final V v = table[i++];
  192. if (v != null) {
  193. found++;
  194. return v;
  195. }
  196. }
  197. throw new NoSuchElementException();
  198. }
  199. @Override
  200. public void remove() {
  201. throw new UnsupportedOperationException();
  202. }
  203. };
  204. }
  205. private void insert(V newValue) {
  206. final int msk = mask;
  207. int j = newValue.w1 & msk;
  208. final V[] tbl = table;
  209. while (tbl[j] != null)
  210. j = (j + 1) & msk;
  211. tbl[j] = newValue;
  212. }
  213. private void grow() {
  214. final V[] oldTable = table;
  215. final int oldSize = table.length;
  216. initTable(oldSize << 1);
  217. for (int i = 0; i < oldSize; i++) {
  218. final V obj = oldTable[i];
  219. if (obj != null)
  220. insert(obj);
  221. }
  222. }
  223. private void initTable(int sz) {
  224. grow = sz >> 1;
  225. mask = sz - 1;
  226. table = createArray(sz);
  227. }
  228. @SuppressWarnings("unchecked")
  229. private final V[] createArray(int sz) {
  230. return (V[]) new ObjectId[sz];
  231. }
  232. }