46 lines
626 B
Go
46 lines
626 B
Go
package main
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type Fifo struct {
|
|
slice []string
|
|
mu sync.Mutex
|
|
size int
|
|
}
|
|
|
|
func newfifo(size int) *Fifo {
|
|
q := Fifo{}
|
|
q.slice = make([]string, 0)
|
|
q.size = size
|
|
return &q
|
|
}
|
|
func (q *Fifo) Add(v string) int {
|
|
q.mu.Lock()
|
|
i := q.Contains(v)
|
|
if i == -1 {
|
|
if len(q.slice) >= q.size {
|
|
q.DeleteFirst()
|
|
}
|
|
q.slice = append(q.slice, v)
|
|
}
|
|
q.mu.Unlock()
|
|
return i
|
|
}
|
|
|
|
func (q *Fifo) Contains(v string) int {
|
|
for i, val := range q.slice {
|
|
if val == v {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
func (q *Fifo) DeleteFirst() string {
|
|
a := q.slice[0]
|
|
q.slice = q.slice[1:]
|
|
return a
|
|
}
|