/** * Make an immutable copy of this index mapping. * * @return An immutable copy of the index mapping. */ public FrozenHashKeyIndex frozenCopy() { return new FrozenHashKeyIndex(indexMap, keyList); } }
/** * Convenience method to create a frozen key index. * @param keys The keys. * @return A frozen index. * @see FrozenHashKeyIndex#create(LongList) */ public static FrozenHashKeyIndex createFrozen(LongList keys) { return FrozenHashKeyIndex.create(keys); }
@Override public int getUpperBound() { return size(); }
@Override public boolean containsKey(long id) { return tryGetIndex(id) >= 0; }
@Override public int getIndex(long id) { int idx = tryGetIndex(id); if (idx < 0) { throw new IllegalArgumentException("key " + id + " not in index"); } else { return idx; } }
/** * Construct a new key index. It maps each long key to its position in the list. * * @param keys The list of keys to store. */ public static FrozenHashKeyIndex create(LongList keys) { return new FrozenHashKeyIndex(keys); }
/** * Create a new key index. * * @param keys The keys. * @return A key index containing the elements of {@code keys}. */ public static FrozenHashKeyIndex create(LongCollection keys) { if (keys instanceof LongList) { return create((LongList) keys); } else { HashKeyIndex index = new HashKeyIndex(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { index.internId(iter.nextLong()); } return index.frozenCopy(); } }
@Override public boolean containsKey(long id) { return tryGetIndex(id) >= 0; }
@Override public int getUpperBound() { return size(); }
/** * Make an immutable copy of this index mapping. * * @return An immutable copy of the index mapping. */ public FrozenHashKeyIndex frozenCopy() { return new FrozenHashKeyIndex(indexMap, keyList); } }
/** * Convenience method to create a frozen key index. * @param keys The keys. * @return A frozen index. * @see FrozenHashKeyIndex#create(LongList) */ public static FrozenHashKeyIndex createFrozen(LongList keys) { return FrozenHashKeyIndex.create(keys); }
@Override public int getIndex(long id) { int idx = tryGetIndex(id); if (idx < 0) { throw new IllegalArgumentException("key " + id + " not in index"); } else { return idx; } }
/** * Construct a new key index. It maps each long key to its position in the list. * * @param keys The list of keys to store. */ public static FrozenHashKeyIndex create(LongList keys) { return new FrozenHashKeyIndex(keys); }
/** * Create a new key index. * * @param keys The keys. * @return A key index containing the elements of {@code keys}. */ public static FrozenHashKeyIndex create(LongCollection keys) { if (keys instanceof LongList) { return create((LongList) keys); } else { HashKeyIndex index = new HashKeyIndex(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { index.internId(iter.nextLong()); } return index.frozenCopy(); } }
@Test public void testEmpty() { KeyIndex empty = new FrozenHashKeyIndex(LongLists.EMPTY_LIST); assertThat(empty.getKeyList(), hasSize(0)); assertThat(empty.containsKey(30), equalTo(false)); assertThat(empty.tryGetIndex(30), equalTo(-1)); try { empty.getIndex(30); fail("getting absent index should fail"); } catch (IllegalArgumentException ex) { /* expected */ } }
@Test public void testSingleton() { KeyIndex idx = new FrozenHashKeyIndex(LongLists.singleton(42)); assertThat(idx.getKeyList(), hasSize(1)); assertThat(idx.getKeyList(), contains(42L)); assertThat(idx.containsKey(30), equalTo(false)); assertThat(idx.containsKey(42), equalTo(true)); assertThat(idx.tryGetIndex(30), equalTo(-1)); assertThat(idx.tryGetIndex(42), equalTo(0)); assertThat(idx.getIndex(42), equalTo(0)); assertThat(idx.getKey(0), equalTo(42L)); try { idx.getIndex(30); fail("getting absent index should fail"); } catch (IllegalArgumentException ex) { /* expected */ } }
@Test public void testFailsWithDuplicates() { LongList keys = new LongArrayList(); keys.add(10); keys.add(42); keys.add(5); keys.add(10); try { KeyIndex idx = new FrozenHashKeyIndex(keys); fail("creating key index with duplicates should fail"); } catch (IllegalArgumentException ex) { /* expected */ } } }