Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cddb96333c3ce8bb7791bdebb5d386cf92d41ce6 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.registry;

/**
 * Essentially a map String -> String[] for small number of keys.
 *
 * For Maps containing a small number of objects hashing often reduces performance.
 * This implementation uses two parallel arrays, one for keys, one for
 * values, and grows them as necessary.
 */
public class DirectMap {

	final private float growthFactor;

	private String[] keyArray;
	private String[][] valueArray;
	private int size;

	public DirectMap(int initialSize, float growthFactor) {
		if (initialSize < 1)
			throw new IllegalArgumentException();
		if (growthFactor <= 0)
			throw new IllegalArgumentException();
		this.growthFactor = growthFactor;
		keyArray = new String[initialSize];
		valueArray = new String[initialSize][];
		size = 0;
	}

	public synchronized void put(String key, String[] value) {
		if (key == null)
			throw new IllegalArgumentException();
		int id = findKey(key);
		if (id != -1)
			throw new IllegalArgumentException();

		if (size >= keyArray.length) { // need to resize
			int newSize = recalcSize(keyArray.length);
			if (newSize <= size)
				newSize = size + 1;

			String[] newKeyArray = new String[newSize];
			System.arraycopy(keyArray, 0, newKeyArray, 0, keyArray.length);
			keyArray = newKeyArray;

			String[][] newValueArray = new String[newSize][];
			System.arraycopy(valueArray, 0, newValueArray, 0, valueArray.length);
			valueArray = newValueArray;
		}
		keyArray[size] = key;
		valueArray[size] = value;
		size++;
	}

	public synchronized boolean containsKey(String key) {
		if (key == null)
			throw new IllegalArgumentException();
		return (findKey(key) != -1);
	}

	public synchronized String[] get(String key) {
		if (key == null)
			throw new IllegalArgumentException();
		int id = findKey(key);
		if (id == -1)
			return null;
		return valueArray[id];
	}

	/* package */String[] getKeys() {
		return keyArray;
	}

	/* package */String[][] getValues() {
		return valueArray;
	}

	/* package */int getSzie() {
		return size;
	}

	private int recalcSize(int currentSize) {
		return (int) (currentSize * (1.0f + growthFactor));
	}

	private int findKey(String key) {
		for (int i = 0; i < keyArray.length; i++) {
			if (keyArray[i] == null)
				continue;
			if (keyArray[i].equals(key))
				return i;
		}
		return -1;
	}
}

Back to the top