2 * Copyright (C) 2009, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.transport
;
41 * Simple Map<long,Object> helper for {@link IndexPack}.
44 * type of the value instance.
46 final class LongMap
<V
> {
47 private static final float LOAD_FACTOR
= 0.75f
;
49 private Node
<V
>[] table
;
51 /** Number of entries currently in the map. */
54 /** Next {@link #size} to trigger a {@link #grow()}. */
58 table
= createArray(64);
59 growAt
= (int) (table
.length
* LOAD_FACTOR
);
62 boolean containsKey(final long key
) {
63 return get(key
) != null;
66 V
get(final long key
) {
67 for (Node
<V
> n
= table
[index(key
)]; n
!= null; n
= n
.next
) {
74 V
remove(final long key
) {
75 Node
<V
> n
= table
[index(key
)];
80 table
[index(key
)] = n
.next
;
92 V
put(final long key
, final V value
) {
93 for (Node
<V
> n
= table
[index(key
)]; n
!= null; n
= n
.next
) {
101 if (++size
== growAt
)
103 insert(new Node
<V
>(key
, value
));
107 private void insert(final Node
<V
> n
) {
108 final int idx
= index(n
.key
);
113 private void grow() {
114 final Node
<V
>[] oldTable
= table
;
115 final int oldSize
= table
.length
;
117 table
= createArray(oldSize
<< 1);
118 growAt
= (int) (table
.length
* LOAD_FACTOR
);
119 for (int i
= 0; i
< oldSize
; i
++) {
120 Node
<V
> e
= oldTable
[i
];
122 final Node
<V
> n
= e
.next
;
129 private final int index(final long key
) {
130 int h
= ((int) key
) >>> 1;
131 h ^
= (h
>>> 20) ^
(h
>>> 12);
132 return h
& (table
.length
- 1);
135 @SuppressWarnings("unchecked")
136 private static final <V
> Node
<V
>[] createArray(final int sz
) {
140 private static class Node
<V
> {
147 Node(final long k
, final V v
) {