コード例 #1
0
ファイル: edges.go プロジェクト: ebering/zellij
func chooseNextEdgeByGeneration(T *quadratic.Map) *quadratic.Edge {
	ActiveFaces := new(vector.Vector)
	onGeneration := -1
	T.Faces.Do(func(F interface{}) {
		Fac := F.(*quadratic.Face)
		if Fac.Value.(string) != "active" {
			return
		}
		ActiveFaces.Push(Fac)
		Fac.DoEdges(func(e *quadratic.Edge) {
			//fmt.Fprintf(os.Stderr,"edge generation: %v onGen: %v\n",e.Generation,onGeneration)
			if onGeneration < 0 || e.Generation < onGeneration {
				onGeneration = e.Generation
			}
		})
	})
	activeEdges := new(generationOrderedEdges)

	ActiveFaces.Do(func(F interface{}) {
		Fac := F.(*quadratic.Face)
		Fac.DoEdges(func(e *quadratic.Edge) {
			if e.Generation != onGeneration {
				return
			}
			activeEdges.Push(e)
		})
	})
	//fmt.Fprintf(os.Stderr,"onGen: %v have %v active edges\n",onGeneration,activeEdges.Len())
	sort.Sort(activeEdges)
	return activeEdges.At(0).(*quadratic.Edge)
}
コード例 #2
0
ファイル: state.go プロジェクト: saysjonathan/europa
func (state *State) EvaluateTree(tree vector.Vector) {
	tree.Do(func(elem interface{}) {
		msg := elem.(IMessage)
		println(msg.GetName())
		msg.GetArguments().Do(func(item interface{}) {
			arg := item.(IMessage)
			println(arg.GetName())
		})
	})
}
コード例 #3
0
ファイル: tree_test.go プロジェクト: serega/go-rbtree
/**
   From Introduction to Algorithms, 2/e
   Red-Black Tree has the following properties
  1. Every Node is either red or black
  2. The root is black
  3. Every leaf (NIL) is black
  4. If a node is red, then both its children are black
  5. For each node, all paths from the node to descendantat leaves
     contain the same number of black nodes
*/
func VerifyRBTreeProperties(tree *RBTree, t *testing.T) {
	if tree.root == NIL {
		return
	}
	if tree.root.color != BLACK {
		t.Errorf("(1) root color is not BLACK")
	}
	leafs := new(vector.Vector)
	inorderTreeWalk(tree.root, func(n *Node) {
		if n.Leaf() {
			leafs.Push(n)
		}
	})
	var numBlacks int = -1
	leafs.Do(func(i interface{}) {
		n := i.(*Node)
		if n.Left != NIL { // NIL is always black
			t.Errorf("left of leaf is not NIL")
		}
		if n.Right != NIL { // NIL is always black
			t.Errorf("right of leaf is not NIL")
		}

		var blacks int = 0
		for n != tree.root {
			if n.color == BLACK {
				blacks += 1
			} else {
				if n.Parent.color == RED { // property 4 failed
					t.Errorf("(4) two consecutive RED nodes %d %d", n.Value.(int), n.Parent.Value.(int))
				}
			}
			n = n.Parent
		}
		if numBlacks == -1 {
			numBlacks = blacks
		} else {
			if numBlacks != blacks {
				t.Errorf("(5) number of blacks differ. %d vs. %d", numBlacks, blacks)
			}
		}
	})
}
コード例 #4
0
ファイル: overlay.go プロジェクト: ebering/zellij
func (m *Map) Overlay(n *Map, mergeFaces func(interface{}, interface{}) (interface{}, os.Error)) (*Map, os.Error) {
	o := NewMap()
	CopiedEdges := new(vector.Vector)
	m.Edges.Do(func(l interface{}) {
		e := l.(*Edge)
		if e.start.Point.Less(e.end.Point) {
			f, g := NewEdgePair(NewVertex(e.start.Copy()), NewVertex(e.end.Copy()))
			f.face = e.face
			g.face = e.twin.face
			f.Generation = e.Generation
			g.Generation = e.twin.Generation
			f.fromMap,g.fromMap = m,m
			CopiedEdges.Push(f)
			CopiedEdges.Push(g)
		}
	})
	n.Edges.Do(func(l interface{}) {
		e := l.(*Edge)
		if e.start.Point.Less(e.end.Point) {
			f, g := NewEdgePair(NewVertex(e.start.Copy()), NewVertex(e.end.Copy()))
			f.face = e.face
			g.face = e.twin.face
			f.Generation = e.Generation
			g.Generation = e.twin.Generation
			f.fromMap,g.fromMap = n,n
			CopiedEdges.Push(f)
			CopiedEdges.Push(g)
		}
	})

	Q := new(vector.Vector)
	T := new(sweepStatus)
	T.segments = new(vector.Vector)

	CopiedEdges.Do(func(l interface{}) {
		e, _ := l.(*Edge)
		if e.start.Point.Less(e.end.Point) {
			evt := new(sweepEvent)
			evt.point = e.start.Point
			evt.coincidentEdge = e
			Q.Push(evt)
			evt = new(sweepEvent)
			evt.point = e.end.Point
			evt.coincidentEdge = nil
			Q.Push(evt)
		}
	})

	heap.Init(Q)

	for Q.Len() > 0 {
		evt, _ := heap.Pop(Q).(*sweepEvent)
		//fmt.Fprintf(os.Stderr,"event: %v\n",evt.point)
		L := new(vector.Vector)
		Lswp := new(sweepStatus)
		Lswp.segments = L
		Lswp.sweepLocation = evt.point
		if evt.coincidentEdge != nil {
			L.Push(evt.coincidentEdge)
		}
		for Q.Len() > 0 && evt.point.Equal(Q.At(0).(*sweepEvent).point) {
			evt, _ := heap.Pop(Q).(*sweepEvent)
			if evt.coincidentEdge != nil {
				L.Push(evt.coincidentEdge)
			}
		}
		sort.Sort(Lswp)
		for i := 0; i < L.Len()-1; {
			if L.At(i).(*Edge).Equal(L.At(i + 1).(*Edge)) {
				L.At(i).(*Edge).newFace = L.At(i + 1).(*Edge).face
				L.At(i).(*Edge).twin.newFace = L.At(i + 1).(*Edge).twin.face
				L.At(i).(*Edge).fromMap = nil //no longer from a unique map
				L.At(i).(*Edge).twin.fromMap = nil //no longer from a unique map
				L.Delete(i + 1)
			} else {
				i++
			}
		}
		//fmt.Fprintf(os.Stderr,"L: %v\n",L)
		R := new(vector.Vector)
		for i := 0; i < T.segments.Len(); {
			e := T.segments.At(i).(*Edge)
			if e.end.Point.Equal(evt.point) {
				R.Push(e)
				T.segments.Delete(i)
			} else if e.Line().On(evt.point) {
				return nil, os.NewError("intersection not at an endpoint")
			} else {
				i++
			}
		}

		// Fill in handle event. You won't need the whole damn thing because
		// Most of the time you just abort with non-terminal intersection
		T.sweepLocation = evt.point
		sort.Sort(T)
		//fmt.Fprintf(os.Stderr,"status: %v\n",T.segments)
		if L.Len() == 0 && R.Len() == 0 {
			return nil, os.NewError("event point with no edges terminal at it " + evt.point.String() + fmt.Sprintf("current status: %v", T.segments))
		} else if L.Len() == 0 {
			above := sort.Search(T.Len(), func(i int) bool {
				return T.segments.At(i).(*Edge).Line().Below(evt.point)
			})
			//fmt.Fprintf(os.Stderr,"Testing status point, no new edge. above: %v, Len: %v\n",above,T.Len())
			if 0 < above && above < T.Len() {
				sa := T.segments.At(above).(*Edge)
				sb := T.segments.At(above - 1).(*Edge)

				cross, _, _ := sb.Line().IntersectAsFloats(sa.Line())
				if cross && !sa.Coterminal(sb) {
					return nil, os.NewError("intersection not at an endpoint")
				}
			}
		} else {
			aboveL := sort.Search(T.Len(), func(i int) bool {
				return L.Last().(*Edge).Line().LessAt(T.segments.At(i).(*Edge).Line(), evt.point)
			})
			belowL := aboveL - 1
			//fmt.Fprintf(os.Stderr,"Testing status point, new edges. above: %v, Len: %v\n",aboveL,T.Len())
			if 0 <= belowL && belowL < T.Len() {
				sa := L.At(0).(*Edge)
				sb := T.segments.At(belowL).(*Edge)

				cross, _, _ := sa.Line().IntersectAsFloats(sb.Line())
				if cross && !sa.Coterminal(sb) {
					return nil, os.NewError("intersection not at an endpoint")
				}
			}
			if aboveL < T.Len() {
				sa := T.segments.At(aboveL).(*Edge)
				sb := L.Last().(*Edge)

				cross, _, _ := sa.Line().IntersectAsFloats(sb.Line())
				if cross && !sa.Coterminal(sb) {
					return nil, os.NewError("intersection not at an endpoint")
				}
			}
		}

		// This is the barrier between preparing the new vertex (below) and determining if the new vertex is good 

		// Setting up edges
		nv := NewVertex(evt.point.Copy())
		R.Do(func(r interface{}) {
			nv.OutgoingEdges.Push(r.(*Edge).twin)
			r.(*Edge).end = nv
			r.(*Edge).twin.start = nv
			o.Edges.Push(r)
			o.Edges.Push(r.(*Edge).twin)
		})
		L.Do(func(l interface{}) {
			l.(*Edge).start = nv
			l.(*Edge).twin.end = nv
			nv.OutgoingEdges.Push(l)
		})
		sort.Sort(nv.OutgoingEdges)

		for i := 0; i < nv.OutgoingEdges.Len(); i++ {
			e := nv.OutgoingEdges.At(i).(*Edge)
			f := nv.OutgoingEdges.At((i + 1) % nv.OutgoingEdges.Len()).(*Edge)
			e.prev = f.twin
			f.twin.next = e
		}

		// Setting up nv's inFace.
		// A new vertex only has relevant inFace information when it comes exclusively from one map, we first determine that
		fromOneMap := true
		fromMap := nv.OutgoingEdges.At(0).(*Edge).fromMap
		nv.OutgoingEdges.Do(func(e interface{}) {
			fromOneMap = fromOneMap && e.(*Edge).fromMap == fromMap && e.(*Edge).fromMap != nil
		})
		// we're from one map, got to find the elements in T from the other map above and below us
		if fromOneMap {
			//fmt.Fprintf(os.Stderr,"from one map, finding face of other\n")
			TfromOtherMap := new(sweepStatus)
			TfromOtherMap.sweepLocation = evt.point
			TfromOtherMap.segments = new(vector.Vector)
			T.segments.Do(func(e interface{}) {
				if e.(*Edge).fromMap != fromMap {
					TfromOtherMap.segments.Push(e)
				}
			})
			sort.Sort(TfromOtherMap)
			above := sort.Search(TfromOtherMap.Len(), func(i int) bool {
				return TfromOtherMap.segments.At(i).(*Edge).Line().Below(evt.point)
			})
			if 0 < above && above < TfromOtherMap.Len() {
				//fmt.Fprintf(os.Stderr,"Testing status point, looking for vertex in face. above: %v, Len: %v\n",above,TfromOtherMap.Len())
				sb := TfromOtherMap.segments.At(above - 1).(*Edge)
				if sb.fromMap == nil {
					if sb.face.fromMap == fromMap {
						nv.inFace = sb.newFace
					} else { 
						nv.inFace = sb.face
					}
				} else {
					nv.inFace = sb.face
				}
			} else if TfromOtherMap.Len() == 0 {
				nv.inFace = nil // completely outside other map, not our problem
			} else if above == 0 {
				sa := TfromOtherMap.segments.At(above).(*Edge)
				if sa.twin.fromMap == nil {
					if sa.twin.face.fromMap == fromMap {
						nv.inFace = sa.twin.newFace
					} else { 
						nv.inFace = sa.twin.face
					}
				} else {
					nv.inFace = sa.twin.face
				}
			} else if above == TfromOtherMap.Len() {
				sb := TfromOtherMap.segments.At(above - 1).(*Edge)
				nv.inFace = sb.face
				if sb.fromMap == nil {
					if sb.face.fromMap == fromMap {
						nv.inFace = sb.newFace
					} else { 
						nv.inFace = sb.face
					}
				} else {
					nv.inFace = sb.face
				}
			} 
		} else {
			nv.inFace = nil //we're not from one map, this vertex happens at the overlay, edges have complete face information.
		}

		o.Verticies.Push(nv)

		// Vertex done, add any new edges to the sweep line.

		L.Do(func(l interface{}) {
			T.segments.Push(l)
		})
	}

	var leFuck string
	for i := 0; i < o.Edges.Len(); i++ {
		e, _ := o.Edges.At(i).(*Edge)
		if e.visited {
			continue
		}
		//fmt.Fprintf(os.Stderr,"found a face containing: %v,",e.start)

		F := new(Face)
		F.boundary = e
		F.fromMap = o
		e.visited = true
		oldFaces := make(map[*Face]int)
		//fmt.Fprintf(os.Stderr,"%v: ",e.start)
		if e.face != nil {
			//fmt.Fprintf(os.Stderr,"f %v ",e.face.Value)
			oldFaces[e.face]++
		}
		if e.newFace != nil {
			//fmt.Fprintf(os.Stderr,"nf %v ",e.newFace.Value)
			oldFaces[e.newFace]++
		}
		if e.start.inFace != nil {
			//fmt.Fprintf(os.Stderr,"if %v ",e.start.inFace.Value)
			oldFaces[e.start.inFace]++
		}
		if e.face == nil && e.newFace == nil {
			panic("the edge without a face\n")
		}
		e.face = F

		for f := e.Next(); f != e; f = f.Next() {
			//fmt.Fprintf(os.Stderr,"%v: ",f.start)
			f.visited = true
			if f.face != nil {
				//fmt.Fprintf(os.Stderr,"f %v ",f.face.Value)
				oldFaces[f.face]++
			}
			if f.newFace != nil {
				//fmt.Fprintf(os.Stderr,"nf %v ",f.newFace.Value)
				oldFaces[f.newFace]++
			}
			if f.start.inFace != nil {
				//fmt.Fprintf(os.Stderr,"if %v ",f.start.inFace.Value)
				oldFaces[f.start.inFace]++
			}
			//fmt.Fprintf(os.Stderr,",")
			f.face = F
		}
		//os.Stderr.WriteString("\n")

		//fmt.Fprintf(os.Stderr,"%v old faces\n",len(oldFaces))
		if len(oldFaces) > 2 {
			leFuck += fmt.Sprintf("%v faces overlapping a new face, input must have been malformed, maps m: %p n: %p\n", len(oldFaces), m, n)
			for f, _ := range oldFaces {
				leFuck = leFuck + fmt.Sprintf("face %p from: %p containing: %v\n", f, f.fromMap, f.Value)
			}
			//os.Stderr.WriteString(leFuck)
		} else if len(oldFaces) == 0 {
			panic(fmt.Sprintf("No old faces. e: %v, e.face: %+v, maps: m: %p n: %p o: %p\n", e, e.face, m, n, o))
		}

		var mFace, nFace *Face
		for f, _ := range oldFaces {
			if f.fromMap == m {
				mFace = f
			} else {
				nFace = f
			}
		}
		if mFace != nil && nFace != nil {
			v, ok := mergeFaces(mFace.Value, nFace.Value)
			if ok != nil {
				return nil, ok
			}
			if mFace.Type != "" {
				F.Type = mFace.Type
			} else if nFace.Type != "" {
				F.Type = nFace.Type
			}
			F.Value = v
		} else if mFace != nil {
			F.Value = mFace.Value
			F.Type = mFace.Type
		} else if nFace != nil {
			F.Value = nFace.Value
			F.Type = nFace.Type
		} else {
			panic(fmt.Sprintf("face didn't come from an mFace or an nFace, pointers m: %v n: %v o: %v face: %v", m, n, o, e.face))
		}

		o.Faces.Push(F)
	}

	if leFuck != "" {
		return o, os.NewError(leFuck)
	}

	o.Init()
	return o, nil
}