Debian Codegraph @

net/sf/saxon/z/IntToIntArrayMap.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.z;
9
10 /**
11 * An implementation of {@link IntToIntMap} that relies on serial searching, and
12 * is therefore optimized for very small map sizes
13 */
14 public class IntToIntArrayMap implements IntToIntMap {
15
16 private int[] keys;
17 private int[] values;
18 private int used = 0;
19 private int defaultValue = Integer.MIN_VALUE;
20
21 /**
22 * Create an initial empty map with default space allocation
23 */
24
25 public IntToIntArrayMap() {
26 keys = new int[8];
27 values = new int[8];
28 }
29
30 /**
31 * Create an initial empty map with a specified initial capacity
32 *
33 * @param capacity the initial capacity (the number of entries that can be held
34 * before more space is allocated)
35 */
36
37 public IntToIntArrayMap(int capacity) {
38 if (capacity <= 0) {
39 throw new IllegalArgumentException("capacity <= 0");
40 }
41 keys = new int[capacity];
42 values = new int[capacity];
43 }
44
45 /**
46 * Clear the map.
47 */
48 public void clear() {
49 used = 0;
50 }
51
52 /**
53 * Finds a key in the map.
54 *
55 * @param key Key
56 * @return true if the key is mapped
57 */
58 public boolean find(int key) {
59 for (int i = 0; i < used; i++) {
60 if (keys[i] == key) {
61 return true;
62 }
63 }
64 return false;
65 }
66
67 /**
68 * Gets the value for this key.
69 *
70 * @param key Key
71 * @return the value, or the default value if not found.
72 */
73 public int get(int key) {
74 for (int i = 0; i < used; i++) {
75 if (keys[i] == key) {
76 return values[i];
77 }
78 }
79 return defaultValue;
80 }
81
82 /**
83 * Get the default value used to indicate an unused entry
84 *
85 * @return the value to be returned by {@link #get(int)} if no entry
86 * exists for the supplied key
87 */
88
89 public int getDefaultValue() {
90 return defaultValue;
91 }
92
93 /**
94 * Get an iterator over the integer key values held in the hash map.
95 * <p>The contents of the hash map must not be modified while this iterator remains in use</p>
96 *
97 * @return an iterator whose next() call returns the key values (in arbitrary order)
98 */
99
100 /*@NotNull*/
101 public IntIterator keyIterator() {
102 return new KeyIterator();
103 }
104
105 /**
106 * Adds a key-value pair to the map.
107 *
108 * @param key Key
109 * @param value Value
110 */
111 public void put(int key, int value) {
112 for (int i = 0; i < used; i++) {
113 if (keys[i] == key) {
114 values[i] = value;
115 return;
116 }
117 }
118 if (used >= keys.length) {
119 int[] k2 = new int[used * 2];
120 System.arraycopy(keys, 0, k2, 0, used);
121 keys = k2;
122 int[] v2 = new int[used * 2];
123 System.arraycopy(values, 0, v2, 0, used);
124 values = v2;
125 }
126 keys[used] = key;
127 values[used++] = value;
128 }
129
130 /**
131 * Removes a key from the map.
132 *
133 * @param key Key to remove
134 * @return true if the value was removed
135 */
136 public boolean remove(int key) {
137 for (int i = 0; i < used; i++) {
138 if (keys[i] == key) {
139 values[i] = defaultValue;
140 return true;
141 }
142 }
143 return false;
144 }
145
146 /**
147 * Set the value to be returned to indicate an unused entry
148 *
149 * @param defaultValue the value to be returned by {@link #get(int)} if no entry
150 * exists for the supplied key
151 */
152 public void setDefaultValue(int defaultValue) {
153 this.defaultValue = defaultValue;
154 }
155
156 /**
157 * Gets the size of the map.
158 *
159 * @return the size
160 */
161 public int size() {
162 return used;
163 }
164
165 private class KeyIterator implements IntIterator {
166
167 private int i = 0;
168 private static final long serialVersionUID = 1720894017771245276L;
169
170 public KeyIterator() {
171 i = 0;
172 }
173
174 public boolean hasNext() {
175 return i < used;
176 }
177
178 public int next() {
179 return keys[i++];
180 }
181 }
182 }
183