Java Code Examples for java.util.Arrays#mismatch()

The following examples show how to use java.util.Arrays#mismatch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestSocketFactory.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(enabled = true, dataProvider = "MatchReplaceData")
static void test3(byte[] match, byte[] replace,
                  byte[] input, byte[] expected) {
    System.out.printf("match: %s, replace: %s%n", Arrays.toString(match), Arrays.toString(replace));
    try (ByteArrayOutputStream output = new ByteArrayOutputStream();
    ByteArrayOutputStream log = new ByteArrayOutputStream();
         OutputStream out = new MatchReplaceOutputStream(output, "test3",
                 log, match, replace)) {
        out.write(input);
        byte[] actual = output.toByteArray();
        long index = Arrays.mismatch(actual, expected);

        if (index >= 0) {
            System.out.printf("array mismatch, offset: %d%n", index);
            System.out.printf("actual: %s%n", Arrays.toString(actual));
            System.out.printf("expected: %s%n", Arrays.toString(expected));
        }
        Assert.assertEquals(actual, expected, "match/replace fail");
    } catch (IOException ioe) {
        Assert.fail("unexpected exception", ioe);
    }
}
 
Example 2
Source File: SimpleTextBKDWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void computeCommonPrefixLength(HeapPointWriter heapPointWriter, byte[] commonPrefix) {
  Arrays.fill(commonPrefixLengths, bytesPerDim);
  PointValue value = heapPointWriter.getPackedValueSlice(0);
  BytesRef packedValue = value.packedValue();
  for (int dim = 0; dim < numDataDims; dim++) {
    System.arraycopy(packedValue.bytes, packedValue.offset + dim * bytesPerDim, commonPrefix, dim * bytesPerDim, bytesPerDim);
  }
  for (int i = 1; i < heapPointWriter.count(); i++) {
    value = heapPointWriter.getPackedValueSlice(i);
    packedValue = value.packedValue();
    for (int dim = 0; dim < numDataDims; dim++) {
      if (commonPrefixLengths[dim] != 0) {
        int j = Arrays.mismatch(commonPrefix, dim * bytesPerDim, dim * bytesPerDim + commonPrefixLengths[dim], packedValue.bytes, packedValue.offset + dim * bytesPerDim, packedValue.offset + dim * bytesPerDim + commonPrefixLengths[dim]);
        if (j != -1) {
          commonPrefixLengths[dim] = j;
        }
      }
    }
  }
}
 
Example 3
Source File: BKDWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void computeCommonPrefixLength(HeapPointWriter heapPointWriter, byte[] commonPrefix, int from, int to) {
  Arrays.fill(commonPrefixLengths, bytesPerDim);
  PointValue value = heapPointWriter.getPackedValueSlice(from);
  BytesRef packedValue = value.packedValue();
  for (int dim = 0; dim < numDataDims; dim++) {
    System.arraycopy(packedValue.bytes, packedValue.offset + dim * bytesPerDim, commonPrefix, dim * bytesPerDim, bytesPerDim);
  }
  for (int i = from + 1; i < to; i++) {
    value =  heapPointWriter.getPackedValueSlice(i);
    packedValue = value.packedValue();
    for (int dim = 0; dim < numDataDims; dim++) {
      if (commonPrefixLengths[dim] != 0) {
        int j = Arrays.mismatch(commonPrefix, dim * bytesPerDim, dim * bytesPerDim + commonPrefixLengths[dim], packedValue.bytes, packedValue.offset + dim * bytesPerDim, packedValue.offset + dim * bytesPerDim + commonPrefixLengths[dim]);
        if (j != -1) {
          commonPrefixLengths[dim] = j;
        }
      }
    }
  }
}
 
