List of usage examples for org.apache.hadoop.util QuickSort sort
@Override public void sort(IndexedSortable s, int p, int r)
From source file:com.linkedin.cubert.memory.LookUpTable.java
License:Open Source License
private void buildTable() throws IOException { QuickSort quickSort = new QuickSort(); long start, end; /* Sort the offsets array */ start = System.currentTimeMillis(); quickSort.sort(sortable, 0, offsetArr.length); end = System.currentTimeMillis(); print.f("LookUpTable: Sorted %d entries in %d ms", offsetArr.length, (end - start)); /* Fill in the HashCode array */ start = System.currentTimeMillis(); int prevHashCode = -1; Tuple prevTuple = newTuple();//from www . j a v a 2s. c o m Tuple t = newTuple(); for (int i = 0; i < offsetArr.length; ++i) { t = store.getTuple(offsetArr[i], t); int hashCode = tupleHashCode(t); if (prevHashCode != hashCode) { hashCodeArr[hashCode] = i; prevHashCode = hashCode; } if (i == 0 || !compareKeys(prevTuple, t)) { offsetArr[i] = offsetArr[i] | SIGNBIT; } /* Object Reuse: Swap the tuples instead of creating new ones */ Tuple temp = t; t = prevTuple; prevTuple = temp; } end = System.currentTimeMillis(); print.f("LookUpTable: Created HashCode Array for %d entries in %d ms", offsetArr.length, (end - start)); }
From source file:com.ricemap.spateDB.core.RTree.java
License:Apache License
/** * Create Prisms that together pack all points in sample such that each * Prism contains roughly the same number of points. In other words it tries * to balance number of points in each Prism. Works similar to the logic of * bulkLoad but does only one level of Prisms. * /* w ww .ja va2s.c o m*/ * @param samples * @param gridInfo * - Used as a hint for number of Prisms per row or column * @return */ public static Prism[] packInPrisms(GridInfo gridInfo, final Point3d[] sample) { Prism[] Prisms = new Prism[gridInfo.layers * gridInfo.columns * gridInfo.rows]; int iPrism = 0; // Sort in t direction final IndexedSortable sortableT = new IndexedSortable() { @Override public void swap(int i, int j) { Point3d temp = sample[i]; sample[i] = sample[j]; sample[j] = temp; } @Override public int compare(int i, int j) { if (sample[i].t < sample[j].t) return -1; if (sample[i].t > sample[j].t) return 1; return 0; } }; // Sort in x direction final IndexedSortable sortableX = new IndexedSortable() { @Override public void swap(int i, int j) { Point3d temp = sample[i]; sample[i] = sample[j]; sample[j] = temp; } @Override public int compare(int i, int j) { if (sample[i].x < sample[j].x) return -1; if (sample[i].x > sample[j].x) return 1; return 0; } }; // Sort in y direction final IndexedSortable sortableY = new IndexedSortable() { @Override public void swap(int i, int j) { Point3d temp = sample[i]; sample[i] = sample[j]; sample[j] = temp; } @Override public int compare(int i, int j) { if (sample[i].y < sample[j].y) return -1; if (sample[i].y > sample[j].y) return 1; return 0; } }; final QuickSort quickSort = new QuickSort(); quickSort.sort(sortableT, 0, sample.length); //tony int tindex1 = 0; double t1 = gridInfo.t1; for (int lay = 0; lay < gridInfo.layers; lay++) { int tindex2 = sample.length * (lay + 1) / gridInfo.layers; double t2 = lay == gridInfo.layers - 1 ? gridInfo.t2 : sample[tindex2 - 1].t; quickSort.sort(sortableX, tindex1, tindex2); int xindex1 = tindex1; double x1 = gridInfo.x1; for (int col = 0; col < gridInfo.columns; col++) { int xindex2 = sample.length * (col + 1) / gridInfo.columns; // Determine extents for all Prisms in this column double x2 = col == gridInfo.columns - 1 ? gridInfo.x2 : sample[xindex2 - 1].x; // Sort all points in this column according to its y-coordinate quickSort.sort(sortableY, xindex1, xindex2); // Create Prisms in this column double y1 = gridInfo.y1; for (int row = 0; row < gridInfo.rows; row++) { int yindex2 = xindex1 + (xindex2 - xindex1) * (row + 1) / gridInfo.rows; double y2 = row == gridInfo.rows - 1 ? gridInfo.y2 : sample[yindex2 - 1].y; Prisms[iPrism++] = new Prism(t1, x1, y1, t2, x2, y2); y1 = y2; } xindex1 = xindex2; x1 = x2; } } return Prisms; }
From source file:edu.umn.cs.pigeon.SJPlaneSweep.java
License:Open Source License
@Override public DataBag exec(Tuple input) throws IOException { DataBag lRelation = (DataBag) input.get(0); DataBag rRelation = (DataBag) input.get(1); boolean dupAvoidance = false; double mbrX1 = 0, mbrY1 = 0, mbrX2 = 0, mbrY2 = 0; if (input.size() > 2) { // Implement duplicate avoidance based on the MBR as specified by // the third argument Geometry cellMBR = geomParser.parseGeom(input.get(2)); if (cellMBR != null) { dupAvoidance = true;//from w w w . j a va2 s. c om Coordinate[] mbrCoords = cellMBR.getCoordinates(); mbrX1 = Math.min(mbrCoords[0].x, mbrCoords[2].x); mbrY1 = Math.min(mbrCoords[0].y, mbrCoords[2].y); mbrX2 = Math.max(mbrCoords[0].x, mbrCoords[2].x); mbrY2 = Math.max(mbrCoords[0].y, mbrCoords[2].y); } } Iterator<Tuple> irGeoms = null; if (input.size() > 4 && input.get(4) instanceof DataBag) { // User specified a column of values for right geometries DataBag rGeoms = (DataBag) input.get(4); if (rGeoms.size() != rRelation.size()) throw new ExecException( String.format("Mismatched sizes of right records column (%d) and geometry column (%d)", rRelation.size(), rGeoms.size())); irGeoms = rGeoms.iterator(); } // TODO ensure that the left bag is the smaller one for efficiency if (lRelation.size() > Integer.MAX_VALUE) throw new ExecException("Size of left dataset is too large " + lRelation.size()); // Read all of the left dataset in memory final Tuple[] lTuples = new Tuple[(int) lRelation.size()]; int leftSize = 0; tupleFactory = TupleFactory.getInstance(); for (Tuple t : lRelation) { lTuples[leftSize++] = tupleFactory.newTupleNoCopy(t.getAll()); } // Extract MBRs of objects for filter-refine approach final double[] lx1 = new double[(int) lRelation.size()]; final double[] ly1 = new double[(int) lRelation.size()]; final double[] lx2 = new double[(int) lRelation.size()]; final double[] ly2 = new double[(int) lRelation.size()]; if (input.size() > 3 && input.get(3) instanceof DataBag) { // User specified a column of values that contain the geometries DataBag lGeoms = (DataBag) input.get(3); if (lRelation.size() != lGeoms.size()) throw new ExecException( String.format("Mismatched sizes of left records column (%d) and geometry column (%d)", lRelation.size(), lGeoms.size())); Iterator<Tuple> ilGeoms = lGeoms.iterator(); for (int i = 0; i < lTuples.length; i++) { Geometry geom = geomParser.parseGeom(ilGeoms.next().get(0)); Coordinate[] mbrCoords = geom.getEnvelope().getCoordinates(); lx1[i] = Math.min(mbrCoords[0].x, mbrCoords[2].x); ly1[i] = Math.min(mbrCoords[0].y, mbrCoords[2].y); lx2[i] = Math.max(mbrCoords[0].x, mbrCoords[2].x); ly2[i] = Math.max(mbrCoords[0].y, mbrCoords[2].y); } } else { int lGeomColumn; if (input.size() > 3 && input.get(3) instanceof Integer) lGeomColumn = (Integer) input.get(3); else if (input.size() > 3 && input.get(3) instanceof Long) lGeomColumn = (int) ((long) (Long) input.get(3)); else lGeomColumn = detectGeomColumn(lTuples[0]); for (int i = 0; i < lTuples.length; i++) { Geometry geom = geomParser.parseGeom(lTuples[i].get(lGeomColumn)); Coordinate[] mbrCoords = geom.getEnvelope().getCoordinates(); lx1[i] = Math.min(mbrCoords[0].x, mbrCoords[2].x); ly1[i] = Math.min(mbrCoords[0].y, mbrCoords[2].y); lx2[i] = Math.max(mbrCoords[0].x, mbrCoords[2].x); ly2[i] = Math.max(mbrCoords[0].y, mbrCoords[2].y); } } // Sort left MBRs by x to prepare for the plane-sweep algorithm IndexedSortable lSortable = new IndexedSortable() { @Override public void swap(int i, int j) { Tuple tt = lTuples[i]; lTuples[i] = lTuples[j]; lTuples[j] = tt; double td = lx1[i]; lx1[i] = lx1[j]; lx1[j] = td; td = ly1[i]; ly1[i] = ly1[j]; ly1[j] = td; td = lx2[i]; lx2[i] = lx2[j]; lx2[j] = td; td = ly2[i]; ly2[i] = ly2[j]; ly2[j] = td; } @Override public int compare(int i, int j) { if (lx1[i] < lx1[j]) return -1; if (lx2[i] > lx2[j]) return 1; return 0; } }; QuickSort quickSort = new QuickSort(); quickSort.sort(lSortable, 0, lTuples.length); // Retrieve objects from the right relation in batches and join with left Iterator<Tuple> ri = rRelation.iterator(); final int batchSize = 10000; final Tuple[] rTuples = new Tuple[batchSize]; final double[] rx1 = new double[batchSize]; final double[] ry1 = new double[batchSize]; final double[] rx2 = new double[batchSize]; final double[] ry2 = new double[batchSize]; IndexedSortable rSortable = new IndexedSortable() { @Override public void swap(int i, int j) { Tuple tt = rTuples[i]; rTuples[i] = rTuples[j]; rTuples[j] = tt; double td = rx1[i]; rx1[i] = rx1[j]; rx1[j] = td; td = ry1[i]; ry1[i] = ry1[j]; ry1[j] = td; td = rx2[i]; rx2[i] = rx2[j]; rx2[j] = td; td = ry2[i]; ry2[i] = ry2[j]; ry2[j] = td; } @Override public int compare(int i, int j) { if (rx1[i] < rx1[j]) return -1; if (rx2[i] > rx2[j]) return 1; return 0; } }; int rSize = 0; DataBag output = BagFactory.getInstance().newDefaultBag(); int rGeomColumn = -1; while (ri.hasNext()) { rTuples[rSize++] = tupleFactory.newTupleNoCopy(ri.next().getAll()); if (rSize == batchSize || !ri.hasNext()) { // Extract MBRs of geometries on the right if (irGeoms != null) { for (int i = 0; i < rSize; i++) { Geometry geom = geomParser.parseGeom(irGeoms.next().get(0)); Coordinate[] mbrCoords = geom.getEnvelope().getCoordinates(); rx1[i] = Math.min(mbrCoords[0].x, mbrCoords[2].x); ry1[i] = Math.min(mbrCoords[0].y, mbrCoords[2].y); rx2[i] = Math.max(mbrCoords[0].x, mbrCoords[2].x); ry2[i] = Math.max(mbrCoords[0].y, mbrCoords[2].y); } } else { if (rGeomColumn == -1) rGeomColumn = detectGeomColumn(rTuples[0]); for (int i = 0; i < rSize; i++) { Geometry geom = geomParser.parseGeom(rTuples[i].get(rGeomColumn)); Coordinate[] mbrCoords = geom.getEnvelope().getCoordinates(); rx1[i] = Math.min(mbrCoords[0].x, mbrCoords[2].x); ry1[i] = Math.min(mbrCoords[0].y, mbrCoords[2].y); rx2[i] = Math.max(mbrCoords[0].x, mbrCoords[2].x); ry2[i] = Math.max(mbrCoords[0].y, mbrCoords[2].y); } } // Perform the join now quickSort.sort(rSortable, 0, rSize); int i = 0, j = 0; while (i < lTuples.length && j < rSize) { if (lx1[i] < rx1[j]) { int jj = j; // Compare left object i to all right object jj while (jj < rSize && rx1[jj] <= lx2[i]) { if (lx2[i] > rx1[jj] && rx2[jj] > lx1[i] && ly2[i] > ry1[jj] && ry2[jj] > ly1[i]) { boolean report = true; if (dupAvoidance) { // Performs the reference point technique to avoid duplicates double intersectX = Math.max(lx1[i], rx1[jj]); if (intersectX >= mbrX1 && intersectX < mbrX2) { double intersectY = Math.max(ly1[i], ry1[jj]); report = intersectY >= mbrY1 && intersectY < mbrY2; } else { report = false; } } if (report) { addToAnswer(output, lTuples[i], rTuples[jj]); } } jj++; progress(); } i++; } else { int ii = i; // Compare all left objects ii to the right object j while (ii < lTuples.length && lx1[ii] <= rx2[j]) { if (lx2[ii] > rx1[j] && rx2[j] > lx1[ii] && ly2[ii] > ry1[j] && ry2[j] > ly1[ii]) { boolean report = true; if (dupAvoidance) { // Performs the reference point technique to avoid duplicates double intersectX = Math.max(lx1[ii], rx1[j]); if (intersectX >= mbrX1 && intersectX < mbrX2) { double intersectY = Math.max(ly1[ii], ry1[j]); report = intersectY >= mbrY1 && intersectY < mbrY2; } else { report = false; } } if (report) { addToAnswer(output, lTuples[ii], rTuples[j]); } } ii++; progress(); } j++; } progress(); } } } return output; }
From source file:edu.umn.cs.spatialHadoop.core.RTree.java
License:Open Source License
/** * Create rectangles that together pack all points in sample such that * each rectangle contains roughly the same number of points. In other words * it tries to balance number of points in each rectangle. * Works similar to the logic of bulkLoad but does only one level of * rectangles./*from ww w. j a v a 2 s .c om*/ * @param samples * @param gridInfo - Used as a hint for number of rectangles per row or column * @return */ public static Rectangle[] packInRectangles(GridInfo gridInfo, final Point[] sample) { Rectangle[] rectangles = new Rectangle[gridInfo.columns * gridInfo.rows]; int iRectangle = 0; // Sort in x direction final IndexedSortable sortableX = new IndexedSortable() { @Override public void swap(int i, int j) { Point temp = sample[i]; sample[i] = sample[j]; sample[j] = temp; } @Override public int compare(int i, int j) { if (sample[i].x < sample[j].x) return -1; if (sample[i].x > sample[j].x) return 1; return 0; } }; // Sort in y direction final IndexedSortable sortableY = new IndexedSortable() { @Override public void swap(int i, int j) { Point temp = sample[i]; sample[i] = sample[j]; sample[j] = temp; } @Override public int compare(int i, int j) { if (sample[i].y < sample[j].y) return -1; if (sample[i].y > sample[j].y) return 1; return 0; } }; final QuickSort quickSort = new QuickSort(); quickSort.sort(sortableX, 0, sample.length); int xindex1 = 0; double x1 = gridInfo.x1; for (int col = 0; col < gridInfo.columns; col++) { int xindex2 = sample.length * (col + 1) / gridInfo.columns; // Determine extents for all rectangles in this column double x2 = col == gridInfo.columns - 1 ? gridInfo.x2 : sample[xindex2 - 1].x; // Sort all points in this column according to its y-coordinate quickSort.sort(sortableY, xindex1, xindex2); // Create rectangles in this column double y1 = gridInfo.y1; for (int row = 0; row < gridInfo.rows; row++) { int yindex2 = xindex1 + (xindex2 - xindex1) * (row + 1) / gridInfo.rows; double y2 = row == gridInfo.rows - 1 ? gridInfo.y2 : sample[yindex2 - 1].y; rectangles[iRectangle++] = new Rectangle(x1, y1, x2, y2); y1 = y2; } xindex1 = xindex2; x1 = x2; } return rectangles; }
From source file:edu.umn.cs.spatialHadoop.delaunay.Triangulation.java
License:Open Source License
/** * Sort edges lexicographically by edgeStart and edgeEnd to be able to use * binary search when finding all neighbors of a specific node. *//*from ww w.ja v a 2 s . c o m*/ public void sortEdges() { QuickSort sorter = new QuickSort(); IndexedSortable sortable = new IndexedSortable() { @Override public int compare(int i, int j) { if (edgeStarts[i] != edgeStarts[j]) return edgeStarts[i] - edgeStarts[j]; return edgeEnds[i] - edgeEnds[j]; } @Override public void swap(int i, int j) { int t = edgeStarts[i]; edgeStarts[i] = edgeStarts[j]; edgeStarts[j] = t; t = edgeEnds[i]; edgeEnds[i] = edgeEnds[j]; edgeEnds[j] = t; } }; if (edgeStarts.length > 0) sorter.sort(sortable, 0, edgeStarts.length); }
From source file:org.apache.drill.exec.physical.impl.orderedpartitioner.SampleSortTemplate.java
License:Apache License
@Override public void sort(SelectionVector2 vector2, VectorContainer container) { QuickSort qs = new QuickSort(); qs.sort(this, 0, vector2.getCount()); }
From source file:org.apache.drill.exec.physical.impl.sort.SortTemplate.java
License:Apache License
@Override public void sort(SelectionVector4 vector4, VectorContainer container) { Stopwatch watch = Stopwatch.createStarted(); QuickSort qs = new QuickSort(); qs.sort(this, 0, vector4.getTotalCount()); logger.debug("Took {} us to sort {} records", watch.elapsed(TimeUnit.MICROSECONDS), vector4.getTotalCount());/*from w w w . ja v a 2 s . c om*/ }
From source file:org.apache.drill.exec.physical.impl.xsort.SingleBatchSorterTemplate.java
License:Apache License
@Override public void sort(SelectionVector2 vector2) { QuickSort qs = new QuickSort(); Stopwatch watch = Stopwatch.createStarted(); if (vector2.getCount() > 0) { qs.sort(this, 0, vector2.getCount()); }/*from www. j av a 2s . c o m*/ logger.debug("Took {} us to sort {} records", watch.elapsed(TimeUnit.MICROSECONDS), vector2.getCount()); }
From source file:org.apache.drill.exec.SortTest.java
License:Apache License
public long doSort() { QuickSort qs = new QuickSort(); ByteSortable b = new ByteSortable(); long nano = System.nanoTime(); qs.sort(b, 0, RECORD_COUNT); return System.nanoTime() - nano; }
From source file:org.apache.flume.channel.recoverable.memory.wal.SequenceIDBuffer.java
License:Apache License
public void sort() { QuickSort quickSort = new QuickSort(); quickSort.sort(this, 0, size()); }