public String findKey(byte[] obj) { if (nodeKeySlots.size() == 0) { return null; } long objHash = hashFn.hashBytes(obj).asLong(); Long2ObjectSortedMap<String> subMap = nodeKeySlots.tailMap(objHash); if (subMap.isEmpty()) { return nodeKeySlots.long2ObjectEntrySet().first().getValue(); } Long2ObjectMap.Entry<String> firstEntry = subMap.long2ObjectEntrySet().first(); return firstEntry.getValue(); }
@Override public K first() { synchronized (sync) { return sortedSet.first(); } } @Override
@Override public K first() { return sortedSet.first(); } @Override
public ErrorRecord first() { return queue.first(); }
@Test public void testSingletonMap() { Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.create(42), new double[]{3.5}); assertThat(map.get(42L), equalTo(3.5)); assertThat(map.size(), equalTo(1)); assertThat(map.isEmpty(), equalTo(false)); assertThat(map.keySet(), contains(42L)); assertThat(map.values(), contains(3.5)); assertThat(map.entrySet(), hasSize(1)); assertThat(map.firstLongKey(), equalTo(42L)); assertThat(map.lastLongKey(), equalTo(42L)); Map.Entry<Long, Double> ent = map.entrySet().first(); assertThat(ent, notNullValue()); assertThat(ent.getKey(), equalTo(42L)); assertThat(ent.getValue(), equalTo(3.5)); assertThat(map.entrySet().contains(Pair.of(42L, 3.5)), equalTo(true)); assertThat(map.entrySet().contains(Pair.of(42L, 3.7)), equalTo(false)); assertThat(map.entrySet().contains(Pair.of(41L, 3.5)), equalTo(false)); assertThat(map.entrySet().first(), equalTo((Object) Pair.of(42L, 3.5))); assertThat(map.entrySet().last(), equalTo((Object) Pair.of(42L, 3.5))); }
@Test public void testEmptyMap() { Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.empty(), new double[0]); assertThat(map.size(), equalTo(0)); assertThat(map.isEmpty(), equalTo(true)); assertThat(map.keySet(), hasSize(0)); assertThat(map.entrySet(), hasSize(0)); assertThat(map.values(), hasSize(0)); assertThat(map.long2DoubleEntrySet(), hasSize(0)); assertThat(map.get(42L), equalTo(0.0)); assertThat(map.get((Long) 42L), nullValue()); try { map.entrySet().first(); fail("entrySet.first should fail"); } catch (NoSuchElementException e) { /* expected */ } try { map.entrySet().last(); fail("entrySet.last should fail"); } catch (NoSuchElementException e) { /* expected */ } }
@Test public void testCreateWithLists() { for (Set<Long> keys: someSets(longs(), integers(0, 500))) { LongSortedSet sorted = LongUtils.packedSet(keys); SortedKeyIndex dom = SortedKeyIndex.fromCollection(keys); double[] values = new double[dom.size()]; for (int i = 0; i < dom.size(); i++) { values[i] = doubles().next(); } Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(dom, values); assertThat(map.size(), equalTo(dom.size())); assertThat(map.size(), equalTo(keys.size())); if (map.size() > 0) { assertThat(map.entrySet().first().getKey(), equalTo(sorted.firstLong())); assertThat(map.entrySet().last().getKey(), equalTo(sorted.lastLong())); assertThat(map.firstLongKey(), equalTo(sorted.firstLong())); assertThat(map.lastLongKey(), equalTo(sorted.lastLong())); } assertThat(map.keySet(), equalTo(sorted)); for (Long k: keys) { assertThat(map.containsKey(k), equalTo(true)); } } }
/** * Dequeues an error from the queue, returning an instance of {@link ErrorRecord}. * Documents are dequeued in inverse score order. * * @return the next {@link ErrorRecord}. */ public final ErrorRecord dequeue() { ErrorRecord v = queue.first(); queue.remove(v); return v; }
public String findKey(byte[] obj) { if (nodeKeySlots.size() == 0) { return null; } long objHash = hashFn.hashBytes(obj).asLong(); Long2ObjectSortedMap<String> subMap = nodeKeySlots.tailMap(objHash); if (subMap.isEmpty()) { return nodeKeySlots.long2ObjectEntrySet().first().getValue(); } Long2ObjectMap.Entry<String> firstEntry = subMap.long2ObjectEntrySet().first(); return firstEntry.getValue(); }
public String findKey(byte[] obj) { if (nodeKeySlots.size() == 0) { return null; } long objHash = hashFn.hashBytes(obj).asLong(); Long2ObjectSortedMap<String> subMap = nodeKeySlots.tailMap(objHash); if (subMap.isEmpty()) { return nodeKeySlots.long2ObjectEntrySet().first().getValue(); } Long2ObjectMap.Entry<String> firstEntry = subMap.long2ObjectEntrySet().first(); return firstEntry.getValue(); }
@Override public boolean contains(long address, long size) { if (indexedMemoryAccessChecker != null) { int result = indexedMemoryAccessChecker.isAllocated(address, size); if (result == IndexedMemoryAccessChecker.ALLOCATED) { return true; } else if (result == IndexedMemoryAccessChecker.NOT_ALLOCATED) { return false; } // Not indexed, so check over allocated memories } Long2LongMap.Entry entry = allocatedMemories.tailMap(address).long2LongEntrySet().first(); if (entry == null) { return false; } long startAddress = entry.getLongKey(); long endAddress = startAddress + entry.getLongValue(); return address >= startAddress && (address + size) <= endAddress; }
@Override public boolean contains(long address) { if (indexedMemoryAccessChecker != null) { int result = indexedMemoryAccessChecker.isAllocated(address); if (result == IndexedMemoryAccessChecker.ALLOCATED) { return true; } else if (result == IndexedMemoryAccessChecker.NOT_ALLOCATED) { return false; } // Not indexed, so check over allocated memories } Long2LongMap.Entry entry = allocatedMemories.tailMap(address).long2LongEntrySet().first(); if (entry == null) { return false; } long startAddress = entry.getLongKey(); long endAddress = startAddress + entry.getLongValue(); return address >= startAddress && address <= endAddress; }
/** * Enqueues a transcript with given score and info. * * @return true if the document has been actually enqueued. */ public synchronized boolean enqueue(float wrongness, INDArray features, INDArray label) { ErrorRecord dsi = new ErrorRecord(wrongness, features, label); if (queue.contains(dsi)) return false; if (queue.size() < maxSize) { queue.add(dsi); return true; } else { dsi = queue.first(); if (wrongness > dsi.wrongness) { queue.remove(dsi); dsi.wrongness = wrongness; dsi.features = features; dsi.label = label; queue.add(dsi); return true; } return false; } }