// C & U func (node Node) Update(cmp *RemoteNode) { bucketID := node.ID.DistanceTo(cmp.ID).GetBucketID() bucket := node.RoutingTable[bucketID] var found bool = false var foundElement *list.Element for elem := bucket.Front(); elem != nil; elem = elem.Next() { e, ok := elem.Value.(*RemoteNode) if !ok { continue // if it's not a NodeID, wtf is it doing in the list?? Probably should error out } if e.ID.EqualsTo(cmp.ID) || e.ID.EqualsTo(node.ID) { found = true foundElement = elem break } } if !found { if bucket.Len() <= BUCKET_SIZE { bucket.PushFront(cmp) } } else { foundElement.Value = cmp // update the foundElement value bucket.MoveToFront(foundElement) } }
func (c *LRUCache) updateInplace(e *list.Element, v interface{}) { e.Value = v c.moveToFront(e) c.checkCapacity() }
func snakeServer(wsch chan *websocket.Conn, comch chan *Command, msgch chan string) { sockets := list.New() wsm := make(map[*websocket.Conn]*Snake) timer := time.NewTicker(200 * time.Millisecond) food := [...]int{rand.Intn(size), rand.Intn(size)} for { select { case com := <-comch: log.Println("receive:", com.C) switch com.C { case "enter": sockets.PushBack(com.W) case "ready": snake := &Snake{Pos: Up, Nodes: list.New(), Alive: true} snake.Nodes.PushBack([...]int{size / 2, size / 2}) snake.Nodes.PushBack([...]int{size / 2, size/2 + 1}) wsm[com.W] = snake case "quit": log.Println("quit") e := new(list.Element) e.Value = com.W sockets.Remove(e) delete(wsm, com.W) case "up": //1 snake := wsm[com.W] if snake != nil && snake.Pos != Down { snake.Pos = Up } case "down": //2 snake := wsm[com.W] if snake != nil && snake.Pos != Up { snake.Pos = Down } case "left": //3 snake := wsm[com.W] if snake != nil && snake.Pos != Right { snake.Pos = Left } case "right": //4 snake := wsm[com.W] if snake != nil && snake.Pos != Left { snake.Pos = Right } default: //ignore } case <-timer.C: v := make([][]int, size) for i := 0; i < size; i++ { v[i] = make([]int, size) for j := 0; j < size; j++ { v[i][j] = 0 } } for _, s := range wsm { if s.Alive { ft := (s.Nodes.Front().Value).([2]int) npw := ft[0] + s.Pos[0] nph := ft[1] + s.Pos[1] if npw >= 0 && npw < 20 && nph >= 0 && nph < 20 { if food[0] == npw && food[1] == nph { food = [...]int{rand.Intn(size), rand.Intn(size)} } else { s.Nodes.Remove(s.Nodes.Back()) } s.Nodes.PushFront([...]int{npw, nph}) } for e := s.Nodes.Front(); e != nil; e = e.Next() { ee := (e.Value).([2]int) v[ee[0]][ee[1]] = 1 } } } v[food[0]][food[1]] = 2 SendMessage(sockets, v) case msg := <-msgch: SendMessage(sockets, msg) } } }