private void visualize(RNode n, java.io.PrintWriter pw, int x0, int y0, int w, int h) { pw.printf( "<div style=\"position:absolute; left: %d; top: %d; width: %d; height: %d; border: 1px dashed\">\n", x0, y0, w, h); pw.println("<pre>"); pw.println("Node: " + n.toString() + " \n(root==" + (n == root) + ") \n"); pw.println("Coords start: " + Arrays.toString(n.getMbr().getP_start()) + "\n"); pw.println("coords end: " + Arrays.toString(n.getMbr().getP_end()) + "\n"); pw.println("# Children: " + ((n.children() == null) ? 0 : n.children().size()) + "\n"); if (n instanceof REntry) { REntry n1 = (REntry) n; pw.println("Content: " + n1.getContent()); } pw.println("isLeaf: " + n.isLeaf() + "\n"); pw.println("</pre>"); int numChildren = (n.children() == null) ? 0 : n.children().size(); for (int i = 0; i < numChildren; i++) { visualize( n.children().get(i), pw, (int) (x0 + (i * w / (float) numChildren)), y0 + elemHeight, (int) (w / (float) numChildren), h - elemHeight); } pw.println("</div>"); }
private RNode getMinimalAreaNode(List<RNode> nodes) { RNode minimal = null; double minArea = Double.MAX_VALUE; for (RNode e : nodes) { double eArea = e.getMbr().getArea(); if (eArea < minArea) { minArea = eArea; minimal = e; } } return minimal; }
private double getDistance(RNode e1, RNode e2) { double[] a1 = e1.getMbr().getP_start(); double[] a2 = e1.getMbr().getP_end(); double[] b1 = e2.getMbr().getP_start(); double[] b2 = e2.getMbr().getP_end(); double dist = 0; for (int i = 0; i < dimension; i++) { /*suma los cuadrados de la resta de los puntos medios de los 2 mbr's para cada coordenada*/ dist += Math.pow(Math.abs(a1[i] - a2[i]) / 2.0 - Math.abs(b1[i] - b2[i]) / 2.0, 2); } dist = Math.sqrt(dist); return dist; }
private void refresh(RNode part1, RNode part2) throws DimensionalException { if (part1 == root) { if (part2 != null) { // build new root and add children. root = makeRoot(false); root.addChild(part1); part1.setParent(root); root.addChild(part2); part2.setParent(root); } update(root); return; } update(part1); if (part2 != null) { update(part2); if (part1.getParent().getChildren().size() > M_order) { RNode[] splits = makePartition(part1.getParent()); refresh(splits[0], splits[1]); } } if (part1.getParent() != null) { refresh(part1.getParent(), null); } }
public void insert(REntry e) throws DimensionalException { if (e.getDimension() != dimension) throw new DimensionalException( "Can only insert n-dimensional objects in n-dimensional RTree"); RNode theLeaf = findFittingLeaf(root, e); theLeaf.children().add(e); e.setParent(theLeaf); size++; // numOfNodes++; if (theLeaf.children().size() > M_order) { RNode[] parts = makePartition(theLeaf); refresh(parts[0], parts[1]); } else { refresh(theLeaf, null); } }
private int countLeafs(RNode n) { if (n.isLeaf()) return 1; int res = 0; for (RNode c : n.children) { res += countLeafs(c); } return res; }
private double getEnlargement(RNode host, RNode guest) throws DimensionalException { double[] p1Host = host.getMbr().getP_start(); double[] p2Host = host.getMbr().getP_end(); double[] p1Guest = guest.getMbr().getP_start(); double[] p2Guest = guest.getMbr().getP_end(); double[] minRes = new double[dimension]; double[] maxRes = new double[dimension]; for (int i = 0; i < dimension; i++) { double minH = Math.min(p1Host[i], p2Host[i]); double maxH = Math.max(p1Host[i], p2Host[i]); double minG = Math.min(p1Guest[i], p2Guest[i]); double maxG = Math.max(p1Guest[i], p2Guest[i]); if (minG < minH) minRes[i] = minG; else minRes[i] = minH; if (maxG > maxH) maxRes[i] = maxG; else maxRes[i] = maxH; } return new MBR(minRes, maxRes, dimension).getArea() - host.getMbr().getArea(); }
private RNode findFittingLeaf(RNode n, REntry e) throws DimensionalException { if (n.isLeaf()) return n; double min = Double.MAX_VALUE; List<RNode> minNodes = new ArrayList<RNode>(); for (RNode nc : n.children()) { double d = getEnlargement(nc, e); if (d < min) { min = d; minNodes.clear(); minNodes.add(nc); } else if (d == min) { minNodes.add(nc); } } RNode next; if (minNodes.size() > 1) next = getMinimalAreaNode(minNodes); else next = minNodes.get(0); return findFittingLeaf(next, e); }
public List<REntry> search(double[] point, double ratio) throws DimensionalException { List<REntry> resultados = new LinkedList<REntry>(); StringBuilder sb = new StringBuilder(); if (point.length != dimension) throw new DimensionalException("Can only search for n-dimensional points"); root.search(point, ratio, resultados, sb); visitedNodes = visitedNodes.add( new BigInteger( "" + sb.toString() .length())); // visitedNodes.add(root.visitedNodes.add(BigInteger.ONE)); return resultados; }
private RNode[] makePartition(RNode n) throws DimensionalException { numOfPartitions++; numOfNodes += 2; RNode[] parts = new RNode[] {n, new RNode(n.getMbr(), n.isLeaf(), M_order)}; parts[1].setParent(n.getParent()); if (parts[1].getParent() != null) { parts[1].getParent().children().add(parts[1]); } List<RNode> cpyc = new LinkedList<RNode>(n.children()); n.children().clear(); RNode[] seeds = getSeeds(cpyc); parts[0].children().add(seeds[0]); parts[1].children().add(seeds[1]); update(parts[0]); update(parts[1]); while (!cpyc.isEmpty()) { if (parts[0].missingEntries(m_order) == 0 && parts[1].missingEntries(m_order) + cpyc.size() == m_order) { parts[1].children().addAll(cpyc); cpyc.clear(); update(parts[0]); update(parts[1]); return parts; } else if (parts[1].missingEntries(m_order) == 0 && parts[0].missingEntries(m_order) + cpyc.size() == m_order) { parts[0].children().addAll(cpyc); cpyc.clear(); update(parts[0]); update(parts[1]); return parts; } RNode next = chooseNext(cpyc, seeds), chosen; double enl1 = getEnlargement(parts[0], next); double enl2 = getEnlargement(parts[1], next); if (enl1 < enl2) chosen = parts[0]; else if (enl2 < enl1) chosen = parts[1]; else { double ar1 = parts[0].getMbr().getArea(); double ar2 = parts[1].getMbr().getArea(); if (ar1 < ar2) chosen = parts[0]; else if (ar2 < ar1) chosen = parts[1]; else { if (parts[0].children().size() < parts[1].children().size()) chosen = parts[0]; else if (parts[0].children().size() > parts[1].children().size()) chosen = parts[1]; else chosen = parts[new Random().nextInt(2)]; } } chosen.children().add(next); update(chosen); } return parts; }
/** * Administrador de memoria para guardar un nodo, guarda en el bloque que corresponde el nodo que * recibe como parametro, si el bloque esta en memoria principal lo guarda directamente, de lo * contrario lo carga desde memoria secundaria y el bloque completo queda en memoria. * * @param nodo nodo que queremos guardar, sera guardado de acuerdo a sus paremetros getFilePos y * getPagePos * @throws IOException */ private void writeAdmin(RNode nodo) throws IOException { long fPos = nodo.getMyfPos(); int i; for (i = 0; i < numOfBuffers; i++) { if (fPos == thisBlock[i]) { writeNode(nodo, i); return; } } // Guardo el bloque con peor prioridad a memoria secundaria si fue modificado, // si no lo fue solo lo sobreescribo i = bufferPrior[numOfBuffers - 1]; if (bufModified[i]) writeBlock(i); readBlock(fPos, i); writeNode(nodo, i); }
/** * shrinks the mbr area to the just necessary * * @param n */ private void update(RNode n) throws DimensionalException { double[] minCoords = new double[dimension]; double[] maxCoords = new double[dimension]; for (int i = 0; i < dimension; i++) { minCoords[i] = Double.MAX_VALUE; maxCoords[i] = -Double.MAX_VALUE; for (RNode c : n.children()) { c.setParent(n); double minp = Math.min(c.getMbr().getP_start()[i], c.getMbr().getP_end()[i]); double maxp = Math.max(c.getMbr().getP_end()[i], c.getMbr().getP_start()[i]); if (minp < minCoords[i]) { minCoords[i] = minp; } if (maxp > maxCoords[i]) { maxCoords[i] = maxp; } } } n.setMbr(new MBR(minCoords, maxCoords, dimension)); }
private RNode[] quadSeeds(List<RNode> cc) throws DimensionalException { RNode e1; RNode e2; RNode s1 = null; RNode s2 = null; double maxDeadSpace = -Double.MAX_VALUE; for (int i = 0; i < dimension; i++) { for (int k = i + 1; k < dimension; k++) { e1 = cc.get(i); e2 = cc.get(k); double areae1 = e1.getMbr().getArea(); double areae2 = e2.getMbr().getArea(); double[] p1Host = e1.getMbr().getP_start(); double[] p2Host = e1.getMbr().getP_end(); double[] p1Guest = e2.getMbr().getP_start(); double[] p2Guest = e2.getMbr().getP_end(); double[] minRes = new double[dimension]; double[] maxRes = new double[dimension]; for (int j = 0; j < dimension; j++) { double minH = Math.min(p1Host[j], p2Host[j]); double maxH = Math.max(p1Host[j], p2Host[j]); double minG = Math.min(p1Guest[j], p2Guest[j]); double maxG = Math.max(p1Guest[j], p2Guest[j]); if (minG < minH) minRes[j] = minG; else minRes[j] = minH; if (maxG > maxH) maxRes[j] = maxG; else maxRes[j] = maxH; } MBR ambr = new MBR(minRes, maxRes, dimension); double totarea = ambr.getArea(); if (maxDeadSpace < totarea - areae1 - areae2) { maxDeadSpace = totarea - areae1 - areae2; s1 = e1; s2 = e2; } } } RNode[] seeds = new RNode[] {s1, s2}; cc.removeAll(Arrays.asList(seeds)); return seeds; }
/** * Guarda un Node en un buffer en memoria principal * * @param nodo Node que queremos guardar en el buffer * @param pPos posicion en el buffer donde guardaremos el nodo * @param i numero de buffer donde guardaremos el nodo (buffer[i]). * @throws IOException */ private void writeNode(RNode nodo, int i) throws IOException { nodo.writeToBuffer(buffer[i]); bufModified[i] = true; improvePriority(i); }
public int countNodes() { return root.countNodes(); }
public RAny outer(Frame frame, RAny xarg, RAny yarg, RAny farg) { // LICENSE: transcribed code from GNU R, which is licensed under GPL if (!(xarg instanceof RArray && yarg instanceof RArray)) { Utils.nyi("unsupported type"); return null; } RArray x = (RArray) xarg; RArray y = (RArray) yarg; int xsize = x.size(); int ysize = y.size(); RArray expy; RArray expx; if (EAGER) { x = x.materialize(); // FIXME: probably unnecessary (both x and y), could be done on-the-fly in the expansion methods y = y.materialize(); if (y instanceof DoubleImpl) { expy = expandYVector((DoubleImpl) y, ysize, xsize); } else if (y instanceof IntImpl) { expy = expandYVector((IntImpl) y, ysize, xsize); } else { expy = expandYVector(y, ysize, xsize); } if (xsize > 0) { if (x instanceof DoubleImpl) { expx = expandXVector((DoubleImpl) x, xsize, ysize); } else if (x instanceof IntImpl) { expx = expandXVector((IntImpl) x, xsize, ysize); } else { expx = expandXVector(x, xsize, ysize); } } else { expx = x; } } else { if (y instanceof RInt) { expy = lazyExpandYVector((RInt) y, ysize, xsize); } else { throw Utils.nyi(); } if (xsize > 0) { if (x instanceof RInt) { expx = lazyExpandXVector((RInt) x, xsize, ysize); } else { throw Utils.nyi(); } } else { expx = x; } } xArgProvider.setValue(expx); yArgProvider.setValue(expy); callableProvider.matchAndSet(frame, farg); RArray res = (RArray) callNode.execute(frame); int[] dimx = x.dimensions(); int[] dimy = y.dimensions(); int[] dim; if (dimx == null) { if (dimy == null) { dim = new int[]{xsize, ysize}; } else { dim = new int[1 + dimy.length]; dim[0] = xsize; System.arraycopy(dimy, 0, dim, 1, dimy.length); } } else { if (dimy == null) { dim = new int[dimx.length + 1]; System.arraycopy(dimx, 0, dim, 0, dimx.length); dim[dimx.length] = ysize; } else { dim = new int[dimx.length + dimy.length]; System.arraycopy(dimx, 0, dim, 0, dimx.length); System.arraycopy(dimy, 0, dim, dimx.length, dimy.length); } } return res.setDimensions(dim); // triggers materialization of the result }
@Override public RNode create(ASTNode call, RSymbol[] names, RNode[] exprs) { ArgumentInfo ia = check(call, names, exprs); boolean product = false; if (ia.provided("FUN")) { RNode fnode = exprs[ia.position("FUN")]; if (fnode instanceof Constant) { RAny value = ((Constant) fnode).execute(null); if (value instanceof RString) { RString str = (RString) value; if (str.size() == 1) { if (str.getString(0).equals("*")) { product = true; } } } } } else { product = true; } if (product) { return new MatrixOperation.OuterProduct(call, exprs[ia.position("X")], exprs[ia.position("Y")]); } int cnArgs = 2 + names.length - 3; // "-2" because both FUN, X, Y RSymbol[] cnNames = new RSymbol[cnArgs]; RNode[] cnExprs = new RNode[cnArgs]; cnNames[0] = null; ValueProvider xArgProvider = new ValueProvider(call); cnExprs[0] = xArgProvider; ValueProvider yArgProvider = new ValueProvider(call); cnExprs[1] = yArgProvider; ValueProvider[] constantArgProviders = new ValueProvider[cnArgs]; int j = 0; for (int i = 0; i < names.length; i++) { if (ia.position("X") == i || ia.position("Y") == i || ia.position("FUN") == i) { continue; } cnNames[2 + j] = names[i]; ValueProvider vp = new ValueProvider(call); cnExprs[2 + j] = vp; constantArgProviders[j] = vp; j++; } RNode funExpr = exprs[ia.position("FUN")]; final CallableProvider callableProvider = new CallableProvider(funExpr.getAST(), funExpr); final RNode callNode = FunctionCall.getFunctionCall(call, callableProvider, cnNames, cnExprs); final int posX = ia.position("X"); final int posY = ia.position("Y"); final int posFUN = ia.position("FUN"); return new OuterBuiltIn(call, names, exprs, callNode, callableProvider, xArgProvider, yArgProvider, constantArgProviders) { @Override public RAny doBuiltIn(Frame frame, RAny[] args) { RAny argx = null; RAny argy = null; RAny argfun = null; int k = 0; for (int i = 0; i < args.length; i++) { if (i == posX) { argx = args[i]; } else if (i == posY) { argy = args[i]; } else if (i == posFUN) { argfun = args[i]; } else { constantArgProviders[k].setValue(args[i]); k++; } } return outer(frame, argx, argy, argfun); } }; }
public void cleanVisitedNodes() { root.cleanVisitedNodes(); visitedNodes = BigInteger.ZERO; }