func outOfOrder(l *list.List) { iTotal := 25 if iTotal > l.Len() { iTotal = l.Len() } ll := make([]*list.List, iTotal) for i := 0; i < iTotal; i++ { ll[i] = list.New() } r := rand.New(rand.NewSource(time.Now().UnixNano())) for e := l.Front(); e != nil; e = e.Next() { fpath, ok := e.Value.(string) if !ok { panic("The path is invalid string") } if rand.Int()%2 == 0 { ll[r.Intn(iTotal)].PushFront(fpath) } else { ll[r.Intn(iTotal)].PushBack(fpath) } } r0 := rand.New(rand.NewSource(time.Now().UnixNano())) l.Init() for i := 0; i < iTotal; i++ { if r0.Intn(2) == 0 { l.PushBackList(ll[i]) } else { l.PushFrontList(ll[i]) } ll[i].Init() } }
func tileWorker(T *quadratic.Map, alternativeStack chan *list.List, sink chan<- *quadratic.Map, workerCount chan int, halt chan int, tileMaps []*quadratic.Map, chooseNextEdge func(*quadratic.Map) *quadratic.Edge, maxtiles int, tileSymmetry string, showIntermediate bool) { localAlternatives := new(list.List) Work: for { select { case <-halt: halt <- 1 fmt.Fprintf(os.Stderr, "premature halt\n") return case L := <-alternativeStack: L.PushFrontList(localAlternatives) localAlternatives.Init() alternativeStack <- L default: if T.Faces.Len() > maxtiles && maxtiles > 0 { sink <- T break Work } else if noActiveFaces(T) { finishTime, _, _ := os.Time() fmt.Fprintf(os.Stderr, "new tiling complete, took %v seconds\n", finishTime-initializationTime) sink <- T break Work } else if showIntermediate { sink <- T } alternatives := addTilesByEdge(T, tileMaps, chooseNextEdge, tileSymmetry) if alternatives.Len() == 0 { break Work } T = alternatives.Remove(alternatives.Front()).(*quadratic.Map) localAlternatives.PushFrontList(alternatives) //fmt.Fprintf(os.Stderr,"currently have %v faces\n",T.Faces.Len()) } } L := <-alternativeStack L.PushFrontList(localAlternatives) localAlternatives.Init() alternativeStack <- L workers := <-workerCount workerCount <- workers - 1 }
//to sort a list, based on the distance from the given key in ascending order func SortList(l *list.List, keyId ID) { //a new list to keep the sorted elements sorted := list.New() //iterate over the given list for e := l.Front(); e != nil; e = e.Next() { var changed bool = false //compute distance var c *Contact c = e.Value.(*Contact) dist := c.NodeID.Xor(keyId) //loop over the sorted array to find position for the current element if sorted.Len() == 0 { sorted.PushFront(c) continue } for s := sorted.Front(); s != nil; s = s.Next() { c1 := s.Value.(*Contact) dist1 := c1.NodeID.Xor(keyId) larger := dist1.Less(dist) // if larger is true then insert the element in the current position // end the loop if larger { changed = true sorted.InsertBefore(c, s) break } } //if not changed, insert the element at the back if !changed { sorted.PushBack(c) } } //push the sorted list into the parameter of the function l = list.New() l.PushFrontList(sorted) return }