xtypes

package module
v0.0.0-...-93946f3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2019 License: MIT Imports: 5 Imported by: 0

README

xtypes

GoDoc Go Report Card

xtypes provides useful data structures for everyday use.

Description

The package provides the following data structures:

  • Priority Queue based on container/heap from standard library
  • Queue based on slice
  • Semaphore implemented with a channel
  • Safe Map

These types are safe for concurrent use.

Install

go get github.com/golocron/xtypes

Examples

Examples can be found here

go run examples/examples.go

doing work in order by priority...
working in order: task with priority: 18
working in order: task with priority: 22
working in order: task with priority: 25
working in order: task with priority: 29
working in order: task with priority: 45
working in order: task with priority: 53
working in order: task with priority: 64
working in order: task with priority: 67
working in order: task with priority: 81
working in order: task with priority: 86
doing work in parallel...
working in parallel: task with priority: 18
working in parallel: task with priority: 53
working in parallel: task with priority: 29
working in parallel: task with priority: 81
working in parallel: task with priority: 45
working in parallel: task with priority: 64
working in parallel: task with priority: 25
working in parallel: task with priority: 22
working in parallel: task with priority: 86
working in parallel: task with priority: 67

Documentation

Overview

Package xtypes prodives useful data structures.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyQueue is returned when an non-applicable operation was called on queue due to its empty state.
	ErrEmptyQueue = errors.New("empty queue")

	// ErrInvalidQueue is returned when an non-applicable operation was called on a nil queue.
	ErrInvalidQueue = errors.New("invalid queue")
)

Functions

func Copy

func Copy(dst, src interface{}) error

Types

type ExtMap

type ExtMap map[string]interface{}

ExtMap represents an extended map with come useful methods for convenience.

It almost the same as url.Values/http.Header. The main purpose of this type is to put values and then marshal them to JSON. This is NOT thread safe.

func (ExtMap) Del

func (m ExtMap) Del(key string)

Del deletes the value associated with key.

func (ExtMap) Encode

func (m ExtMap) Encode() []byte

Encode encodes the values into raw json bytes.

func (ExtMap) Get

func (m ExtMap) Get(key string) interface{}

Get gets the value associated with the given key. If there is no value associated with the key, Get returns the nil.

func (ExtMap) Set

func (m ExtMap) Set(key string, value interface{})

Set sets the key to value. It replaces the existing value.

type PQItem

type PQItem interface {
	Priority() int
	Index() int
	SetIndex(idx int)
}

PQItem defines contract which an item of a queue must implement.

type PQItems

type PQItems []PQItem

PQItems represents the queue items.

func (PQItems) Len

func (pqi PQItems) Len() int

Len implements heap.Interface. Returns the len of underlying storage.

func (PQItems) Less

func (pqi PQItems) Less(i, j int) bool

Less implements heap.Interface. Returns true if inequality is met.

func (*PQItems) Pop

func (pqi *PQItems) Pop() interface{}

Pop implements heap.Interface. Returns the first item.

func (*PQItems) Push

func (pqi *PQItems) Push(x interface{})

Push implements heap.Interface. Inserts the x to the queue.

func (PQItems) Swap

func (pqi PQItems) Swap(i, j int)

Swap implements heap.Interface. Swaps two elements.

type PriorityQueue

type PriorityQueue struct {
	// contains filtered or unexported fields
}

PriorityQueue is a priority queue implemented using heap.

This is clean and simple thread safe implementation with no magic. PriorityQueue MUST be created using constructor.

func NewPriorityQueue

func NewPriorityQueue(hint int) *PriorityQueue

NewPriorityQueue creates and inits a new PriorityQueue.

func (*PriorityQueue) Empty

func (pq *PriorityQueue) Empty() bool

Empty returns true if the queue is empty.

func (*PriorityQueue) Get

func (pq *PriorityQueue) Get(n int) ([]PQItem, error)

Get returns up to requested n of items.

func (*PriorityQueue) Len

func (pq *PriorityQueue) Len() int

Len returns the len of the queue.

func (*PriorityQueue) Peek

func (pq *PriorityQueue) Peek() PQItem

Peek returns the first element without modifying the queue.

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() (PQItem, error)

Pop returns the first element from the queue. If the queue is empty - an error will be returned.

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(item PQItem) error

Push adds an item to the queue. If the underlying storage is nil - an error will be returned.

func (*PriorityQueue) Put

func (pq *PriorityQueue) Put(items ...PQItem) error

Put adds items to queue.

type QItems

type QItems []interface{}

QItems represents the queue items.

func (*QItems) Pop

func (qi *QItems) Pop() interface{}

Pop returns the first item.

func (*QItems) Push

func (qi *QItems) Push(x interface{})

Push inserts the x to the queue.

type Queue

type Queue struct {
	// contains filtered or unexported fields
}

Queue is a simple queue based on slice.

This is clean and simple thread safe implementation with no magic. Queue MUST be created using constructor.

func NewQueue

func NewQueue(hint int) *Queue

NewQueue creates and inits a new Queue.

func (*Queue) Empty

func (q *Queue) Empty() bool

Empty returns true if the queue is empty.

func (*Queue) Get

func (q *Queue) Get(n int) ([]interface{}, error)

Get returns up to requested n of items.

func (*Queue) Len

func (q *Queue) Len() int

Len returns the len of the queue.

func (*Queue) Peek

func (q *Queue) Peek() interface{}

Peek returns the first element without modifying the queue.

func (*Queue) Pop

func (q *Queue) Pop() (interface{}, error)

Pop returns the first element from the queue. If the queue is empty - an error will be returned.

func (*Queue) Push

func (q *Queue) Push(x interface{}) error

Push adds an item to the queue. If the underlying storage is nil - an error will be returned.

func (*Queue) Put

func (q *Queue) Put(items ...interface{}) error

Put adds items to queue.

type SafeMap

type SafeMap struct {
	// contains filtered or unexported fields
}

SafeMap provides a storage based on a map.

It is safe to use in concurrent mode. The storage is protected by the mutex.

func NewSafeMap

func NewSafeMap() *SafeMap

NewSafeMap returns a ready to use instance of SafeMap.

func (*SafeMap) Del

func (s *SafeMap) Del(key string)

Del deletes the object.

func (*SafeMap) Drain

func (s *SafeMap) Drain() []interface{}

Drain returns all elements as slice and removes keys from the storage. Elements are sorted by key.

func (*SafeMap) Get

func (s *SafeMap) Get(key string) (interface{}, bool)

Get returns object.

func (*SafeMap) Keys

func (s *SafeMap) Keys() []string

Keys returns all the keys as a slice.

func (*SafeMap) Len

func (s *SafeMap) Len() int

Len returns count of elements in the storage.

func (*SafeMap) Set

func (s *SafeMap) Set(key string, value interface{}) error

Set sets the object.

type Semaphore

type Semaphore chan struct{}

Semaphore presents a channel that implements the Semaphore pattern.

func NewSemaphore

func NewSemaphore(size int) Semaphore

NewSemaphore returns a new semaphore ready to use.

func (Semaphore) Acquire

func (s Semaphore) Acquire(n int)

Acquire gets the n resources from the Semaphore.

func (Semaphore) Release

func (s Semaphore) Release(n int)

Release returns the specified number of resources to the Semaphore.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL