mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
CharArrayMap can now be sorted
This commit is contained in:
parent
40a775b158
commit
68a2d2ad5e
2 changed files with 94 additions and 17 deletions
|
@ -10,7 +10,10 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.core.parser.tests.ast2;
|
package org.eclipse.cdt.core.parser.tests.ast2;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
@ -162,23 +165,25 @@ public class CharArrayMapTest extends TestCase {
|
||||||
public void testBasicUsage2() {
|
public void testBasicUsage2() {
|
||||||
char[] chars = "pantera, megadeth, soulfly, metallica, in flames, lamb of god, carcass".toCharArray();
|
char[] chars = "pantera, megadeth, soulfly, metallica, in flames, lamb of god, carcass".toCharArray();
|
||||||
|
|
||||||
Slice[] slices = new Slice[7];
|
Slice[] slices = {
|
||||||
slices[0] = new Slice(chars, 0, 7);
|
new Slice(chars, 0, 7),
|
||||||
slices[1] = new Slice(chars, 9, 8);
|
new Slice(chars, 9, 8),
|
||||||
slices[2] = new Slice(chars, 19, 7);
|
new Slice(chars, 19, 7),
|
||||||
slices[3] = new Slice(chars, 28, 9);
|
new Slice(chars, 28, 9),
|
||||||
slices[4] = new Slice(chars, 39, 9);
|
new Slice(chars, 39, 9),
|
||||||
slices[5] = new Slice(chars, 50, 11);
|
new Slice(chars, 50, 11),
|
||||||
slices[6] = new Slice(chars, 63, 7);
|
new Slice(chars, 63, 7)
|
||||||
|
};
|
||||||
|
|
||||||
char[][] keys = new char[7][];
|
char[][] keys = {
|
||||||
keys[0] = "pantera".toCharArray();
|
"pantera".toCharArray(),
|
||||||
keys[1] = "megadeth".toCharArray();
|
"megadeth".toCharArray(),
|
||||||
keys[2] = "soulfly".toCharArray();
|
"soulfly".toCharArray(),
|
||||||
keys[3] = "metallica".toCharArray();
|
"metallica".toCharArray(),
|
||||||
keys[4] = "in flames".toCharArray();
|
"in flames".toCharArray(),
|
||||||
keys[5] = "lamb of god".toCharArray();
|
"lamb of god".toCharArray(),
|
||||||
keys[6] = "carcass".toCharArray();
|
"carcass".toCharArray()
|
||||||
|
};
|
||||||
|
|
||||||
CharArrayMap<Integer> map = new CharArrayMap<Integer>();
|
CharArrayMap<Integer> map = new CharArrayMap<Integer>();
|
||||||
assertTrue(map.isEmpty());
|
assertTrue(map.isEmpty());
|
||||||
|
@ -232,6 +237,48 @@ public class CharArrayMapTest extends TestCase {
|
||||||
assertEquals(0, map.size());
|
assertEquals(0, map.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testOrderedMap() {
|
||||||
|
char[] chars = "alpha beta aaa cappa almost".toCharArray();
|
||||||
|
Slice[] slices = {
|
||||||
|
new Slice(chars, 0, 5),
|
||||||
|
new Slice(chars, 6, 4),
|
||||||
|
new Slice(chars, 11, 3),
|
||||||
|
new Slice(chars, 15, 5),
|
||||||
|
new Slice(chars, 21, 6)
|
||||||
|
};
|
||||||
|
int[] order = {3, 4, 1, 5, 2};
|
||||||
|
|
||||||
|
CharArrayMap<Integer> map = CharArrayMap.createOrderedMap();
|
||||||
|
|
||||||
|
for(int i = 0; i < slices.length; i++) {
|
||||||
|
Slice slice = slices[i];
|
||||||
|
map.put(slice.chars, slice.start, slice.length, order[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> properOrder = Arrays.asList("aaa", "almost", "alpha", "beta", "cappa");
|
||||||
|
|
||||||
|
Collection<char[]> keys = map.keys();
|
||||||
|
assertEquals(5, keys.size());
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
for(char[] key : keys) {
|
||||||
|
assertEquals(properOrder.get(i), String.valueOf(key));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Collection<Integer> values = map.values();
|
||||||
|
assertEquals(5, values.size());
|
||||||
|
{
|
||||||
|
int i = 1;
|
||||||
|
for(int value : values) {
|
||||||
|
assertEquals(i++, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testProperFail() {
|
public void testProperFail() {
|
||||||
char[] hello = "hello".toCharArray();
|
char[] hello = "hello".toCharArray();
|
||||||
CharArrayMap<Integer> map = new CharArrayMap<Integer>();
|
CharArrayMap<Integer> map = new CharArrayMap<Integer>();
|
||||||
|
|
|
@ -15,6 +15,7 @@ import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -33,7 +34,7 @@ public final class CharArrayMap<V> implements ICharArrayMap<V> {
|
||||||
* This class is private so it is assumed that the arguments
|
* This class is private so it is assumed that the arguments
|
||||||
* passed to the constructor are legal.
|
* passed to the constructor are legal.
|
||||||
*/
|
*/
|
||||||
private static final class Key {
|
private static final class Key implements Comparable<Key>{
|
||||||
private final char[] buffer;
|
private final char[] buffer;
|
||||||
private final int start;
|
private final int start;
|
||||||
private final int length;
|
private final int length;
|
||||||
|
@ -87,6 +88,16 @@ public final class CharArrayMap<V> implements ICharArrayMap<V> {
|
||||||
return "'" + slice + "'@(" + start + "," + length + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
return "'" + slice + "'@(" + start + "," + length + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public int compareTo(Key other) {
|
||||||
|
char[] b1 = buffer, b2 = other.buffer;
|
||||||
|
|
||||||
|
for(int i = start, j = other.start; i < b1.length && j < b2.length; i++, j++) {
|
||||||
|
if(b1[i] != b2[j])
|
||||||
|
return b1[i] < b2[j] ? -1 : 1;
|
||||||
|
}
|
||||||
|
return b1.length - b2.length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -114,6 +125,25 @@ public final class CharArrayMap<V> implements ICharArrayMap<V> {
|
||||||
map = new HashMap<Key,V>();
|
map = new HashMap<Key,V>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static factory method that constructs an empty CharArrayMap with default initial capacity,
|
||||||
|
* and the map will be kept in ascending key order.
|
||||||
|
*
|
||||||
|
* Characters are compared using a strictly numerical comparison; it is not locale-dependent.
|
||||||
|
*/
|
||||||
|
public static <V> CharArrayMap<V> createOrderedMap() {
|
||||||
|
// TreeMap does not have a constructor that takes an initial capacity
|
||||||
|
return new CharArrayMap<V>(new TreeMap<Key, V>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private CharArrayMap(Map<Key, V> map) {
|
||||||
|
assert map != null;
|
||||||
|
this.map = map;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs an empty CharArrayMap with the given initial capacity.
|
* Constructs an empty CharArrayMap with the given initial capacity.
|
||||||
* @throws IllegalArgumentException if the initial capacity is negative
|
* @throws IllegalArgumentException if the initial capacity is negative
|
||||||
|
|
Loading…
Add table
Reference in a new issue