/*
 * LensKit, a reference implementation of recommender algorithms.
 * Copyright 2010-2011 Regents of the University of Minnesota
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.data;

import it.unimi.dsi.fastutil.longs.Long2IntMap;
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import it.unimi.dsi.fastutil.longs.LongLists;

public class Indexer implements Index {
    private static final long serialVersionUID = -8383883342128686850L;

    private Long2IntMap indexes;
    private LongArrayList ids;

    public Indexer() {
        indexes = new Long2IntOpenHashMap();
        indexes.defaultReturnValue(-1);
        ids = new LongArrayList();
    }

    @Override
    public long getId(int idx) {
        return ids.getLong(idx);
    }

    @Override
    public LongList getIds() {
        return LongLists.unmodifiable(ids);
    }

    @Override
    public int getIndex(long id) {
        return indexes.get(id);
    }

    @Override
    public int getObjectCount() {
        return ids.size();
    }

    public int internId(long id) {
        int idx = getIndex(id);
        if (idx < 0) {
            idx = ids.size();
            ids.add(id);
            indexes.put(id, idx);
        }
        return idx;
    }
}

@@ -1140,16 +1140,131 @@
 Lists;%0A%0A
+/**%0A * Build contiguous 0-based indexes for long IDs.%0A * %0A * @author Michael Ekstrand %3Cekstrand@cs.umn.edu%3E%0A *%0A */%0A
 public c
@@ -1433,16 +1433,112 @@
 t ids;%0A%0A
+    /**%0A     * Construct a new empty indexer.  The first interned ID will have index 0.%0A     */%0A
     publ
@@ -1687,186 +1687,684 @@
     
-@Override%0A    public long getId(int idx) %7B%0A        return ids.getLong(idx);%0A    %7D%0A%0A    @Override%0A    public LongList getIds() %7B%0A        return LongLists.unmodifiable(ids);%0A    %7D%0A
+/**%0A     * Get the ID for an index.%0A     * @param idx The index to query.%0A     * @return The ID with index %3Cvar%3Eidx%3C/var%3E.%0A     * @throws IndexOutOfBoundsException if the index is invalid.%0A     */%0A    @Override%0A    public long getId(int idx) %7B%0A        return ids.getLong(idx);%0A    %7D%0A%0A    /**%0A     * Get the list of all IDs in index order.%0A     * @return The list of IDs.%0A     */%0A    @Override%0A    public LongList getIds() %7B%0A        return LongLists.unmodifiable(ids);%0A    %7D%0A%0A    /**%0A     * Get the index for an ID.%0A     * %0A     * @param id The ID o query.%0A     * @return The index assigned to %3Cvar%3Eid%3C/var%3E, or -1 if %3Cvar%3Eid%3C/var%3E has%0A     *         not been interned.%0A     */
 %0A   
@@ -2444,24 +2444,99 @@
 id);%0A    %7D%0A%0A
+    /**%0A     * Get the number of objects interned in this indexer.%0A     */%0A
     @Overrid
@@ -2601,24 +2601,327 @@
 e();%0A    %7D%0A%0A
+    /**%0A     * Get an index for an ID, generating a new one if necessary.%0A     * %0A     * @param id The ID.%0A     * @return The index for %3Cvar%3Eid%3C/var%3E. If the ID has already been interned,%0A     *         the old index is returned; otherwise, a new index is generated%0A     *         and returned.%0A     */%0A
     public i

Document Indexer.