Example 4
Source File: TestBKDRadixSort.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** returns a common prefix length equal or lower than the current one */
private int getRandomCommonPrefix(HeapPointWriter points, int start, int end, int bytesPerDimension, int sortDim)  {
  int commonPrefixLength = bytesPerDimension;
  PointValue value = points.getPackedValueSlice(start);
  BytesRef bytesRef = value.packedValue();
  byte[] firstValue = new byte[bytesPerDimension];
  int offset = sortDim * bytesPerDimension;
  System.arraycopy(bytesRef.bytes, bytesRef.offset + offset, firstValue, 0, bytesPerDimension);
  for (int i = start + 1; i < end; i++) {
    value = points.getPackedValueSlice(i);
    bytesRef = value.packedValue();
    int diff = Arrays.mismatch(bytesRef.bytes, bytesRef.offset + offset, bytesRef.offset + offset + bytesPerDimension, firstValue, 0, bytesPerDimension);
    if (diff != -1 && commonPrefixLength > diff) {
      if (diff == 0) {
        return diff;
      }
      commonPrefixLength = diff;
    }
  }
  return (random().nextBoolean()) ? commonPrefixLength : random().nextInt(commonPrefixLength);
}
 
Example 5
Source File: TestBKDRadixSelector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private byte[] getMinDataDimension(BKDRadixSelector.PathSlice p, int bytesPerDimension, int dataDims, int indexDims, byte[] minDim, int splitDim) throws  IOException {
  byte[] min = new byte[(dataDims - indexDims) * bytesPerDimension];
  Arrays.fill(min, (byte) 0xff);
  int offset = splitDim * bytesPerDimension;
  try (PointReader reader = p.writer.getReader(p.start, p.count)) {
    byte[] value = new byte[(dataDims - indexDims) * bytesPerDimension];
    while (reader.next()) {
      PointValue pointValue = reader.pointValue();
      BytesRef packedValue = pointValue.packedValue();
      if (Arrays.mismatch(minDim, 0, bytesPerDimension, packedValue.bytes, packedValue.offset + offset, packedValue.offset + offset + bytesPerDimension) == -1) {
        System.arraycopy(packedValue.bytes, packedValue.offset + indexDims * bytesPerDimension, value, 0, (dataDims - indexDims) * bytesPerDimension);
        if (Arrays.compareUnsigned(min, 0, (dataDims - indexDims) * bytesPerDimension, value, 0, (dataDims - indexDims) * bytesPerDimension) > 0) {
          System.arraycopy(value, 0, min, 0, (dataDims - indexDims) * bytesPerDimension);
        }
      }
    }
  }
  return min;
}
 
Example 6
Source File: TestBKDRadixSelector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private byte[] getMaxDataDimension(BKDRadixSelector.PathSlice p, int bytesPerDimension, int dataDims, int indexDims, byte[] maxDim, int splitDim) throws  IOException {
  byte[] max = new byte[(dataDims - indexDims) * bytesPerDimension];
  Arrays.fill(max, (byte) 0);
  int offset = splitDim * bytesPerDimension;
  try (PointReader reader = p.writer.getReader(p.start, p.count)) {
    byte[] value = new byte[(dataDims - indexDims) * bytesPerDimension];
    while (reader.next()) {
      PointValue pointValue = reader.pointValue();
      BytesRef packedValue = pointValue.packedValue();
      if (Arrays.mismatch(maxDim, 0, bytesPerDimension, packedValue.bytes, packedValue.offset + offset, packedValue.offset + offset + bytesPerDimension) == -1) {
        System.arraycopy(packedValue.bytes, packedValue.offset + indexDims * bytesPerDimension, value, 0, (dataDims - indexDims) * bytesPerDimension);
        if (Arrays.compareUnsigned(max, 0, (dataDims - indexDims) * bytesPerDimension, value, 0, (dataDims - indexDims) * bytesPerDimension) < 0) {
          System.arraycopy(value, 0, max, 0, (dataDims - indexDims) * bytesPerDimension);
        }
      }
    }
  }
  return max;
}
 
Example 7
Source File: FastHit.java    From vespa with Apache License 2.0 6 votes vote down vote up
static int compareSortData(FastHit left, FastHit right, Sorting sorting) {
    if (!left.hasSortData(sorting) || !right.hasSortData(sorting)) {
        return 0; // cannot sort
    }
    int i = Arrays.mismatch(left.sortData, right.sortData);
    if (i < 0) {
        return 0;
    }
    int max = Integer.min(left.sortData.length, right.sortData.length);
    if (i >= max) {
        return left.sortData.length - right.sortData.length;
    }
    int vl = (int) left.sortData[i] & 0xFF;
    int vr = (int) right.sortData[i] & 0xFF;
    return vl - vr;
}
 
