Ejemplo n.º 1
0
 @Override
 public void collect(int doc) throws IOException {
   if (values != null) {
     int ord = (int) ordinals.getOrd(doc);
     long parentIdx = parentIdsIndex.get(ord);
     if (parentIdx < 0) {
       final BytesRef bytes = values.getValueByOrd(ord);
       final int hash = values.currentValueHash();
       parentIdx = parentIds.add(bytes, hash);
       if (parentIdx < 0) {
         parentIdx = -parentIdx - 1;
         scores.increment(parentIdx, scorer.score());
         occurrences.increment(parentIdx, 1);
       } else {
         scores = bigArrays.grow(scores, parentIdx + 1);
         scores.set(parentIdx, scorer.score());
         occurrences = bigArrays.grow(occurrences, parentIdx + 1);
         occurrences.set(parentIdx, 1);
       }
       parentIdsIndex.set(ord, parentIdx);
     } else {
       scores.increment(parentIdx, scorer.score());
       occurrences.increment(parentIdx, 1);
     }
   }
 }
  public static void main(String[] args) {
    final int size = 10_000;
    IntArray ia = new IntArray(size);
    IntArrayVolatile iav = new IntArrayVolatile(size);

    Mark7("IntArray", i -> ia.isSorted() ? 1.0 : 0.0);
    Mark7("IntArrayVolatile", i -> iav.isSorted() ? 1.0 : 0.0);
    Mark7("IntArray", i -> ia.isSorted() ? 1.0 : 0.0);
    Mark7("IntArrayVolatile", i -> iav.isSorted() ? 1.0 : 0.0);
  }
 @NotNull
 public synchronized int[] get(final int id) {
   assertPointer(id);
   try {
     if (id >= pointers.size) {
       return ArrayUtil.EMPTY_INT_ARRAY;
     }
     int arrayBase = pointers.get(id);
     IntArray array = arrayBase == 0 ? EMPTY : new IntArray(data, arrayBase);
     return array.toArray();
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Ejemplo n.º 4
0
 public void testIntArrayGrowth() {
   final int totalLen = randomIntBetween(1, 1000000);
   final int startLen = randomIntBetween(1, randomBoolean() ? 1000 : totalLen);
   IntArray array = bigArrays.newIntArray(startLen, randomBoolean());
   int[] ref = new int[totalLen];
   for (int i = 0; i < totalLen; ++i) {
     ref[i] = randomInt();
     array = bigArrays.grow(array, i + 1);
     array.set(i, ref[i]);
   }
   for (int i = 0; i < totalLen; ++i) {
     assertEquals(ref[i], array.get(i));
   }
   array.close();
 }
 private IntArray reallocWith(ByteBuffer bytes, int maxSize) throws IOException {
   assert maxSize > 0 && maxSize < MAX_LIST_LENGTH : maxSize;
   int newSize = Math.max(maxSize, bytes.limit() / 4);
   int newCapacity = newSize < 10 ? (newSize + 1) * 2 : newSize * 3 / 2;
   int newBase = (int) data.size();
   writeInt(data, newBase, newSize);
   writeInt(data, newBase + 4, newCapacity);
   bytes.position(0);
   data.write(bytes, newBase + 8);
   fillWithZeros(data, newBase + 8 + newSize * 4, (newCapacity - newSize) * 4);
   IntArray array = new IntArray(data, newBase);
   assert array.size == newSize;
   assert array.capacity == newCapacity;
   assert array.base == newBase;
   array.assertListLength();
   return array;
 }
Ejemplo n.º 6
0
  protected int binarySearchForNearest(int val, int begin, int end) {

    int mid = (begin + end) / 2;
    int midval = array.get(mid);

    if (mid == end) return midval >= val ? mid : -1;

    if (midval < val) {
      // Find number equal or greater than the target.
      if (array.get(mid + 1) >= val) return mid + 1;

      return binarySearchForNearest(val, mid + 1, end);
    } else {
      // Find number equal or greater than the target.
      if (midval == val) return mid;

      return binarySearchForNearest(val, begin, mid);
    }
  }
  public synchronized void addAll(final int id, @NotNull final int[] values) {
    assert 0 < values.length && values.length <= MAX_LIST_LENGTH : values.length;
    assert id > 0;
    Arrays.sort(values);
    try {
      if (id >= pointers.size) {
        pointers = pointers.reallocWith(pointers.toBuffer(), id + 1);
        writeInt(data, 0, pointers.base);
        assert pointers.size > id : id + " > " + pointers.size;
      }
      int arrayBase = pointers.get(id);
      IntArray array = arrayBase == 0 ? EMPTY : new IntArray(data, arrayBase);
      IntArray newArray = array.addAll(values);
      if (newArray != null) {
        pointers.put(id, newArray.base);
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    int[] ids = get(id);
    assertSorted(ids);
    TIntHashSet set = new TIntHashSet(ids);
    assert set.containsAll(values)
        : "ids: " + Arrays.toString(ids) + ";\n values:" + Arrays.toString(values);
  }
Ejemplo n.º 8
0
  public static @infix void toString(Object o, StringBuilder builder) {
    if (o == null) {
      builder.append("null");
      return;
    }

    Class c = o.getClass();
    if (c == String.class) {
      builder.append((String) o);
      return;
    }
    if (c == boolean[].class) {
      BooleanArray.toString((boolean[]) o, builder);
      return;
    }
    if (c == byte[].class) {
      ByteArray.toString((byte[]) o, builder);
      return;
    }
    if (c == short[].class) {
      ShortArray.toString((short[]) o, builder);
      return;
    }
    if (c == char[].class) {
      CharArray.toString((char[]) o, builder);
      return;
    }
    if (c == int[].class) {
      IntArray.toString((int[]) o, builder);
      return;
    }
    if (c == long[].class) {
      LongArray.toString((long[]) o, builder);
      return;
    }
    if (c == float[].class) {
      FloatArray.toString((float[]) o, builder);
      return;
    }
    if (c == double[].class) {
      DoubleArray.toString((double[]) o, builder);
      return;
    }
    if (c.isArray()) {
      deepToString((Object[]) o, builder);
      return;
    }

    builder.append(o.toString());
  }
Ejemplo n.º 9
0
      @Override
      public int advance(int target) throws IOException {
        if (remaining == 0) {
          return currentDocId = NO_MORE_DOCS;
        }

        currentDocId = parentsIterator.advance(target);
        if (currentDocId == DocIdSetIterator.NO_MORE_DOCS) {
          return currentDocId;
        }

        bytesValues.setDocument(currentDocId);
        long index = parentIds.find(bytesValues.nextValue(), bytesValues.currentValueHash());
        if (index != -1) {
          currentScore = scores.get(index);
          currentScore /= occurrences.get(index);
          remaining--;
          return currentDocId;
        } else {
          return nextDoc();
        }
      }
Ejemplo n.º 10
0
  public VzOFF(String path, VzMesh.Style style) throws IOException {
    this.path = path;
    this.style = style;

    BufferedReader ins = new BufferedReader(new FileReader(new File(path)));

    FloatArray vertexArray = new FloatArray();
    IntArray indexArray = new IntArray();
    FloatArray normalArray;

    String header = ins.readLine();
    if (!header.equals("OFF")) throw new IOException("Not an OFF file");

    int nvertexArray, nfaces, nedges;
    if (true) {
      String sizes = ins.readLine();
      String toks[] = fastSplit(sizes);
      nvertexArray = Integer.parseInt(toks[0]);
      nfaces = Integer.parseInt(toks[1]);
      nedges = Integer.parseInt(toks[2]);
    }

    for (int i = 0; i < nvertexArray; i++) {
      String line = ins.readLine();
      String toks[] = fastSplit(line);

      float x = Float.parseFloat(toks[0]);
      float y = Float.parseFloat(toks[1]);
      float z = Float.parseFloat(toks[2]);

      xyz_min[0] = Math.min(xyz_min[0], x);
      xyz_min[1] = Math.min(xyz_min[1], y);
      xyz_min[2] = Math.min(xyz_min[2], z);

      xyz_max[0] = Math.max(xyz_max[0], x);
      xyz_max[1] = Math.max(xyz_max[1], y);
      xyz_max[2] = Math.max(xyz_max[2], z);

      vertexArray.add(x);
      vertexArray.add(y);
      vertexArray.add(z);
    }

    float vs[] = vertexArray.getData();
    float ns[] = new float[vs.length];
    normalArray = new FloatArray(ns);

    for (int i = 0; i < nfaces; i++) {
      String line = ins.readLine();
      String toks[] = fastSplit(line);

      int len = Integer.parseInt(toks[0]);
      assert (len + 1 == toks.length);

      for (int j = 2; j + 1 <= len; j++) {
        int a = Integer.parseInt(toks[1]);
        int b = Integer.parseInt(toks[j]);
        int c = Integer.parseInt(toks[j + 1]);

        indexArray.add(a);
        indexArray.add(b);
        indexArray.add(c);

        float vba[] =
            new float[] {
              vs[b * 3 + 0] - vs[a * 3 + 0],
              vs[b * 3 + 1] - vs[a * 3 + 1],
              vs[b * 3 + 2] - vs[a * 3 + 2]
            };

        float vca[] =
            new float[] {
              vs[c * 3 + 0] - vs[a * 3 + 0],
              vs[c * 3 + 1] - vs[a * 3 + 1],
              vs[c * 3 + 2] - vs[a * 3 + 2]
            };

        float n[] = LinAlg.normalize(LinAlg.crossProduct(vba, vca));

        for (int k = 0; k < 3; k++) {
          ns[3 * a + k] += n[k];
          ns[3 * b + k] += n[k];
          ns[3 * c + k] += n[k];
        }
      }
    }

    for (int i = 0; i + 2 < ns.length; i += 3) {
      double mag = Math.sqrt(ns[i + 0] * ns[i + 0] + ns[i + 1] * ns[i + 1] + ns[i + 2] * ns[i + 2]);
      ns[i + 0] /= mag;
      ns[i + 1] /= mag;
      ns[i + 2] /= mag;
    }

    mesh =
        new VzMesh(
            new VisVertexData(vs, vs.length / 3, 3),
            new VisVertexData(ns, ns.length / 3, 3),
            new VisIndexData(indexArray),
            VzMesh.TRIANGLES);
    ins.close();
  }
Ejemplo n.º 11
0
 /** Returns a new array containing the remaining values. */
 public IntArray toArray() {
   IntArray array = new IntArray(true, map.size);
   while (hasNext) array.add(next());
   return array;
 }
Ejemplo n.º 12
0
 @Override
 public void addDoc(int docid) {
   ++pos;
   array.add(docid);
 }
Ejemplo n.º 13
0
 @Override
 public long sizeInBytes() {
   // Object Overhead
   return array.length() * 4 + 64;
 }