Exemple #1
0
func main() {
	tree := llrb.New(lessInt)
	tree.ReplaceOrInsert(1)
	tree.ReplaceOrInsert(2)
	tree.ReplaceOrInsert(3)
	tree.ReplaceOrInsert(4)
	tree.DeleteMin()
	tree.Delete(4)
	c := tree.IterAscend()
	for {
		u := <-c
		if u == nil {
			break
		}
		fmt.Printf("%d\n", int(u.(int)))
	}
}
Exemple #2
0
func order(input []RectObject) []int {
	defer func() {
		if err := recover(); err != nil {
			base.Error().Printf("Failure in sorting: %v", err)
		}
	}()
	var minx, miny int
	for _, r := range input {
		x, y := r.Pos()
		if x < minx {
			minx = x
		}
		if y < miny {
			miny = y
		}
	}

	ra := make([]RectObject, len(input))
	for i, r := range input {
		x, y := r.Pos()
		dx, dy := r.Dims()
		ra[i] = arog{x - minx + 1, y - miny + 1, dx, dy}
	}

	mapping := make(map[RectObject]int, len(ra))
	for i := range ra {
		mapping[ra[i]] = i
	}
	var e endpointArray
	for i := range ra {
		e = append(e, endpoint{RectObject: ra[i], first: false})
		e = append(e, endpoint{RectObject: ra[i], first: true})
	}
	sort.Sort(e)
	var sweep_pos int
	less_func := func(_a, _b interface{}) bool {
		a := _a.(RectObject)
		b := _b.(RectObject)
		ax, ay, ax2, ay2 := firstAndLastPoints(a)
		da := ax*ax + ay*ay
		da2 := ax2*ax2 + ay2*ay2
		w_a := width(a.Dims())
		bx, by, bx2, by2 := firstAndLastPoints(b)
		db := bx*bx + by*by
		db2 := bx2*bx2 + by2*by2
		w_b := width(b.Dims())
		va := w_b * (w_a*da + (da2-da)*(sweep_pos-pos(ax, ay)))
		vb := w_a * (w_b*db + (db2-db)*(sweep_pos-pos(bx, by)))
		return va < vb
	}
	l := llrb.New(less_func)

	dag := make(adag, len(ra))

	for _, p := range e {
		if p.first {
			sweep_pos = pos(firstPoint(p.RectObject))
			l.ReplaceOrInsert(p.RectObject)
			lower := l.LowerBound(p.RectObject)
			upper := l.UpperBound(p.RectObject)
			if lower != nil {
				index := mapping[lower.(RectObject)]
				dag[index] = append(dag[index], mapping[p.RectObject])
			}
			if upper != nil {
				index := mapping[p.RectObject]
				dag[index] = append(dag[index], mapping[upper.(RectObject)])
			}
		} else {
			l.Delete(p.RectObject)
		}
	}
	return algorithm.TopoSort(dag)
}