001    /*******************************************************************************
002     * Copyright (c) 2004, 2008 IBM Corporation and others.
003     * All rights reserved. This program and the accompanying materials
004     * are made available under the terms of the Eclipse Public License v1.0
005     * which accompanies this distribution, and is available at
006     * http://www.eclipse.org/legal/epl-v10.html
007     *
008     * Contributors:
009     *     IBM Corporation - initial API and implementation
010     *     Markus Schorn (Wind River Systems)
011     *******************************************************************************/
012    package org.eclipse.cdt.core.parser.util;
013    
014    /**
015     * @author Doug Schaefer
016     */
017    public class CharArrayIntMap extends CharTable {
018    
019            private int[] valueTable;
020            public final int undefined;
021    
022            public CharArrayIntMap(int initialSize, int undefined) {
023                    super(initialSize);
024                    valueTable = new int[capacity()];
025                    this.undefined = undefined;
026            }
027    
028            @Override
029            protected void resize(int size) {
030                    int[] oldValueTable = valueTable;
031                    valueTable = new int[size];
032                    System.arraycopy(oldValueTable, 0, valueTable, 0, Math.min(size, oldValueTable.length));
033                    super.resize(size);
034            }
035            
036            @Override
037            public void clear() {
038                    super.clear();
039                    for( int i = 0; i < capacity(); i++ )
040                            valueTable[i] = undefined;
041            }
042            @Override
043            public Object clone(){
044                CharArrayIntMap newMap = (CharArrayIntMap) super.clone();
045                newMap.valueTable = new int[ capacity() ];
046                System.arraycopy(valueTable, 0, newMap.valueTable, 0, valueTable.length);
047                return newMap;
048            }
049            
050            public int put(char[] key, int start, int length, int value) {
051                    int i = addIndex(key, start, length);
052                    int oldvalue = valueTable[i];
053                    valueTable[i] = value;
054                    return oldvalue;
055            }
056    
057            public int put(char[] key, int value) {
058                    return put(key, 0, key.length, value);
059            }
060            
061            public int get(char[] key, int start, int length) {
062                    int i = lookup(key, start, length);
063                    if (i >= 0)
064                            return valueTable[i];
065                    return undefined;
066            }
067            
068            public int get(int pos) {
069                    if( pos < 0 || pos > currEntry )
070                            return undefined;
071                    return valueTable[pos];
072            }
073            
074            public int getKeyLocation(char[] key, int start, int length) {
075                    int i = lookup(key, start, length);
076                    if (i >= 0)
077                            return i;
078                    return undefined;
079            }
080    
081            public int get(char[] image) {
082                    return get( image, 0, image.length );
083            }
084    
085            /**
086             * Puts all mappings of map into this map. The keys are not cloned.
087             * @since 5.0
088             */
089            public void putAll(CharArrayIntMap map) {
090                    resize(size() + map.size());
091                    for(int i=0; i<=map.currEntry; i++) {
092                            put(map.keyTable[i], map.valueTable[i]);
093                    }
094            }
095    }