Example 8
Source File: BKDWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void writeLowCardinalityLeafBlockPackedValues(DataOutput out, int[] commonPrefixLengths, int count, IntFunction<BytesRef> packedValues) throws IOException {
  if (numIndexDims != 1) {
    writeActualBounds(out, commonPrefixLengths, count, packedValues);
  }
  BytesRef value = packedValues.apply(0);
  System.arraycopy(value.bytes, value.offset, scratch1, 0, packedBytesLength);
  int cardinality = 1;
  for (int i = 1; i < count; i++) {
    value = packedValues.apply(i);
    for(int dim = 0; dim < numDataDims; dim++) {
      final int start = dim * bytesPerDim + commonPrefixLengths[dim];
      final int end = dim * bytesPerDim + bytesPerDim;
      if (Arrays.mismatch(value.bytes, value.offset + start, value.offset + end, scratch1, start, end) != -1) {
        out.writeVInt(cardinality);
        for (int j = 0; j < numDataDims; j++) {
          out.writeBytes(scratch1, j * bytesPerDim + commonPrefixLengths[j], bytesPerDim - commonPrefixLengths[j]);
        }
        System.arraycopy(value.bytes, value.offset, scratch1, 0, packedBytesLength);
        cardinality = 1;
        break;
      } else if (dim == numDataDims - 1){
        cardinality++;
      }
    }
  }
  out.writeVInt(cardinality);
  for (int i = 0; i < numDataDims; i++) {
    out.writeBytes(scratch1, i * bytesPerDim + commonPrefixLengths[i], bytesPerDim - commonPrefixLengths[i]);
  }
}
 
Example 9
Source File: CharsRef.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public int compare(CharsRef a, CharsRef b) {
  int aEnd = a.offset + a.length;
  int bEnd = b.offset + b.length;
  int i = Arrays.mismatch(a.chars, a.offset, aEnd, 
                                b.chars, b.offset, bEnd);

  if (i >= 0 && i < Math.min(a.length, b.length)) {
    // http://icu-project.org/docs/papers/utf16_code_point_order.html

    char aChar = a.chars[a.offset + i];
    char bChar = b.chars[b.offset + i];        
    /* aChar != bChar, fix up each one if they're both in or above the surrogate range, then compare them */
    if (aChar >= 0xd800 && bChar >= 0xd800) {
      if (aChar >= 0xe000) {
        aChar -= 0x800;
      } else {
        aChar += 0x2000;
      }
      
      if (bChar >= 0xe000) {
        bChar -= 0x800;
      } else {
        bChar += 0x2000;
      }
    }

    /* now aChar and bChar are in code point order */
    return (int)aChar - (int)bChar; /* int must be 32 bits wide */
  }

  // One is a prefix of the other, or, they are equal:
  return a.length - b.length;
}
 
Example 10
Source File: TestBKDRadixSelector.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** returns a common prefix length equal or lower than the current one */
private int getRandomCommonPrefix(BKDRadixSelector.PathSlice inputSlice, int bytesPerDimension, int splitDim) throws IOException {
  byte[] pointsMax = getMax(inputSlice, bytesPerDimension, splitDim);
  byte[] pointsMin = getMin(inputSlice, bytesPerDimension, splitDim);
  int commonPrefixLength = Arrays.mismatch(pointsMin, 0, bytesPerDimension, pointsMax, 0, bytesPerDimension);
  if (commonPrefixLength == -1) {
    commonPrefixLength = bytesPerDimension;
  }
  return (random().nextBoolean()) ? commonPrefixLength : commonPrefixLength == 0 ? 0 : random().nextInt(commonPrefixLength);
}
 
Example 11
Source File: LeanHit.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static int compareData(byte [] left, byte [] right) {
    int i = Arrays.mismatch(left, right);
    if (i < 0) {
        return 0;
    }
    int max = Integer.min(left.length, right.length);
    if (i >= max) {
        return left.length - right.length;
    }
    int vl = (int) left[i] & 0xFF;
    int vr = (int) right[i] & 0xFF;
    return vl - vr;
}
 
