本文整理汇总了Golang中container/list.List.PushFront方法的典型用法代码示例。如果您正苦于以下问题:Golang List.PushFront方法的具体用法?Golang List.PushFront怎么用?Golang List.PushFront使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类container/list.List
的用法示例。
在下文中一共展示了List.PushFront方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
give := make(chan []byte)
get := make(chan []byte)
go func() {
q := new(list.List)
for {
if q.Len() == 0 {
q.PushFront(make([]byte, 100))
}
e := q.Front()
select {
case s := <-give:
q.PushFront(s)
case get <- e.Value.([]byte):
q.Remove(e)
}
}
}()
// Gets a new buffer from the recycler.
buffer := <-get
// Give it back to the recycler.
give <- buffer
// Get the recycled buffer again.
buffer = <-get
}
示例2: StackSpec
func StackSpec(c Context) {
stack := new(list.List)
c.Specify("An empty stack", func() {
c.Specify("contains no elements", func() {
c.Expect(stack.Len()).Equals(0)
})
})
c.Specify("When elements are pushed onto a stack", func() {
stack.PushFront("pushed first")
stack.PushFront("pushed last")
c.Specify("then it contains some elements", func() {
c.Expect(stack.Len()).NotEquals(0)
})
c.Specify("the element pushed last is popped first", func() {
poppedFirst := stack.Remove(stack.Front())
c.Expect(poppedFirst).Equals("pushed last")
})
c.Specify("the element pushed first is popped last", func() {
stack.Remove(stack.Front())
poppedLast := stack.Remove(stack.Front())
c.Expect(poppedLast).Equals("pushed first")
})
})
}
示例3: houseKeeping
func houseKeeping(get, give chan interface{},
factory func() interface{}) {
q := new(list.List)
for {
if q.Len() == 0 {
atomic.AddInt64(&makes, 1)
q.PushFront(queued{when: time.Now(), data: factory()})
}
element := q.Front()
timeout := time.NewTimer(time.Minute)
select {
case b := <-give:
timeout.Stop()
q.PushFront(queued{when: time.Now(), data: b})
case get <- element.Value.(queued).data:
timeout.Stop()
q.Remove(element)
case <-timeout.C:
e := q.Front()
for e != nil {
n := e.Next()
if time.Since(e.Value.(queued).when) > time.Minute {
q.Remove(e)
e.Value = nil
}
e = n
}
}
}
}
示例4: CreateObject
func CreateObject(l *list.List, newId, protectorId *protected_objects.ObjectIdMessage,
encKey *tao.Keys, program *auth.Prin, domain *tao.Domain, newType string,
newVal []byte) error {
if !domain.Guard.IsAuthorized(*program, "CREATE", []string{protectorId.String()}) {
return errors.New("program not authorized to create requested secret")
}
_, _, err := readObjRec(l, encKey, newId)
if err == nil {
return errors.New("creating object with existing id")
}
protectorType, protectorKey, err := readObjRec(l, encKey, protectorId)
if err != nil {
return err
}
if *protectorType != "key" {
return errors.New("creating object protected by object type not key")
}
new := protected_objects.ObjectMessage{
ObjId: newId,
ObjVal: newVal,
ObjType: &newType}
pNew, err := protected_objects.MakeProtectedObject(new, *protectorId.ObjName,
*protectorId.ObjEpoch, protectorKey)
l.PushFront(*pNew)
return nil
}
示例5: Insert
func Insert(e Elem, L *list.List) int {
if L.Len() == 0 {
L.PushFront(e)
return L.Len()
}
front := L.Front()
if e.GetTime() < front.Value.(Elem).GetTime() {
L.InsertBefore(e, front)
return L.Len()
}
el := L.Back()
Loop:
for {
if el.Value.(Elem).GetTime() > e.GetTime() {
el = el.Prev()
} else {
break Loop
}
}
L.InsertAfter(e, el)
return L.Len()
}
示例6: bufferRecycler
func bufferRecycler(give, get chan *bytes.Buffer) {
q := new(list.List)
for {
//This means that we are getting more than we are giving and memory is not being
//cleaned up properly
if q.Len() > 1000 {
log.Warnf("Memory Recycler Overload (high memory use): %d", q.Len())
}
if q.Len() == 0 {
q.PushFront(&bytes.Buffer{})
}
e := q.Front()
select {
case s := <-give:
s.Reset()
q.PushFront(s)
case get <- e.Value.(*bytes.Buffer):
q.Remove(e)
}
}
}
示例7: recycler
func recycler(give, get chan []byte) {
q := new(list.List)
for {
//This means that we are getting more than we are giving and memory is not being
//cleaned up properly
if q.Len() > 1000 {
log.Warnf("Memory Recycler Overload (high memory use): %d", q.Len())
}
if q.Len() == 0 {
q.PushFront(make([]byte, defaultMemorySize))
}
e := q.Front()
select {
case s := <-give:
q.PushFront(s[:0])
case get <- e.Value.([]byte):
q.Remove(e)
}
}
}
示例8: buildArgumentsTrim
func (server *Server) buildArgumentsTrim(arguments *list.List, params imageserver.Params) error {
trim, _ := params.GetBool("trim")
if trim {
// We must execute trim first. (order of operations)
arguments.PushFront("-trim")
}
return nil
}
示例9: HandleResponse
func HandleResponse(r *ReplyInfo, completed *int, jobs *list.List) {
if r.OK {
//add to our completed count
*completed++
} else {
//add job back to queue, it failed.
jobs.PushFront(r.job)
}
}
示例10: dfsrp
func dfsrp(g *Digraph, s int, marked map[int]bool, l *list.List) {
marked[s] = true
for _, v := range g.Adj(s) {
if !marked[v.vertex] {
dfsrp(g, v.vertex, marked, l)
}
}
l.PushFront(s) // Reverse post
}
示例11: Bish_mech
func Bish_mech(bpos pos) list.List {
options := list.List{}
// positions of the form row+i,col+i, where i is (TODO: fix this)> -min(row,col) and <
// for now lazy implementation that will have options removed as out of board later
for i := -7; i < 8; i++ {
options.PushFront(pos{bpos.row + i, bpos.col + i})
}
return options
}
示例12: Pawn_mech
func Pawn_mech(ppos pos) list.List {
options := list.List{}
// +1 fwd, TODO: +2 fwd if on homeRow
// TODO: en passant
options.PushFront(pos{ppos.row + 1, ppos.col})
// pawn attack
options.PushFront(pos{ppos.row + 1, ppos.col + 1})
options.PushFront(pos{ppos.row + 1, ppos.col - 1})
return options
}
示例13: AddObject
// Add the indicated protected object to the list.
func AddObject(l *list.List, obj ObjectMessage) error {
for e := l.Front(); e != nil; e = e.Next() {
o := e.Value.(ObjectMessage)
if o.ObjId.ObjName == obj.ObjId.ObjName && o.ObjId.ObjEpoch == obj.ObjId.ObjEpoch {
return nil
}
}
l.PushFront(interface{}(obj))
return nil
}
示例14: King_mech
func King_mech(kpos pos) list.List {
options := list.List{}
// TODO: casteling, requires remembering if King ever has been in check
for a := -1; a < 2; a++ {
for b := -1; b < 2; b++ {
options.PushFront(pos{kpos.row + a, kpos.col + b})
// TODO: remove no-move move, (a=0,b=0)
}
}
return options
}
示例15: Rook_mech
func Rook_mech(rpos pos) list.List {
options := list.List{}
for newCol := 1; newCol < 9; newCol++ {
options.PushFront(pos{rpos.row, newCol})
}
for newRow := 1; newRow < 9; newRow++ {
options.PushFront(pos{newRow, rpos.col})
}
//TODO: this will include current position twice and should have current position removed from options by filtering!
return options
}