ls 5 роки тому
батько
коміт
03fb34bb28
2 змінених файлів з 152 додано та 0 видалено
  1. 95 0
      queue.go
  2. 57 0
      utils.go

+ 95 - 0
queue.go

@@ -0,0 +1,95 @@
+package util
+
+import (
+	"container/list"
+	"sync"
+)
+
+// Queue concurrency safe list
+type Queue struct {
+	l *list.List
+	m sync.Mutex
+}
+
+// NewQueue new queue
+func NewQueue() *Queue {
+	return &Queue{l: list.New()}
+}
+
+// PushBack inserts a new element e with value v at the back of list l and returns e.
+func (q *Queue) PushBack(v interface{}) *list.Element {
+	q.m.Lock()
+	defer q.m.Unlock()
+
+	return q.l.PushBack(v)
+}
+
+// PushFront inserts a new element e with value v at the front of list l and returns e.
+func (q *Queue) PushFront(v interface{}) *list.Element {
+	q.m.Lock()
+	defer q.m.Unlock()
+
+	return q.l.PushFront(v)
+}
+
+// InsertBefore inserts a new element e with value v immediately before mark and returns e.
+// If mark is not an element of l, the list is not modified.
+// The mark must not be nil.
+func (q *Queue) InsertBefore(v interface{}, mark *list.Element) *list.Element {
+	q.m.Lock()
+	defer q.m.Unlock()
+
+	return q.l.InsertBefore(v, mark)
+}
+
+// InsertAfter inserts a new element e with value v immediately after mark and returns e.
+// If mark is not an element of l, the list is not modified.
+// The mark must not be nil.
+func (q *Queue) InsertAfter(v interface{}, mark *list.Element) *list.Element {
+	q.m.Lock()
+	defer q.m.Unlock()
+
+	return q.l.InsertAfter(v, mark)
+}
+
+// Remove removes e from l if e is an element of list l.
+// It returns the element value e.Value.
+// The element must not be nil.
+func (q *Queue) Remove(e *list.Element) interface{} {
+	q.m.Lock()
+	defer q.m.Unlock()
+
+	return q.l.Remove(e)
+}
+
+// Len returns the number of elements of list l.
+// The complexity is O(1).
+func (q *Queue) Len() int {
+	q.m.Lock()
+	defer q.m.Unlock()
+	return q.l.Len()
+}
+
+// Back returns the last element of list l or nil if the list is empty.
+func (q *Queue) Back() *list.Element {
+	q.m.Lock()
+	defer q.m.Unlock()
+
+	return q.l.Back()
+}
+
+// Front returns the first element of list l or nil if the list is empty.
+func (q *Queue) Front() *list.Element {
+	q.m.Lock()
+	defer q.m.Unlock()
+
+	return q.l.Front()
+}
+
+// Init initializes or clears list l.
+func (q *Queue) Init(v interface{}, mark *list.Element) *list.List {
+	q.m.Lock()
+	defer q.m.Unlock()
+
+	return q.l.Init()
+}

+ 57 - 0
utils.go

@@ -0,0 +1,57 @@
+package util
+
+import (
+	"encoding/json"
+	"encoding/xml"
+)
+
+const (
+	_ = iota
+	// KB  kilo byte
+	KB int = 1 << (10 * iota)
+	// MB mega byte
+	MB
+	// GB giga byte
+	GB
+	// TB tera byte
+	TB
+	// PB peta byte
+	PB
+)
+
+// Map is a shortcut for map[string]interface{}
+type Map map[string]interface{}
+
+// String map to json string
+func (m Map) String() string {
+	bs, _ := json.Marshal(m)
+	return string(bs)
+}
+
+// Bytes map to json bytes
+func (m Map) Bytes() []byte {
+	bs, _ := json.Marshal(m)
+	return bs
+}
+
+// MarshalXML allows type Map to be used with xml.Marshal.
+func (m Map) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
+	start.Name = xml.Name{
+		Space: "",
+		Local: "map",
+	}
+	if err := e.EncodeToken(start); err != nil {
+		return err
+	}
+	for key, value := range m {
+		elem := xml.StartElement{
+			Name: xml.Name{Space: "", Local: key},
+			Attr: []xml.Attr{},
+		}
+		if err := e.EncodeElement(value, elem); err != nil {
+			return err
+		}
+	}
+
+	return e.EncodeToken(xml.EndElement{Name: start.Name})
+}