Example 12
Source File: Main.java    From Java-Coding-Problems with MIT License 4 votes vote down vote up
public static void main(String[] args) {

        // arrays of integers
        int[] integers1 = new int[]{3, 4, 5, 6, 1, 5};
        int[] integers2 = new int[]{3, 4, 5, 6, 1, 5};
        int[] integers3 = new int[]{3, 4, 5, 6, 1, 3};

        // arrays of melons
        Melon[] melons1 = new Melon[]{new Melon("Horned", 1500), new Melon("Gac", 1000)};
        Melon[] melons2 = new Melon[]{new Melon("Horned", 1500), new Melon("Gac", 1000)};
        Melon[] melons3 = new Melon[]{new Melon("Hami", 1500), new Melon("Gac", 1000)};

        // Comparators
        Comparator<Melon> byType = Comparator.comparing(Melon::getType);
        Comparator<Melon> byWeight = Comparator.comparing(Melon::getWeight);

        System.out.println("equals:\n------\n");

        // integers1 equals integers2
        boolean i12 = Arrays.equals(integers1, integers2);
        System.out.println("integers 1 equal integers 2? " + i12);

        // integers1 equals integers3
        boolean i13 = Arrays.equals(integers1, integers3);
        System.out.println("integers 1 equal integers 3? " + i13);

        // integers1 equals integers3
        boolean is13 = Arrays.equals(integers1, 1, 4, integers3, 1, 4);
        System.out.println("integers 1 equal integers 3 between indexes 1-4? " + is13);

        // melons1 equals melons2
        boolean m12 = Arrays.equals(melons1, melons2);
        System.out.println("melons1 equals melons2? " + m12);

        // melons1 equals melons3
        boolean m13 = Arrays.equals(melons1, melons3);
        System.out.println("melons1 equals melons3? " + m13);

        // melons1 equals melons3
        boolean ms13 = Arrays.equals(melons1, 1, 2, melons3, 1, 2);
        System.out.println("melons1 equals melons3 between indexes 1-2? " + ms13);

        // melons1 equals melon3 via Comparator by weight        
        boolean mw13 = Arrays.equals(melons1, melons3, byWeight);
        System.out.println("melons1 equals melons3 by weight? " + mw13);

        // melons1 equals melon3 via Comparator by type        
        boolean mt13 = Arrays.equals(melons1, melons3, byType);
        System.out.println("melons1 equals melons3 by type? " + mt13);

        // melons1 equals melon3 in range via Comparator by type        
        boolean mrt13 = Arrays.equals(melons1, 1, 2, melons3, 1, 2, byType);
        System.out.println("melons1 equals melons3 by type between indexes 1-2? " + mrt13);

        System.out.println("\n\nmismatch:\n--------\n");

        // integers1 mismatch integers2
        int mi12 = Arrays.mismatch(integers1, integers2);
        System.out.println("integers 1 mismatch integers 2? " + mi12);

        // integers1 mismatch integers3
        int mi13 = Arrays.mismatch(integers1, integers3);
        System.out.println("integers 1 mismatch integers 3? " + mi13);

        // integers1 mismatch integers3
        int mis13 = Arrays.mismatch(integers1, 1, 4, integers3, 1, 4);
        System.out.println("integers 1 mismatch integers 3 between indexes 1-4? " + mis13);

        // melons1 mismatch melons2
        int mm12 = Arrays.mismatch(melons1, melons2);
        System.out.println("melons1 mismatch melons2? " + mm12);

        // melons1 mismatch melons3
        int mm13 = Arrays.mismatch(melons1, melons3);
        System.out.println("melons1 mismatch melons3? " + mm13);

        // melons1 mismatch melons3
        int mms13 = Arrays.mismatch(melons1, 1, 2, melons3, 1, 2);
        System.out.println("melons1 mismatch melons3 between indexes 1-2? " + mms13);

        // melons1 mismatch melon3 via Comparator by weight
        int mmw13 = Arrays.mismatch(melons1, melons3, byWeight);
        System.out.println("melons1 mismatch melons3 by weight? " + mmw13);

        // melons1 mismatch melon3 via Comparator by type
        int mmt13 = Arrays.mismatch(melons1, melons3, byType);
        System.out.println("melons1 mismatch melons3 by type? " + mmt13);

        // melons1 mismatch melon3 in range via Comparator by type
        int mmrt13 = Arrays.mismatch(melons1, 1, 2, melons3, 1, 2, byType);
        System.out.println("melons1 mismatch melons3 by type between indexes 1-2? " + mmrt13);
    }
 
