/** * Create a new {@link Long2DoubleSortedArrayMap} from unsorted key and value * arrays. The provided arrays will be modified and should not be used * by the client after this operation has completed. The key domain of * the new {@link Long2DoubleSortedArrayMap} will be the same as {@code keys}. * * @param keys Array of entry keys. This should be duplicate-free. * @param values The values of the vector, in key order. * @return A sparse vector backed by the provided arrays. * @throws IllegalArgumentException if there is a problem with the provided * arrays (length mismatch, etc.). */ public static Long2DoubleSortedArrayMap wrapUnsorted(long[] keys, double[] values) { IdComparator comparator = new IdComparator(keys); ParallelSwapper swapper = new ParallelSwapper(keys, values); quickSort(0, keys.length, comparator, swapper); int n = keys.length; for (int i = 1; i < n; i++) { if (keys[i-1] == keys[i]) { throw new IllegalArgumentException("duplicate keys"); } } SortedKeyIndex index = SortedKeyIndex.wrap(keys, keys.length); return wrap(index, values); }
/** * Compute the set difference of two sets. * * @param items The initial set * @param exclude The items to remove * @return The elements of <var>items</var> that are not in <var>exclude</var>. */ public static LongSortedSet setDifference(LongSet items, LongSet exclude) { long[] data = new long[items.size()]; final LongIterator iter = items.iterator(); int i = 0; while (iter.hasNext()) { final long x = iter.nextLong(); if (!exclude.contains(x)) { data[i++] = x; } } if (!(items instanceof LongSortedSet)) { Arrays.sort(data, 0, i); } // trim the array if (data.length * 2 > i * 3) { data = Arrays.copyOf(data, i); } return SortedKeyIndex.wrap(data, i).keySet(); }
return wrap(SortedKeyIndex.wrap(nks, ni), nvs);
@Test public void testEmptyArray() { long[] rawKeys = {}; SortedKeyIndex keys = SortedKeyIndex.wrap(rawKeys, 0); assertThat(keys.size(), equalTo(0)); assertThat(keys.size(), equalTo(0)); assertThat(keys.keySet(), hasSize(0)); assertThat(keys.getKeyList(), hasSize(0)); assertThat(keys.tryGetIndex(42), lessThan(0)); }
@Theory public void testABunch(KeyData data) { long[] rawKeys = data.getKeys(10); List<Long> keyList = new LongArrayList(rawKeys); SortedKeyIndex keys = SortedKeyIndex.wrap(rawKeys, rawKeys.length); assertThat(keys.size(), equalTo(10)); assertThat(keys.size(), equalTo(10)); assertThat(keys.getKeyList(), contains(keyList.toArray())); assertThat(keys.keySet(), contains(keyList.toArray())); assertThat(keys.tryGetIndex(data.getLow()), lessThan(0)); for (int i = 0; i < 10; i++) { assumeThat(keys.tryGetIndex(rawKeys[i]), equalTo(i)); assertThat(keys.tryGetIndex(data.getAfter(i)), lessThan(0)); } } }
return SortedKeyIndex.wrap(data, i).keySet();
@Theory public void testSingleton(KeyData data) { assumeThat(data, notNullValue()); long key = data.getKey(); // key to use long low = data.getLow(); // unused low key long high = data.getAfter(1); // unused high key long[] rawKeys = {key}; SortedKeyIndex keys = SortedKeyIndex.wrap(rawKeys, 1); assertThat(keys.size(), equalTo(1)); assertThat(keys.size(), equalTo(1)); assertThat(keys.keySet(), hasSize(1)); assertThat(keys.tryGetIndex(key), equalTo(0)); assertThat(keys.tryGetIndex(low), lessThan(0)); assertThat(keys.tryGetIndex(high), lessThan(0)); assertThat(keys.getKeyList(), contains(key)); }
@Theory public void testMultiple(KeyData data) { long[] rawKeys = data.getKeys(3); long k1 = rawKeys[0]; long k2 = rawKeys[1]; long k3 = rawKeys[2]; SortedKeyIndex keys = SortedKeyIndex.wrap(rawKeys, 3); assertThat(keys.size(), equalTo(3)); assertThat(keys.size(), equalTo(3)); assertThat(keys.getKeyList(), contains(k1, k2, k3)); assertThat(keys.keySet(), contains(k1, k2, k3)); assertThat(keys.tryGetIndex(k1), equalTo(0)); assertThat(keys.tryGetIndex(k2), equalTo(1)); assertThat(keys.tryGetIndex(k3), equalTo(2)); assertThat(keys.tryGetIndex(data.getLow()), lessThan(0)); assertThat(keys.tryGetIndex(data.getAfter(0)), lessThan(0)); assertThat(keys.tryGetIndex(data.getAfter(1)), lessThan(0)); assertThat(keys.tryGetIndex(data.getAfter(2)), lessThan(0)); }
return SortedKeyIndex.wrap(data, data.length).keySet();
/** * Compute the set difference of two sets. * * @param items The initial set * @param exclude The items to remove * @return The elements of <var>items</var> that are not in <var>exclude</var>. */ public static LongSortedSet setDifference(LongSet items, LongSet exclude) { long[] data = new long[items.size()]; final LongIterator iter = items.iterator(); int i = 0; while (iter.hasNext()) { final long x = iter.nextLong(); if (!exclude.contains(x)) { data[i++] = x; } } if (!(items instanceof LongSortedSet)) { Arrays.sort(data, 0, i); } // trim the array if (data.length * 2 > i * 3) { data = Arrays.copyOf(data, i); } return SortedKeyIndex.wrap(data, i).keySet(); }
/** * Create a new {@link Long2DoubleSortedArrayMap} from unsorted key and value * arrays. The provided arrays will be modified and should not be used * by the client after this operation has completed. The key domain of * the new {@link Long2DoubleSortedArrayMap} will be the same as {@code keys}. * * @param keys Array of entry keys. This should be duplicate-free. * @param values The values of the vector, in key order. * @return A sparse vector backed by the provided arrays. * @throws IllegalArgumentException if there is a problem with the provided * arrays (length mismatch, etc.). */ public static Long2DoubleSortedArrayMap wrapUnsorted(long[] keys, double[] values) { IdComparator comparator = new IdComparator(keys); ParallelSwapper swapper = new ParallelSwapper(keys, values); quickSort(0, keys.length, comparator, swapper); // FIXME Verify that the keys have no duplicates SortedKeyIndex index = SortedKeyIndex.wrap(keys, keys.length); return wrap(index, values); }
return wrap(SortedKeyIndex.wrap(nks, ni), nvs);
return SortedKeyIndex.wrap(data, i).keySet();
return SortedKeyIndex.wrap(data, data.length).keySet();