public static void triangulateSingleNum(IntList data, List<int[]> idsVert){ IntList dataNew=new IntArrayList(idsVert.size()); int count=0; for(int[] face:idsVert){ dataNew.add(data.getInt(count)); dataNew.add(data.getInt(count+1)); dataNew.add(data.getInt(count+2)); if(face.length>3){ dataNew.add(data.getInt(count)); dataNew.add(data.getInt(count+2)); dataNew.add(data.getInt(count+3)); } count+=face.length; } data.clear(); data.addAll(dataNew); }
private void executePara(int attribute) throws CouldNotReceiveResultException, ColumnNameMismatchException { for (OpenBitSet lhs : this.lhss.get(attribute)) { if (lhs.get(attribute)) { continue; } IntList bits = new IntArrayList(); int lastIndex = lhs.nextSetBit(0); while (lastIndex != -1) { bits.add(lastIndex); lastIndex = lhs.nextSetBit(lastIndex + 1); } FunctionalDependencyGroup2 fdg = new FunctionalDependencyGroup2(attribute, bits); this.fdrr.receiveResult((fdg.buildDependency(this.relationName, this.columns))); this.result.add(fdg); } }
private void output() throws CouldNotReceiveResultException, ColumnNameMismatchException { // Read the discovered INDs from the attributes Int2ObjectOpenHashMap<IntList> dep2ref = new Int2ObjectOpenHashMap<IntList>(this.numColumns); for (Attribute spiderAttribute : this.attributeId2attributeObject.values()) if (!spiderAttribute.getReferenced().isEmpty()) dep2ref.put(spiderAttribute.getAttributeId(), new IntArrayList(spiderAttribute.getReferenced())); // Write the result to the resultReceiver for (int dep : dep2ref.keySet()) { String depTableName = this.getTableNameFor(dep, this.tableColumnStartIndexes); String depColumnName = this.columnNames.get(dep); for (int ref : dep2ref.get(dep)) { String refTableName = this.getTableNameFor(ref, this.tableColumnStartIndexes); String refColumnName = this.columnNames.get(ref); this.resultReceiver.receiveResult(new InclusionDependency(new ColumnPermutation(new ColumnIdentifier(depTableName, depColumnName)), new ColumnPermutation(new ColumnIdentifier(refTableName, refColumnName)))); this.numUnaryINDs++; } } }
/** * @return a IntList from the input JsonNode with the given name, or throw an BadRequestException */ public static IntList getRequiredListOfInteger(JsonNode json, String name) throws BadRequestException { final JsonNode node = json.get(name); if (node == null || !(node.isArray())) { throw new BadRequestException("json is missing required List: " + name); } final IntList list = new IntArrayList(node.size()); for (JsonNode innerNode : node) { if (!innerNode.canConvertToInt()) { throw new BadRequestException("json is not an int: " + innerNode.toString()); } list.add(innerNode.asInt()); } return list; }
public LearningInstance featurize(JsonNode entity, boolean update) { Map<String, List<Feature>> feaMap = FeaturizerUtilities.getFeatureMap(entity, true, featureExtractors, indexSpace); String group = null; if (groupKeys != null && groupKeys.size() > 0) { group = FeatureExtractorUtilities.composeConcatenatedKey(entity, groupKeys); } TensorFlowInstance instance = new TensorFlowInstance(group); for (Map.Entry<String, List<Feature>> entry : feaMap.entrySet()) { DoubleList doubles = new DoubleArrayList(); IntList ints = new IntArrayList(); for (Feature feature : entry.getValue()) { doubles.add(feature.getValue()); ints.add(feature.getIndex()); } double[] darr = doubles.toDoubleArray(); instance.putValues(entry.getKey(), darr); int[] iarr = ints.toIntArray(); instance.putIndices(entry.getKey(), iarr); } return instance; }
private List<double[]> getNeighbors(int curIndex, IntList svdIndices, SVDFeature svdFeature, List<String> features) { List<double[]> raw = new ArrayList<>(svdIndices.size()); for (int target : svdIndices) { if (target != curIndex && (numMatch == 0 || matchPrefixFeatures(curIndex, target, features))) { double[] pair = new double[2]; pair[0] = target; pair[1] = svdFeature.getVectorVarByNameIndex(SVDFeatureKey.FACTORS.get(), target) .cosine(svdFeature.getVectorVarByNameIndex(SVDFeatureKey.FACTORS.get(), curIndex)); raw.add(pair); } } Ordering<double[]> pairDoubleOrdering = SortingUtilities.pairDoubleSecondOrdering(); List<double[]> neighbors; if (reverse) { neighbors = pairDoubleOrdering.leastOf(raw, numNeighbors); } else { neighbors = pairDoubleOrdering.greatestOf(raw, numNeighbors); } return neighbors; }
/** * Returns the permutation to obtain the re-ranking * * @return permutation to obtain the re-ranking */ public int[] rerankPermutation() { List<Tuple2od<I>> list = recommendation.getItems(); IntList perm = new IntArrayList(); IntLinkedOpenHashSet remainingI = new IntLinkedOpenHashSet(); IntStream.range(0, list.size()).forEach(remainingI::add); while (!remainingI.isEmpty() && perm.size() < min(maxLength, cutoff)) { int bestI = selectItem(remainingI, list); perm.add(bestI); remainingI.remove(bestI); update(list.get(bestI)); } while (perm.size() < min(maxLength, list.size())) { perm.add(remainingI.removeFirstInt()); } return perm.toIntArray(); }
@Test public void whenThereIsNotIntervalsOnListItShouldWriteIntervalsSuccinctRepresentationSuccessfully() throws Exception { final int valueBitSize = 1; final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 16, 19}); final String expectedOutput = "1".replace(" ", ""); final int expectedOffset = 1; final Helper.Output out = getOutput(); final int offset = out.stream.writeIntervals(list, valueBitSize); out.close(); assertEquals(expectedOutput, toBinaryString(out.buffer, offset)); assertEquals(expectedOffset, offset); }
@Test public void whenListIsSmallerThanMinIntervalSizeItShouldWriteIntervalsSuccinctRepresentationSuccessfully() throws Exception { final int valueBitSize = 1; final IntList list = new IntArrayList(new int[] {1, 3, 4}); final String expectedOutput = "1".replace(" ", ""); final int expectedOffset = 1; final Helper.Output out = getOutput(); final int offset = out.stream.writeIntervals(list, valueBitSize); out.close(); assertEquals(expectedOutput, toBinaryString(out.buffer, offset)); assertEquals(expectedOffset, offset); }
@Test public void whenThereIsIntervalsOnListItShouldWriteIntervalsSuccinctRepresentationSuccessfully() throws Exception { final int valueBitSize = 3; final IntList list = new IntArrayList( new int[] {1, 3, 4, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 30} ); /** * 3 [7, 8, 9, 10] [14, 15, 16, 17] [19, 20, 21, 22, 23, 24] Intervals from list. * 3 [7, 10] [14, 17] [19, 24] Interval format. * 3 [7, 0] [2, 0] [0, 2] Interval delta format. * 4 7 1 3 1 1 3 Add 1 to ensure non zeros. * 001 111 1 11 1 1 11 Binary representation. * 3-00 111 1 2-1 1 1 2-1 Decimal Gamma Prefix and Binary Gamma Suffix. * 11-00 111 1 01-1 1 1 01-1 Binary Gamma Prefix and Binary Gamma Suffix. * 01100 111 1 0101 1 1 0101 Delta Encoding. */ final String expectedOutput = "01100 111 1 0101 1 1 0101".replace(" ", ""); final int expectedOffset = 19; final Helper.Output out = getOutput(); final int offset = out.stream.writeIntervals(list, valueBitSize); out.close(); assertEquals(expectedOutput, toBinaryString(out.buffer, offset)); assertEquals(expectedOffset, offset); }
@Test public void whenReferenceListLastElementsMatchesWithListShouldRecoverListSegmentOnReferenceListSuccessfully() throws Exception { final Helper.Input input = getInput("0100 01100 0 1 0101 1"); final int listIndex = 1; final IntList referenceList = new IntArrayList(new int[] {0, 2, 3, 5, 9, 12, 13}); final IntList expectedList = new IntArrayList(new int[] {2, 3, 5, 12, 13}); final IntList list = new IntArrayList(); ReferenceIterator referenceIterator = new ReferenceIteratorBuilder(input, listIndex, referenceList).build(); while (referenceIterator.hasNext()) { list.add(referenceIterator.next()); } assertEquals(expectedList, list); }
@Test public void whenThereIsAnEncodedRepetitionsListItShouldReturnExpectedRemainingElementsSuccessfully() throws Exception { final int originalListSize = 16; Helper.Input input = getInput("01110 1 1 0100 0100 0100 1 0101 1 0100 1"); /** * (0, 2), (2, 3), (4, 2), (7, 2), (9, 2) Repetitions */ final IntList expectedRemainingElements = new IntArrayList( new int[] {4, 4, 3, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0} ); final IntList remainingElements = new IntArrayList(); RepeatsIterator repeatsIterator = new RepeatsIterator(input.stream); for(int i = 0; i < originalListSize; i++) { remainingElements.add(repeatsIterator.getRemainingElements()); repeatsIterator.next(); } remainingElements.add(repeatsIterator.getRemainingElements()); assertEquals(expectedRemainingElements, remainingElements); }
@Test public void whenIndexDoesNotHaveAnyPreviousIntersectingListItShouldIndexListAsExpected() { final Long2ReferenceMap<IntList> expectedListsInvertedIndex = new Long2ReferenceOpenHashMap<IntList>() {{ put(1L, new IntArrayList(new int[] {0})); put(3L, new IntArrayList(new int[] {0})); put(4L, new IntArrayList(new int[] {0})); put(5L, new IntArrayList(new int[] {0})); put(8L, new IntArrayList(new int[] {0})); }}; final InvertedListLongReferenceListsIndex listsIndex = new InvertedListLongReferenceListsIndex(); final int index = 0; final LongList list = new LongArrayList(new long[] {1L, 3L, 4L, 5L, 8L}); listsIndex.addListIntoListsInvertedIndex(index, list); assertEquals( Helper.<Long, Long2ReferenceMap<IntList>>translateToUtilsCollection(expectedListsInvertedIndex), Helper.<Long, Long2ReferenceMap<IntList>>translateToUtilsCollection(listsIndex.listsInvertedIndex) ); }
@Test public void whenIndexHavePreviousIntersectingListItShouldIndexListAsExpected() { final Long2ReferenceMap<IntList> expectedListsInvertedIndex = new Long2ReferenceOpenHashMap<IntList>() {{ put(1L, new IntArrayList(new int[] {1})); put(2L, new IntArrayList(new int[] {0})); put(3L, new IntArrayList(new int[] {0, 1})); put(4L, new IntArrayList(new int[] {1})); put(5L, new IntArrayList(new int[] {0, 1})); put(8L, new IntArrayList(new int[] {1})); }}; final InvertedListLongReferenceListsIndex listsIndex = new InvertedListLongReferenceListsIndex(); listsIndex.listsInvertedIndex.put(2L, new IntArrayList(new int[]{0})); listsIndex.listsInvertedIndex.put(3L, new IntArrayList(new int[]{0})); listsIndex.listsInvertedIndex.put(5L, new IntArrayList(new int[]{0})); final int index = 1; final LongList list = new LongArrayList(new long[] {1L, 3L, 4L, 5L, 8L}); listsIndex.addListIntoListsInvertedIndex(index, list); assertEquals( Helper.<Long, Long2ReferenceMap<IntList>>translateToUtilsCollection(expectedListsInvertedIndex), Helper.<Long, Long2ReferenceMap<IntList>>translateToUtilsCollection(listsIndex.listsInvertedIndex) ); }
@Test public void whenRepetitionsItShouldWriteItsSuccinctRepresentationSuccessfully() throws Exception { final IntList list = new IntArrayList(new int[] {1, 1, 2, 3, 3, 3, 5, 6, 6, 7, 10, 11, 11, 16, 19, 19}); /** * {(0, 2), (2, 3), (4, 2), (7, 2), (9, 2)} Repetitions Intervals. * 5 0 0 1 1 1 0 2 0 1 0 Delta Representation. * 6 1 1 2 2 2 1 3 1 2 1 Add 1 to ensure non zeros. * 011 1 1 10 10 10 1 11 1 10 1 Binary representation. * 3-10 1 1 2-0 2-0 2-0 1 2-1 1 2-0 1 Decimal Gamma Prefix and Binary Gamma Suffix * 11-10 1 1 10-0 10-0 10-0 1 10-1 1 10-0 1 Binary Gamma Prefix and Binary Gamma Suffix * 01110 1 1 0100 0100 0100 1 0101 1 0100 1 Delta Encoding */ final String expectedOutput = "01110 1 1 0100 0100 0100 1 0101 1 0100 1".replace(" ", ""); final int expectedOffset = 30; final Helper.Output out = getOutput(); final int offset = out.stream.writeRepetitions(list); out.close(); assertEquals(expectedOutput, toBinaryString(out.buffer, offset)); assertEquals(expectedOffset, offset); }
@Test public void itShouldWriteDeltaSuccinctRepresentationSuccessfully() throws Exception { final int valueBitSize = 1; final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 7, 10}); /** * 6 1 0 0 1 1 2 Delta list. * 7 1 1 1 2 2 3 Add 1 to ensure non zeros. * 111 1 1 1 01 01 11 Binary representation. * 3-11 1 1 1 2-0 2-0 2-1 Decimal Gamma Prefix and Binary Gamma Suffix. * 11-11 1 1 1 01-0 01-0 01-1 Binary Gamma Prefix and Binary Gamma Suffix. * 01111 1 1 1 0100 0100 0101 Delta Encoding. */ final String expectedOutput = "01111 1 1 1 0100 0100 0101".replace(" ", ""); final int expectedOffset = 20; final Helper.Output out = getOutput(); final int offset = out.stream.writeDelta(list, valueBitSize); out.close(); assertEquals(expectedOutput, toBinaryString(out.buffer, offset)); assertEquals(expectedOffset, offset); }
@Test public void whenReferenceListHasMatchesItShouldReturnExpectedOffsetSuccessfully() { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 16, 19}); final IntList referenceList = new IntArrayList(new int[] {0, 2, 3, 5, 9, 12, 13}); /** * { 1, 2, 3, 5, 6, 7, 10, 11, 16, 19} List. * {0, 2, 3, 5, 9, 12, 13 } Reference list. * 0 1 1 1 0 0 0 Matches. * 3 0 1 3 3 Matches blocks representation. * 2 0 0 2 Matches blocks concise representation. * 3 0 1 3 Add 1 to ensure non zeros. * 11 0 1 11 Binary representation. * 2-1 0 1 2-1 Decimal Gamma Prefix and Binary Gamma Suffix. * 01-1 0 1 01-1 Binary Gamma Prefix and Binary Gamma Suffix. * 0101 0 1 0101 Delta Encoding. */ final int expectedOffset = 10; IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteDifferenceOffset(list, referenceList); assertEquals(expectedOffset, offset); }
@Test public void whenReferenceStartWithMatchesItShouldWReturnExpectedOffsetSuccessfully() { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 16, 19}); final IntList referenceList = new IntArrayList(new int[] {1, 2, 3, 5, 9, 12, 13}); /** * {1, 2, 3, 5, 6, 7, 10, 11, 16, 19} List. * {1, 2, 3, 5, 9, 12, 13 } Reference list. * 1 1 1 1 0 0 0 Matches. * 2 1 4 3 Matches blocks representation. * 1 1 3 Matches blocks concise representation. * 2 1 4 Add 1 to ensure non zeros. * 01 1 001 Binary representation. * 2-0 1 3-00 Decimal Gamma Prefix and Binary Gamma Suffix. * 01-0 1 11-00 Binary Gamma Prefix and Binary Gamma Suffix. * 0100 1 01100 Delta Encoding. */ final int expectedOffset = 10; IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteDifferenceOffset(list, referenceList); assertEquals(expectedOffset, offset); }
@Test public void whenReferenceListHasMatchesAtTheEndOfListItReturnExpectedOffsetSuccessfully() { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 12, 13}); final IntList referenceList = new IntArrayList(new int[] {0, 2, 3, 5, 9, 12, 13, 14}); /** * { 1, 2, 3, 5, 6, 7, 10, 11, 12, 13 } List. * {0, 2, 3, 5, 9, 12, 13, 14} Reference list. * 0 1 1 1 0 1 1 0 Matches. * 5 0 1 3 1 2 1 Matches blocks representation. * 4 0 0 2 0 1 Matches blocks concise representation. * 5 0 1 3 1 2 Add 1 to ensure non zeros. * 101 0 1 11 1 01 Binary representation. * 3-01 0 1 2-1 1 2-0 Decimal Gamma Prefix and Binary Gamma Suffix. * 11-01 0 1 01-1 1 01-0 Binary Gamma Prefix and Binary Gamma Suffix. * 01101 0 1 0101 1 0100 Delta Encoding. */ final int expectedOffset = 16; IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteDifferenceOffset(list, referenceList); assertEquals(expectedOffset, offset); }
@Test public void whenReferenceListHasMatchesAtTheEndOfReferenceListItShouldReturnExpectedOffsetSuccessfully() { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 12, 13, 14}); final IntList referenceList = new IntArrayList(new int[] {0, 2, 3, 5, 9, 12, 13}); /** * { 1, 2, 3, 5, 6, 7, 10, 11, 12, 13 14} List. * {0, 2, 3, 5, 9, 12, 13, } Reference list. * 0 1 1 1 0 1 1 Matches. * 4 0 1 3 1 2 Matches blocks representation. * 3 0 0 2 0 Matches blocks concise representation. * 4 0 1 3 1 Add 1 to ensure non zeros. * 001 0 1 11 1 Binary representation. * 3-00 0 1 2-1 1 Decimal Gamma Prefix and Binary Gamma Suffix. * 11-00 0 1 01-1 1 Binary Gamma Prefix and Binary Gamma Suffix. * 01100 0 1 0101 1 Delta Encoding. */ final int expectedOffset = 12; IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteDifferenceOffset(list, referenceList); assertEquals(expectedOffset, offset); }
@Test public void whenReferenceListHasMatchesAndNoMatchAtEndItShouldReturnExpectedOffsetSuccessfully() { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11}); final IntList referenceList = new IntArrayList(new int[] {0, 2, 3, 5, 9, 12, 13}); /** * { 1, 2, 3, 5, 6, 7, 10, 11, } List. * {0, 2, 3, 5, 9, 12, 13} Reference list. * 0 1 1 1 0 0 0 Matches. * 3 0 1 3 3 Matches blocks representation. * 2 0 0 2 Matches blocks concise representation. * 3 0 1 3 Add 1 to ensure non zeros. * 11 0 1 11 Binary representation. * 2-1 0 1 2-1 Decimal Gamma Prefix and Binary Gamma Suffix. * 01-1 0 1 01- Binary Gamma Prefix and Binary Gamma Suffix. * 0101 0 1 0101 Delta Encoding. */ final int expectedOffset = 10; IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteDifferenceOffset(list, referenceList); assertEquals(expectedOffset, offset); }
@Test public void whenReferenceListHasMatchesAndListGetsIteratedFirstItShouldReturnExpectedOffsetSuccessfully() { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 12}); final IntList referenceList = new IntArrayList(new int[] {0, 2, 3, 5, 9, 11, 13}); /** * { 1, 2, 3, 5, 6, 7, 10, 11, 12 } List. * {0, 2, 3, 5, 9, 11, 13} Reference list. * 0 1 1 1 0 1 0 Matches. * 5 0 1 3 1 11 Matches blocks representation. * 4 0 0 2 0 0 Matches blocks concise representation. * 5 0 1 3 1 1 Add 1 to ensure non zeros. * 101 0 1 11 1 1 Binary representation. * 3-01 0 1 2-1 1 1 Decimal Gamma Prefix and Binary Gamma Suffix. * 11-01 0 1 01-1 1 1 Binary Gamma Prefix and Binary Gamma Suffix. * 01101 0 1 0101 1 1 Delta Encoding. */ final int expectedOffset = 13; IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteDifferenceOffset(list, referenceList); assertEquals(expectedOffset, offset); }
@Test public void whenEncodedRepetitionsListIsEmptyItShouldRecoverOriginalRepetitionsSuccessfully() throws Exception { final int originalListSize = 10; Helper.Input input = getInput("1"); /** * 1, 2, 3, 5, 6, 7, 10, 11, 16, 19 List from which the repetitions where extracted * 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 Repetition flags */ final IntList expectedRepetitions = new IntArrayList( new int[] {0, 0, 0, 0, 0, 0, 0 ,0 ,0, 0} ); final IntList repetitions = new IntArrayList(); RepeatsIterator repeatsIterator = new RepeatsIterator(input.stream); for(int i = 0; i < originalListSize; i++) { repetitions.add(repeatsIterator.next()); } assertEquals(expectedRepetitions, repetitions); }
@Test public void whenReferenceListFullyMatchesItShouldReturnExpectedOffsetSuccessfully() { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 16, 19}); final IntList referenceList = new IntArrayList(new int[] {1, 2, 3, 7, 11, 19}); /** * {1, 2, 3, 5, 6, 7, 10, 11, 16, 19} List. * {1, 2, 3, 7, 11, 19} Reference list. * 1 1 1 1 1 1 Matches. * 1 1 6 Matches blocks representation. * 0 1 Matches blocks concise representation. * 1 1 Add 1 to ensure non zeros. * 1 1 Binary representation. * 1 1 Decimal Gamma Prefix and Binary Gamma Suffix. * 1 1 Binary Gamma Prefix and Binary Gamma Suffix. * 1 1 Delta Encoding. */ final int expectedOffset = 2; IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteDifferenceOffset(list, referenceList); assertEquals(expectedOffset, offset); }
@Test public void itShouldReturnExpectedOffsetSuccessfully() { final int valueBitSize = 1; final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 7, 10}); /** * 6 1 0 0 1 1 2 Delta list. * 7 1 1 1 2 2 3 Add 1 to ensure non zeros. * 111 1 1 1 01 01 11 Binary representation. * 3-11 1 1 1 2-0 2-0 2-1 Decimal Gamma Prefix and Binary Gamma Suffix. * 11-11 1 1 1 01-0 01-0 01-1 Binary Gamma Prefix and Binary Gamma Suffix. * 01111 1 1 1 0100 0100 0101 Delta Encoding. */ final int expectedOffset = 20; IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteDeltaOffset(list, valueBitSize); assertEquals(expectedOffset, offset); }
@Test public void whenThereIsIntervalsOnListItShouldGetExpectedOffsetSuccessfully() { final int valueBitSize = 3; final IntList list = new IntArrayList( new int[] {1, 3, 4, 5, 7, 8, 9, 10, 12, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 30} ); /** * 3 [7, 8, 9, 10] [14, 15, 16, 17] [19, 20, 21, 22, 23, 24] Intervals from list. * 3 [7, 10] [14, 17] [19, 24] Interval format. * 3 [7, 0] [2, 0] [0, 2] Interval delta format. * 4 7 1 3 1 1 3 Add 1 to ensure non zeros. * 001 111 1 11 1 1 11 Binary representation. * 3-00 111 1 2-1 1 1 2-1 Decimal Gamma Prefix and Binary Gamma Suffix. * 11-00 111 1 01-1 1 1 01-1 Binary Gamma Prefix and Binary Gamma Suffix. * 01100 111 1 0101 1 1 0101 Delta Encoding. */ final int expectedOffset = 19; final IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteIntervalsOffset(list, valueBitSize); assertEquals(expectedOffset, offset); }
/** * Attempts to get a int array from the {@link DataView} * for the specific {@link DataQuery} path. * * @param dataView The data view * @param path The path * @return The int array */ public static Optional<int[]> getIntArray(DataView dataView, DataQuery path) { final Optional<Object> optObject = dataView.get(path); if (optObject.isPresent() && optObject.get() instanceof int[]) { return (Optional) optObject; } return dataView.getIntegerList(path).map(list -> { if (list instanceof IntList) { return ((IntList) list).toIntArray(); } final int[] array = new int[list.size()]; for (int i = 0; i < list.size(); i++) { array[i] = list.get(i); } return array; }); }
@Test public void whenListIsOneSingleIntervalItShouldGetExpectedOffsetSuccessfully() { final int valueBitSize = 3; final IntList list = new IntArrayList(new int[] {7, 8, 9, 10}); /** * 1 [7, 8, 9, 10] Intervals from list. * 1 [7, 10] Interval format. * 1 [7, 0] Interval delta format. * 2 7 1 Add 1 to ensure non zeros. * 01 111 1 Binary representation. * 2-0 111 1 Decimal Gamma Prefix and Binary Gamma Suffix. * 01-0 111 1 Binary Gamma Prefix and Binary Gamma Suffix. * 0100 111 1 Delta Encoding. */ final int expectedOffset = 8; final IntOutputOffset outputOffset = new IntOutputOffset(); final int offset = outputOffset.getWriteIntervalsOffset(list, valueBitSize); assertEquals(expectedOffset, offset); }
@Test public void whenReferenceListFirstElementsMatchesWithListShouldRecoverListSegmentOnReferenceListSuccessfully() throws Exception { final Helper.Input input = getInput("0100 0100 1 01100"); final int listIndex = 1; final IntList referenceList = new IntArrayList(new int[] {1, 2, 3, 5, 9, 12, 13}); final IntList expectedList = new IntArrayList(new int[] {1, 2, 3, 5}); final IntList list = new IntArrayList(); ReferenceIterator referenceIterator = new ReferenceIteratorBuilder(input, listIndex, referenceList).build(); while (referenceIterator.hasNext()) { list.add(referenceIterator.next()); } assertEquals(expectedList, list); }
@Test public void whenListSizeIsSmallerThanMinListSizeItShouldNotAddList() { final IntReferenceListsIndex referenceListsIndex = mock(IntReferenceListsIndex.class); final int size = 3; final int maxRecursiveReferences = 1; final int minListSize = 3; final IntReferenceListsStore referenceListsStore = new IntReferenceListsStore( size, maxRecursiveReferences, minListSize, referenceListsIndex ); final int listIndex = 0; final int recursiveReferences = 0; final IntList list = new IntArrayList(new int[] {1, 2}); final boolean added = referenceListsStore.add(listIndex, recursiveReferences, list); assertFalse(added); verify(referenceListsIndex, never()).removeListFromListsInvertedIndex(anyInt(), any(IntList.class)); verify(referenceListsIndex, never()).addListIntoListsInvertedIndex(anyInt(), any(IntList.class)); }
@Test public void whenAddListInANonEmptySlotItShouldAddListSuccessfully() { final IntReferenceListsIndex referenceListsIndex = mock(IntReferenceListsIndex.class); final int size = 1; final int maxRecursiveReferences = 1; final int minListSize = 3; final IntReferenceListsStore referenceListsStore = new IntReferenceListsStore( size, maxRecursiveReferences, minListSize, referenceListsIndex ); final int listIndex = 0; final int recursiveReferences = 0; final IntList previousList = new IntArrayList(new int[] {0, 3, 5, 7}); final IntList list = new IntArrayList(new int[] {1, 2, 3, 4, 5, 6}); referenceListsStore.add(listIndex, recursiveReferences, previousList); final boolean added = referenceListsStore.add(listIndex, recursiveReferences, list); assertTrue(added); verify(referenceListsIndex).removeListFromListsInvertedIndex(anyInt(), eq(previousList)); verify(referenceListsIndex).addListIntoListsInvertedIndex(anyInt(), eq(list)); }
@Test public void itShouldRemoveListSuccessfully() { final IntReferenceListsIndex referenceListsIndex = mock(IntReferenceListsIndex.class); final int size = 3; final int maxRecursiveReferences = 1; final int minListSize = 3; final IntReferenceListsStore referenceListsStore = new IntReferenceListsStore( size, maxRecursiveReferences, minListSize, referenceListsIndex ); IntReferenceListsStore.ReferenceList referenceList = mock(IntReferenceListsStore.ReferenceList.class); referenceListsStore.remove(referenceList); verify(referenceListsIndex).removeListFromListsInvertedIndex(anyInt(), any(IntList.class)); }
@Test public void whenIndexDoesNotHaveAnyPreviousIntersectingListItShouldIndexListAsExpected() { final Int2ReferenceMap<IntList> expectedListsInvertedIndex = new Int2ReferenceOpenHashMap<IntList>() {{ put(1, new IntArrayList(new int[] {0})); put(3, new IntArrayList(new int[] {0})); put(4, new IntArrayList(new int[] {0})); put(5, new IntArrayList(new int[] {0})); put(8, new IntArrayList(new int[] {0})); }}; final InvertedListIntReferenceListsIndex listsIndex = new InvertedListIntReferenceListsIndex(); final int index = 0; final IntList list = new IntArrayList(new int[] {1, 3, 4, 5, 8}); listsIndex.addListIntoListsInvertedIndex(index, list); assertEquals( Helper.<Integer, Int2ReferenceMap<IntList>>translateToUtilsCollection(expectedListsInvertedIndex), Helper.<Integer, Int2ReferenceMap<IntList>>translateToUtilsCollection(listsIndex.listsInvertedIndex) ); }
@Test public void whenIndexHavePreviousIntersectingListItShouldIndexListAsExpected() { final Int2ReferenceMap<IntList> expectedListsInvertedIndex = new Int2ReferenceOpenHashMap<IntList>() {{ put(1, new IntArrayList(new int[] {1})); put(2, new IntArrayList(new int[] {0})); put(3, new IntArrayList(new int[] {0, 1})); put(4, new IntArrayList(new int[] {1})); put(5, new IntArrayList(new int[] {0, 1})); put(8, new IntArrayList(new int[] {1})); }}; final InvertedListIntReferenceListsIndex listsIndex = new InvertedListIntReferenceListsIndex(); listsIndex.listsInvertedIndex.put(2, new IntArrayList(new int[]{0})); listsIndex.listsInvertedIndex.put(3, new IntArrayList(new int[]{0})); listsIndex.listsInvertedIndex.put(5, new IntArrayList(new int[]{0})); final int index = 1; final IntList list = new IntArrayList(new int[] {1, 3, 4, 5, 8}); listsIndex.addListIntoListsInvertedIndex(index, list); assertEquals( Helper.<Integer, Int2ReferenceMap<IntList>>translateToUtilsCollection(expectedListsInvertedIndex), Helper.<Integer, Int2ReferenceMap<IntList>>translateToUtilsCollection(listsIndex.listsInvertedIndex) ); }
@Test public void whenReferenceListHasMatchesItShouldWriteItsSuccinctRepresentationSuccessfully() throws Exception { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 16, 19}); final IntList referenceList = new IntArrayList(new int[] {0, 2, 3, 5, 9, 12, 13}); /** * { 1, 2, 3, 5, 6, 7, 10, 11, 16, 19} List. * {0, 2, 3, 5, 9, 12, 13 } Reference list. * 0 1 1 1 0 0 0 Matches. * 3 0 1 3 3 Matches blocks representation. * 2 0 0 2 Matches blocks concise representation. * 3 0 1 3 Add 1 to ensure non zeros. * 11 0 1 11 Binary representation. * 2-1 0 1 2-1 Decimal Gamma Prefix and Binary Gamma Suffix. * 01-1 0 1 01-1 Binary Gamma Prefix and Binary Gamma Suffix. * 0101 0 1 0101 Delta Encoding. */ final String expectedOutput = "0101 0 1 0101".replace(" ", ""); final int expectedOffset = 10; final Helper.Output out = getOutput(); final int offset = out.stream.writeDifference(list, referenceList); out.close(); assertEquals(expectedOutput, toBinaryString(out.buffer, offset)); assertEquals(expectedOffset, offset); }
@Override public ByteBuffer encode(CodecContext context, MessagePlayOutUnlockRecipes message) throws CodecException { final ByteBuffer buf = context.byteBufAlloc().buffer(); if (message instanceof MessagePlayOutUnlockRecipes.Remove) { buf.writeVarInt((short) 2); } else if (message instanceof MessagePlayOutUnlockRecipes.Add) { buf.writeVarInt((short) 1); } else if (message instanceof MessagePlayOutUnlockRecipes.Init) { buf.writeVarInt((short) 0); } else { throw new EncoderException(); } buf.writeBoolean(message.hasOpenCraftingBook()); buf.writeBoolean(message.hasCraftingFilter()); IntList recipeIds = message.getRecipeIds(); buf.writeVarInt(recipeIds.size()); recipeIds.forEach(buf::writeVarInt); if (message instanceof MessagePlayOutUnlockRecipes.Init) { recipeIds = ((MessagePlayOutUnlockRecipes.Init) message).getRecipeIdsToBeDisplayed(); buf.writeVarInt(recipeIds.size()); recipeIds.forEach(buf::writeVarInt); } return buf; }
@Test public void whenReferenceListFullyMatchesItShouldWriteItsSuccinctRepresentationSuccessfully() throws Exception { final IntList list = new IntArrayList(new int[] {1, 2, 3, 5, 6, 7, 10, 11, 16, 19}); final IntList referenceList = new IntArrayList(new int[] {1, 2, 3, 7, 11, 19}); /** * {1, 2, 3, 5, 6, 7, 10, 11, 16, 19} List. * {1, 2, 3, 7, 11, 19} Reference list. * 1 1 1 1 1 1 Matches. * 1 1 6 Matches blocks representation. * 0 1 Matches blocks concise representation. * 1 1 Add 1 to ensure non zeros. * 1 1 Binary representation. * 1 1 Decimal Gamma Prefix and Binary Gamma Suffix. * 1 1 Binary Gamma Prefix and Binary Gamma Suffix. * 1 1 Delta Encoding. */ final String expectedOutput = "1 1".replace(" ", ""); final int expectedOffset = 2; final Helper.Output out = getOutput(); final int offset = out.stream.writeDifference(list, referenceList); out.close(); assertEquals(expectedOutput, toBinaryString(out.buffer, offset)); assertEquals(expectedOffset, offset); }
@Test public void readDeltaItShouldReadExpectedValues() throws IOException { final IntList expectedValues = new IntArrayList(new int[] {17, 4, 6}); /** * 18 5 7 Increment one. * 10010 101 111 Binary representation. * 5-0010 3-01 3-11 Decimal Gamma Prefix and Binary Gamma Suffix. * 101-0010 11-01 11-11 Binary Gamma Prefix and Binary Gamma Suffix. * 001010010 01101 01111 Delta Encoding. */ final Helper.Input input = getInput("001010010 01101 01111"); final InputBitStream inputStream = new InputBitStream(input.buffer); final IntList values = new IntArrayList(); for (int i = 0; i < expectedValues.size(); i++) { values.add(inputStream.readDelta()); } assertEquals(expectedValues, values); }
@Test public void whenThereIsAnEncodedNonEmptyIntervalsListItShouldRecoverOriginalIntervalsSuccessfully() throws Exception { Helper.Input input = getInput("01100 0000000000000000000000000000111 1 0101 1 1 0101"); final IntList expectedList = new IntArrayList(new int[] {7, 8, 9, 10, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24}); final IntList list = new IntArrayList(); IntervalIterator intervalIterator = new IntervalIterator( DefaultParametersValues.INT_BITS, DefaultParametersValues.DEFAULT_MIN_INTERVAL_SIZE, input.stream ); while (intervalIterator.hasNext()) { list.add(intervalIterator.next()); } assertEquals(expectedList, list); }