blob: 31211e243ab76471858a7ab02aa71be86cd185b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*
* Copyright (C) 2009, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.util;
/**
* Simple Map<long, Object>.
*
* @param <V>
* type of the value instance.
* @since 4.9
*/
public class LongMap<V> {
private static final float LOAD_FACTOR = 0.75f;
private Node<V>[] table;
/** Number of entries currently in the map. */
private int size;
/** Next {@link #size} to trigger a {@link #grow()}. */
private int growAt;
/**
* Initialize an empty LongMap.
*/
public LongMap() {
table = createArray(64);
growAt = (int) (table.length * LOAD_FACTOR);
}
/**
* Whether {@code key} is present in the map.
*
* @param key
* the key to find.
* @return {@code true} if {@code key} is present in the map.
*/
public boolean containsKey(long key) {
return get(key) != null;
}
/**
* Get value for this {@code key}
*
* @param key
* the key to find.
* @return stored value for this key, or {@code null}.
*/
public V get(long key) {
for (Node<V> n = table[index(key)]; n != null; n = n.next) {
if (n.key == key)
return n.value;
}
return null;
}
/**
* Remove an entry from the map
*
* @param key
* key to remove from the map.
* @return old value of the key, or {@code null}.
*/
public V remove(long key) {
Node<V> n = table[index(key)];
Node<V> prior = null;
while (n != null) {
if (n.key == key) {
if (prior == null)
table[index(key)] = n.next;
else
prior.next = n.next;
size--;
return n.value;
}
prior = n;
n = n.next;
}
return null;
}
/**
* Put a new entry into the map
*
* @param key
* key to store {@code value} under.
* @param value
* new value.
* @return prior value, or null.
*/
public V put(long key, V value) {
for (Node<V> n = table[index(key)]; n != null; n = n.next) {
if (n.key == key) {
final V o = n.value;
n.value = value;
return o;
}
}
if (++size == growAt)
grow();
insert(new Node<>(key, value));
return null;
}
private void insert(Node<V> n) {
final int idx = index(n.key);
n.next = table[idx];
table[idx] = n;
}
private void grow() {
final Node<V>[] oldTable = table;
final int oldSize = table.length;
table = createArray(oldSize << 1);
growAt = (int) (table.length * LOAD_FACTOR);
for (int i = 0; i < oldSize; i++) {
Node<V> e = oldTable[i];
while (e != null) {
final Node<V> n = e.next;
insert(e);
e = n;
}
}
}
private final int index(long key) {
int h = ((int) key) >>> 1;
h ^= (h >>> 20) ^ (h >>> 12);
return h & (table.length - 1);
}
@SuppressWarnings("unchecked")
private static final <V> Node<V>[] createArray(int sz) {
return new Node[sz];
}
private static class Node<V> {
final long key;
V value;
Node<V> next;
Node(long k, V v) {
key = k;
value = v;
}
}
}
|