2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
/// Fast, efficient map specifically for
52
/// <see cref="ObjectId">ObjectId</see>
55
/// This map provides an efficient translation from any ObjectId instance to a
56
/// cached subclass of ObjectId that has the same value.
58
/// If object instances are stored in only one map,
59
/// <see cref="ObjectIdOwnerMap{V}">ObjectIdOwnerMap<V></see>
61
/// more efficient implementation.
64
public class ObjectIdSubclassMap<V> : Iterable<V> where V:ObjectId
66
private const int INITIAL_TABLE_SIZE = 2048;
76
/// <summary>Create an empty map.</summary>
77
/// <remarks>Create an empty map.</remarks>
78
public ObjectIdSubclassMap()
80
InitTable(INITIAL_TABLE_SIZE);
83
/// <summary>Remove all entries from this map.</summary>
84
/// <remarks>Remove all entries from this map.</remarks>
85
public virtual void Clear()
88
InitTable(INITIAL_TABLE_SIZE);
91
/// <summary>Lookup an existing mapping.</summary>
92
/// <remarks>Lookup an existing mapping.</remarks>
93
/// <param name="toFind">the object identifier to find.</param>
94
/// <returns>the instance mapped to toFind, or null if no mapping exists.</returns>
95
public virtual V Get(AnyObjectId toFind)
98
int i = toFind.w1 & msk;
101
while ((obj = tbl[i]) != null)
103
if (AnyObjectId.Equals(obj, toFind))
112
/// <summary>Returns true if this map contains the specified object.</summary>
113
/// <remarks>Returns true if this map contains the specified object.</remarks>
114
/// <param name="toFind">object to find.</param>
115
/// <returns>true if the mapping exists for this object; false otherwise.</returns>
116
public virtual bool Contains(AnyObjectId toFind)
118
return Get(toFind) != null;
121
/// <summary>Store an object for future lookup.</summary>
123
/// Store an object for future lookup.
125
/// An existing mapping for <b>must not</b> be in this map. Callers must
127
/// <see cref="ObjectIdSubclassMap{V}.Get(AnyObjectId)">ObjectIdSubclassMap<V>.Get(AnyObjectId)
129
/// to verify there is no current
130
/// mapping prior to adding a new mapping, or use
131
/// <see cref="ObjectIdSubclassMap{V}.AddIfAbsent{Q}(ObjectId)">ObjectIdSubclassMap<V>.AddIfAbsent<Q>(ObjectId)
135
/// <param name="newValue">the object to store.</param>
137
public virtual void Add<Q>(Q newValue) where Q:V
146
/// <summary>Store an object for future lookup.</summary>
148
/// Store an object for future lookup.
151
/// <code>newValue</code>
152
/// , but only if there is not already an object for
153
/// the same object name. Callers can tell if the value is new by checking
154
/// the return value with reference equality:
157
/// boolean wasNew = map.addIfAbsent(obj) == obj;
160
/// <param name="newValue">the object to store.</param>
163
/// <code>newValue</code>
164
/// if stored, or the prior value already stored and
165
/// that would have been returned had the caller used
166
/// <code>get(newValue)</code>
170
public virtual V AddIfAbsent<Q>(Q newValue) where Q:V
173
int i = ((ObjectId)newValue).w1 & msk;
176
while ((obj = tbl[i]) != null)
178
if (AnyObjectId.Equals(obj, newValue))
196
/// <returns>number of objects in map</returns>
197
public virtual int Size()
204
/// <see cref="ObjectIdSubclassMap{V}.Size()">ObjectIdSubclassMap<V>.Size()</see>
207
public virtual bool IsEmpty()
212
public override Sharpen.Iterator<V> Iterator()
214
return new _Iterator_190(this);
217
private sealed class _Iterator_190 : Sharpen.Iterator<V>
219
public _Iterator_190(ObjectIdSubclassMap<V> _enclosing)
221
this._enclosing = _enclosing;
228
public override bool HasNext()
230
return this.found < this._enclosing.size;
233
public override V Next()
235
while (this.i < this._enclosing.table.Length)
237
V v = this._enclosing.table[this.i++];
244
throw new NoSuchElementException();
247
public override void Remove()
249
throw new NotSupportedException();
252
private readonly ObjectIdSubclassMap<V> _enclosing;
255
private void Insert(V newValue)
258
int j = newValue.w1 & msk;
260
while (tbl[j] != null)
269
V[] oldTable = table;
270
int oldSize = table.Length;
271
InitTable(oldSize << 1);
272
for (int i = 0; i < oldSize; i++)
282
private void InitTable(int sz)
286
table = CreateArray(sz);
289
private V[] CreateArray(int sz)