Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d2be1e64c8a40057893fde3eac0038c8a6073c30 (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
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
/**********************************************************************
 * Copyright (c) 2004, 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.core.parser.util;

import java.lang.reflect.Array;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Doug Schaefer
 */
public class CharArrayObjectMap extends CharTable {
    public static final CharArrayObjectMap EMPTY_MAP = new CharArrayObjectMap( 0 ){
        @Override
		public Object clone()                         { return this; }
        @Override
		public List toList()                		  { return Collections.EMPTY_LIST; }
        @Override
		public Object put( char[] key, int start, int length, Object value ) 
        { throw new UnsupportedOperationException(); }
    };

	private Object[] valueTable;

	public CharArrayObjectMap(int initialSize) {
		super(initialSize);
		valueTable = new Object[capacity()];
	}
	
	public Object put(char[] key, int start, int length, Object value) {
		int i = addIndex(key, start, length);
		Object oldvalue = valueTable[i];
		valueTable[i] = value;
		return oldvalue;
	}

	final public Object put(char[] key, Object value) {
		return put(key, 0, key.length, value);
	}
	
	final public Object get(char[] key, int start, int length) {
		int i = lookup(key, start, length);
		if (i >= 0)
			return valueTable[i];
		return null;
	}
	
	final public Object get(char[] key) {
		return get(key, 0, key.length);
	}
	
	final public Object getAt( int i ){
	    if( i < 0 || i > currEntry )
	        return null;
	    return valueTable[i];
	}
	
	final public Object remove(char[] key, int start, int length) {
		int i = lookup(key, start, length);
		if (i < 0)
			return null;

		Object value = valueTable[i];

	    if (i < currEntry)
			System.arraycopy(valueTable, i + 1, valueTable, i, currEntry - i);
		
	    valueTable[currEntry] = null;
		
		removeEntry(i);
		
		return value;
	}
	
	@Override
	public Object clone(){
        CharArrayObjectMap newTable = (CharArrayObjectMap) super.clone();
        newTable.valueTable = new Object[ capacity() ];
	    System.arraycopy(valueTable, 0, newTable.valueTable, 0, valueTable.length);

	    return newTable;
	}
	
	@Override
	protected void resize(int size) {
		Object[] oldValueTable = valueTable;
		valueTable = new Object[size];
		System.arraycopy(oldValueTable, 0, valueTable, 0, oldValueTable.length);
		super.resize(size);
	}
    
	@Override
	public void clear() {
		super.clear();
		for( int i = 0; i < capacity(); i++ )
			valueTable[i] = null;
	}

    @Override
	protected int partition( Comparator c, int p, int r ){
        char[] x = keyTable[ p ];
        Object temp = null;
        int i = p;
        int j = r;
        
        while( true ){
            while( c.compare( keyTable[ j ], x ) > 0 ){ j--; }
            if( i < j ) 
                while( c.compare( keyTable[ i ], x ) < 0 ){ i++; }
            
            if( i < j ){
                temp = keyTable[j];
                keyTable[j] = keyTable[i];
                keyTable[i] = (char[]) temp;
                
                temp = valueTable[j];
                valueTable[j] = valueTable[i];
                valueTable[i] = temp;
            } else {
                return j;
            }
        }
    }
    
    public Object [] valueArray(){
	    Object [] values = new Object[ size() ];
	    System.arraycopy( valueTable, 0, values, 0, values.length );
	    return values;
	}

    public Object [] valueArray(Class clazz){
	    Object[] values= (Object[]) Array.newInstance(clazz, size());
	    System.arraycopy( valueTable, 0, values, 0, values.length );
	    return values;
	}
}

Back to the top