001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.core.collections;
025    
026    import java.util.ArrayList;
027    import java.util.Collection;
028    import java.util.HashMap;
029    import java.util.Map;
030    
031    /**
032     * ArrayIndexList is like an ArrayList, but keeps track of the indices where
033     * objects have been added. This improves the speed of indexOf() and contains()
034     * 
035     * @author Holger Arndt
036     * 
037     * @param <M>
038     *            Type of the elements in the list
039     */
040    public class ArrayIndexList<M> extends ArrayList<M> {
041            private static final long serialVersionUID = 3657191905843442834L;
042    
043            private final Map<M, Integer> indexMap = new HashMap<M, Integer>();
044    
045            public ArrayIndexList() {
046                    super();
047            }
048    
049            public ArrayIndexList(Collection<? extends M> c) {
050                    super();
051                    addAll(c);
052            }
053    
054            public void add(int index, M element) {
055                    new Exception("not implemented").printStackTrace();
056            }
057    
058            public boolean add(M e) {
059                    indexMap.put(e, size());
060                    return super.add(e);
061            }
062    
063            public boolean addAll(Collection<? extends M> c) {
064                    for (M m : c) {
065                            add(m);
066                    }
067                    return true;
068            }
069    
070            public boolean addAll(int index, Collection<? extends M> c) {
071                    new Exception("not implemented").printStackTrace();
072                    return false;
073            }
074    
075            public void clear() {
076                    indexMap.clear();
077                    super.clear();
078            }
079    
080            public boolean contains(Object o) {
081                    return indexMap.containsKey(o);
082            }
083    
084            public int indexOf(Object o) {
085                    return indexMap.get(o);
086            }
087    
088            public int lastIndexOf(Object o) {
089    
090                    return super.lastIndexOf(o);
091            }
092    
093            public M remove(int index) {
094                    M m = super.remove(index);
095                    indexMap.remove(m);
096                    return m;
097            }
098    
099            public boolean remove(Object o) {
100                    indexMap.remove(o);
101                    return super.remove(o);
102            }
103    
104            public M set(int index, M element) {
105                    new Exception("not implemented").printStackTrace();
106                    return null;
107            }
108    
109    }