From: adam Date: Thu, 8 Apr 2004 02:34:34 +0000 (+0000) Subject: Massive improvement in Simplex layout solver X-Git-Tag: RC4~27 X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=commitdiff_plain;h=b1568b13f53ca805911fcd7737daf79492d6be1f Massive improvement in Simplex layout solver darcs-hash:20040408023434-5007d-4db3e0e4c7d1602bd9509357ddd40fe7b7f4636d.gz --- diff --git a/src/org/ibex/Box.java b/src/org/ibex/Box.java index c2f16dc..e3e30ec 100644 --- a/src/org/ibex/Box.java +++ b/src/org/ibex/Box.java @@ -1,5 +1,5 @@ // FIXME -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] package org.ibex; // FEATURE: reflow before allowing js to read from width/height @@ -56,8 +56,6 @@ public final class Box extends JSScope implements Scheduler.Task { //#define CHECKSET_BOOLEAN(prop) boolean nu = toBoolean(value); if (nu == prop) break; prop = nu; //#define CHECKSET_STRING(prop) if ((value==null&&prop==null)||(value!=null&&value.equals(prop))) break; prop=(String)value; - void mark_for_repack() { MARK_REPACK; } - protected Box() { super(null); } static Hash boxToCursor = new Hash(500, 3); @@ -187,9 +185,6 @@ public final class Box extends JSScope implements Scheduler.Task { dirty(); } - public Box getRoot() { return parent == null ? this : parent.getRoot(); } - public Surface getSurface() { return Surface.fromBox(getRoot()); } - // FEATURE: use cx2/cy2 format /** Adds the intersection of (x,y,w,h) and the node's current actual geometry to the Surface's dirty list */ public void dirty() { dirty(0, 0, width, height); } @@ -212,84 +207,46 @@ public final class Box extends JSScope implements Scheduler.Task { // Reflow //////////////////////////////////////////////////////////////////////////////////////// - Box nextPackedSibling() { Box b = nextSibling(); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); } - Box firstPackedChild() { Box b = getChild(0); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); } - - private static Box[] frontier = new Box[65535]; - private static int[] frontier_content = new int[65535]; + // worst case runtime = O(numboxes * numboxes_in_widest_row) + + private static Box[] frontier = new Box[65535]; // FIXME: GC hazard /** pack the boxes into rows and columns; also computes contentwidth */ - void repack() { - for(Box child = getChild(0); child != null; child = child.nextSibling()) child.repack(); - contentwidth = 0; - contentheight = 0; - if (treeSize() == 0) { constrain(); return; } - //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan contentheight/contentwidth contentwidth/contentheight - if (test(FIXED) == COLS) { - int childnum = 0; - Box lastpacked = null; - int maxfront = 0; - int maxrow = 0; - int rowwidth = 0; - for(Box child = getChild(0); child != null; child = child.nextSibling(), childnum++) { - if (!(child.test(PACKED) && child.test(VISIBLE))) continue; - int col = lastpacked == null ? 0 : (lastpacked.col + lastpacked.colspan); - int row = lastpacked == null ? 0 : lastpacked.row; - int colspan = min(cols, child.colspan); - for(int i=0; i cols) { - row++; - for(; i>0; i--) row = max(row, frontier[i].row + frontier[i].rowspan); - col = 0; - rowwidth = 0; - continue; - } - Box front = frontier[i]; // FIXME: O(nlgn) - if (front.row + front.rowspan <= row) { - frontier[i] = frontier[maxfront-1]; - frontier_content[i] = frontier_content[maxfront-1]; - maxfront--; - frontier[maxfront] = null; - frontier_content[maxfront] = 0; - i--; - continue; - } - if ((front.col <= col && front.col + front.colspan > col) || - (front.col < (col+colspan) && front.col + front.colspan >= (col+colspan))) { - col = front.col + front.colspan; - rowwidth += front.contentwidth; // FIXME: suspect + void pack() { + for(Box child = getChild(0); child != null; child = child.nextSibling()) child.pack(); + int frontier_size = 0; contentwidth = 0; contentheight = 0; + //#repeat COLS/ROWS rows/cols cols/rows col/row row/col colspan/rowspan rowspan/colspan \ + // contentheight/contentwidth contentwidth/contentheight + if (treeSize() > 0 && test(FIXED) == COLS) { + rows = 0; + for(Box child = getChild(0); child != null; child = child.nextSibling()) { + if (!child.test(PACKED) || !child.test(VISIBLE)) continue; + child.col = (short)(frontier_size <= 0 ? 0 : (frontier[frontier_size-1].col + frontier[frontier_size-1].colspan)); + child.row = (short)(frontier_size <= 0 ? 0 : frontier[frontier_size-1].row); + if (child.col + min(cols,child.colspan) > cols) { child.col = 0; child.row++; } + for(int i=0; ichild.col) { + child.col = (short)(frontier[i].col + frontier[i].colspan); + if (child.col + min(cols,child.colspan) > cols) { + child.row = (short)(frontier[i].row + frontier[i].rowspan); + for(i--; i>0; i--) child.row = (short)min(row, frontier[i].row + frontier[i].rowspan); + child.col = (short)0; + } i = -1; - continue; - } - break; - } - child.col = (short)col; - child.row = (short)row; - maxrow = max(maxrow, child.row + child.rowspan); - contentwidth = max(contentwidth, rowwidth); - rowwidth = 0; - lastpacked = child; - for(int i=0; i 0 && cols > 1) do { - // FIXME: numboxes^2, and damn ugly to boot - for(Box c = firstPackedChild(); c != null; c = c.nextPackedSibling()) { - int target = c.col; - for(boolean stop = false;;) { - for(int i=0; i<=numregions; i++) { - if (i == numregions) { regions[numregions++] = target; break; } - if (target == regions[i]) break; - if (target < regions[i]) { int tmp = target; target = regions[i]; regions[i] = tmp; } - } - if (stop) break; - stop = true; - target = min(cols, c.col+c.colspan); - } - } - if (regions[numregions-1] == cols) numregions--; - else regions[numregions] = cols; - - /* boolean easy_width = contentwidth >= width; */ - /* - for(Box c = firstPackedChild(); easy_width && c != null; c = c.nextPackedSibling()) { - if (c.contentwidth == c.maxwidth) continue; - if (c.maxwidth == Integer.MAX_VALUE) continue; - easy_width = false; - } - if (easy_width) for(int i=0; i i && c.maxwidth < Integer.MAX_VALUE) - good = false; - if (good) { easy_width = true; break; } - } - if (easy_width) break; - */ - int nc = numregions * 2 + numkids + 1; + computeRegions(); + int nc = numregions * 3 + numkids * 2 + 3; if (coeff == null || nc+1>coeff.length) coeff = new float[nc+1]; lp_h.init(nc); for(int i=0; i= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) coeff[r] = (float)(regions[r+1] - regions[r]); - lp_h.add_constraint(coeff, LinearProgramming.GE, (float)child.contentwidth); - - // priority 2: honor maxwidths - int child_maxwidth = child.test(HSHRINK) ? min(child.maxwidth, child.contentwidth) : child.maxwidth; - if (child_maxwidth < Integer.MAX_VALUE) { - for(int i=0; i= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) - coeff[r] = (float)(regions[r+1] - regions[r]); - coeff[numregions*2+childnum] = (float)-1.0; - lp_h.add_constraint(coeff, LinearProgramming.LE, (float)child_maxwidth); + lp_h.add_constraint(coeff, Simplex.GE, (float)child.contentwidth); + if (!findMinimum) { + // priority 2: honor maxwidths + int child_maxwidth = child.test(HSHRINK) ? min(child.maxwidth, child.contentwidth) : child.maxwidth; + if (child_maxwidth < Integer.MAX_VALUE) { + for(int i=0; i= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) + coeff[r] = (float)(regions[r+1] - regions[r]); + coeff[numregions*2+childnum] = (float)-1.0; + lp_h.add_constraint(coeff, Simplex.LE, (float)child_maxwidth); + } } - childnum++; } - // priority 3: equalize columns - float avg = ((float)width)/((float)numregions); - for(int r=0; r= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) { + for(int r=0; r= child.col && regions[r+1] <= min(child.col+child.colspan,cols)) child_width += Math.round(lp_h.solution[lp_h.rows+r+1] * (regions[r+1] - regions[r])); - } else if (regions[r+1] <= child.col) { + else if (regions[r+1] <= child.col) child_x += Math.round(lp_h.solution[lp_h.rows+r+1] * (regions[r+1] - regions[r])); - } - } } diff = (child_width - (child.test(HSHRINK) ? child.contentwidth : min(child_width, child.maxwidth))); child_x += (child.test(ALIGN_RIGHT) ? diff : child.test(ALIGN_LEFT) ? 0 : diff / 2); @@ -501,7 +434,7 @@ public final class Box extends JSScope implements Scheduler.Task { for(Box child = getChild(0); child != null; child = child.nextSibling()) if (child.test(VISIBLE) && child.treeSize() > 0) - child.place_children(); + child.place(); } @@ -547,11 +480,7 @@ public final class Box extends JSScope implements Scheduler.Task { // Methods to implement org.ibex.js.JS ////////////////////////////////////// - public int globalToLocalX(int x) { return parent == null ? x : parent.globalToLocalX(x - this.x); } - public int globalToLocalY(int y) { return parent == null ? y : parent.globalToLocalY(y - this.y); } - public int localToGlobalX(int x) { return parent == null ? x : parent.globalToLocalX(x + this.x); } - public int localToGlobalY(int y) { return parent == null ? y : parent.globalToLocalY(y + this.y); } - + public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn { switch (nargs) { case 1: { @@ -577,8 +506,6 @@ public final class Box extends JSScope implements Scheduler.Task { return super.callMethod(method, a0, a1, a2, rest, nargs); } - public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); } - protected boolean isTrappable(Object key, boolean isRead) { if (key == null) return false; else if (key instanceof String) { @@ -657,11 +584,11 @@ public final class Box extends JSScope implements Scheduler.Task { } } - void setMaxWidth(Object value) { + public void setMaxWidth(Object value) { do { CHECKSET_INT(maxwidth); MARK_RESIZE; } while(false); - if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth; + if (parent == null && getSurface() != null) getSurface().pendingWidth = maxwidth; } - void setMaxHeight(Object value) { + public void setMaxHeight(Object value) { do { CHECKSET_INT(maxheight); MARK_RESIZE; } while(false); if (parent == null && getSurface() != null) getSurface().pendingHeight = maxheight; } @@ -965,6 +892,17 @@ public final class Box extends JSScope implements Scheduler.Task { // Trivial Helper Methods (should be inlined) ///////////////////////////////////////// + void mark_for_repack() { MARK_REPACK; } + public Enumeration keys() { throw new Error("you cannot apply for..in to a " + this.getClass().getName()); } + public Box getRoot() { return parent == null ? this : parent.getRoot(); } + public Surface getSurface() { return Surface.fromBox(getRoot()); } + Box nextPackedSibling() { Box b = nextSibling(); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); } + Box firstPackedChild() { Box b = getChild(0); return b == null || (b.test(PACKED | VISIBLE)) ? b : b.nextPackedSibling(); } + public int globalToLocalX(int x) { return parent == null ? x : parent.globalToLocalX(x - this.x); } + public int globalToLocalY(int y) { return parent == null ? y : parent.globalToLocalY(y - this.y); } + public int localToGlobalX(int x) { return parent == null ? x : parent.globalToLocalX(x + this.x); } + public int localToGlobalY(int y) { return parent == null ? y : parent.globalToLocalY(y + this.y); } + static short min(short a, short b) { if (a