Example 13
Source File: Files.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Finds and returns the position of the first mismatched byte in the content
 * of two files, or {@code -1L} if there is no mismatch. The position will be
 * in the inclusive range of {@code 0L} up to the size (in bytes) of the
 * smaller file.
 *
 * <p> Two files are considered to match if they satisfy one of the following
 * conditions:
 * <ul>
 * <li> The two paths locate the {@linkplain #isSameFile(Path, Path) same file},
 *      even if two {@linkplain Path#equals(Object) equal} paths locate a file
 *      does not exist, or </li>
 * <li> The two files are the same size, and every byte in the first file
 *      is identical to the corresponding byte in the second file. </li>
 * </ul>
 *
 * <p> Otherwise there is a mismatch between the two files and the value
 * returned by this method is:
 * <ul>
 * <li> The position of the first mismatched byte, or </li>
 * <li> The size of the smaller file (in bytes) when the files are different
 *      sizes and every byte of the smaller file is identical to the
 *      corresponding byte of the larger file. </li>
 * </ul>
 *
 * <p> This method may not be atomic with respect to other file system
 * operations. This method is always <i>reflexive</i> (for {@code Path f},
 * {@code mismatch(f,f)} returns {@code -1L}). If the file system and files
 * remain static, then this method is <i>symmetric</i> (for two {@code Paths f}
 * and {@code g}, {@code mismatch(f,g)} will return the same value as
 * {@code mismatch(g,f)}).
 *
 * @param   path
 *          the path to the first file
 * @param   path2
 *          the path to the second file
 *
 * @return  the position of the first mismatch or {@code -1L} if no mismatch
 *
 * @throws  IOException
 *          if an I/O error occurs
 * @throws  SecurityException
 *          In the case of the default provider, and a security manager is
 *          installed, the {@link SecurityManager#checkRead(String) checkRead}
 *          method is invoked to check read access to both files.
 *
 * @since 12
 */
public static long mismatch(Path path, Path path2) throws IOException {
    if (isSameFile(path, path2)) {
        return -1;
    }
    byte[] buffer1 = new byte[BUFFER_SIZE];
    byte[] buffer2 = new byte[BUFFER_SIZE];
    try (InputStream in1 = Files.newInputStream(path);
         InputStream in2 = Files.newInputStream(path2);) {
        long totalRead = 0;
        while (true) {
            int nRead1 = in1.readNBytes(buffer1, 0, BUFFER_SIZE);
            int nRead2 = in2.readNBytes(buffer2, 0, BUFFER_SIZE);

            int i = Arrays.mismatch(buffer1, 0, nRead1, buffer2, 0, nRead2);
            if (i > -1) {
                return totalRead + i;
            }
            if (nRead1 < BUFFER_SIZE) {
                // we've reached the end of the files, but found no mismatch
                return -1;
            }
            totalRead += nRead1;
        }
    }
}
 
Example 14
Source File: StringHelper.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Compares two {@link BytesRef}, element by element, and returns the
 * number of elements common to both arrays (from the start of each).
 * This method assumes currentTerm comes after priorTerm.
 *
 * @param priorTerm The first {@link BytesRef} to compare
 * @param currentTerm The second {@link BytesRef} to compare
 * @return The number of common elements (from the start of each).
 */
public static int bytesDifference(BytesRef priorTerm, BytesRef currentTerm) {
  int mismatch = Arrays.mismatch(priorTerm.bytes, priorTerm.offset, priorTerm.offset + priorTerm.length, 
                                       currentTerm.bytes, currentTerm.offset, currentTerm.offset + currentTerm.length);
  if (mismatch < 0) {
    throw new IllegalArgumentException("terms out of order: priorTerm=" + priorTerm + ",currentTerm=" + currentTerm);
  }
  return mismatch;
}