@Override public ObjectBidirectionalIterator<K> iterator() { return sortedSet.iterator(); } @Override
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(); }
@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))); }
@Override public double getMaxTimestamp() { if (mainIndex) { Double2IntSortedMap sortedMap = (Double2IntSortedMap) timestampIndexStore.timeSortedMap; if (!sortedMap.isEmpty()) { return sortedMap.lastDoubleKey(); } } else { Double2IntSortedMap sortedMap = (Double2IntSortedMap) timestampIndexStore.timeSortedMap; if (!sortedMap.isEmpty()) { ObjectBidirectionalIterator<Double2IntMap.Entry> bi = sortedMap.double2IntEntrySet().iterator(sortedMap .double2IntEntrySet().last()); while (bi.hasPrevious()) { Double2IntMap.Entry entry = bi.previous(); double timestamp = entry.getDoubleKey(); int index = entry.getIntValue(); if (index < timestamps.length) { TimeIndexEntry timestampEntry = timestamps[index]; if (timestampEntry != null) { return timestamp; } } } } } return Double.POSITIVE_INFINITY; }
/** * 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; } }
@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 */ } }
@Override public K last() { synchronized (sync) { return sortedSet.last(); } } }
/** * Return a random sample of exactly numElements. * * @param numElements The number of elements to return * @return a random sample drawn with replacement. */ public List<ErrorRecord> getRandomSample(int numElements) { List<ErrorRecord> sample = new ObjectArrayList<>(); assert !queue.isEmpty() : "queue cannot be empty to return a random sample"; while (sample.size() < numElements) { for (ErrorRecord record : queue) { float value = random.nextFloat(); if (value < 1f / queue.size()) { sample.add(record); } if (sample.size() >= numElements) { break; } } } return sample; }
public int size() { return queue.size(); }
@Override public double getMaxTimestamp() { if (mainIndex) { Double2IntSortedMap sortedMap = (Double2IntSortedMap) timestampIndexStore.timeSortedMap; if (!sortedMap.isEmpty()) { return sortedMap.lastDoubleKey(); } } else { Double2IntSortedMap sortedMap = (Double2IntSortedMap) timestampIndexStore.timeSortedMap; if (!sortedMap.isEmpty()) { ObjectBidirectionalIterator<Double2IntMap.Entry> bi = sortedMap.double2IntEntrySet().iterator(sortedMap .double2IntEntrySet().last()); while (bi.hasPrevious()) { Double2IntMap.Entry entry = bi.previous(); double timestamp = entry.getDoubleKey(); int index = entry.getIntValue(); if (index < timestamps.length) { TimeIndexEntry timestampEntry = timestamps[index]; if (timestampEntry != null) { return timestamp; } } } } } return Double.POSITIVE_INFINITY; }
@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)); } } }
@Override public K last() { return sortedSet.last(); } }
@Override public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { Int2IntAVLTreeMap links = new Int2IntAVLTreeMap(); // remove duplicate links for (IntWritable value : values) { if (links.containsKey(value.get())) { // increment 1 link count links.put(value.get(), links.get(value.get()) + 1); } else { links.put(value.get(), 1); } } context.getCounter(Counts.LINKS).increment(links.entrySet().size()); for (Entry<Integer, Integer> link : links.entrySet()) { String outputValue = String.valueOf(link.getKey()) + "," + String.valueOf(link.getValue()); context.write(key, new Text(outputValue)); } } }
@Override public ObjectBidirectionalIterator<K> iterator(final K from) { return sortedSet.iterator(from); } @Override
@Override public K first() { synchronized (sync) { return sortedSet.first(); } } @Override
public ErrorRecord last() { return queue.last(); }
@Test public void testIterStartFrom() { double[] values = { 1.5, 2.4, -3.2, 4.3, -5.7 }; Long2DoubleSortedMap map = new Long2DoubleSortedArrayMap(SortedKeyIndex.create(1, 2, 3, 4, 5), values); AbstractLong2DoubleMap.BasicEntry key = new AbstractLong2DoubleMap.BasicEntry(2, 2.0); ObjectBidirectionalIterator<Long2DoubleMap.Entry> iter = map.long2DoubleEntrySet().iterator(key); assertThat(iter.next().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(3L)); assertThat(iter.previous().getLongKey(), equalTo(2L)); }
@Override public K first() { return sortedSet.first(); } @Override