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

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