util

package
v1.3.33 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

gox/util

Documentation

Overview

Package containers provides core interfaces and functions for data structures.

Container is the base interface for all data structures to implement.

Iterators provide stateful iterators.

Enumerable provides Ruby inspired (each, select, map, find, any?, etc.) container functions.

Serialization provides serializers (marshalers) and deserializers (unmarshalers).

Package lists provides an abstract List interface.

In computer science, a list or sequence is an abstract data type that represents an ordered sequence of values, where the same value may occur more than once. An instance of a list is a computer representation of the mathematical concept of a finite sequence; the (potentially) infinite analog of a list is a stream. Lists are a basic example of containers, as they contain other values. If the same value occurs multiple times, each occurrence is considered a distinct item.

Reference: https://en.wikipedia.org/wiki/List_%28abstract_data_type%29

Package maps provides an abstract Map interface.

In computer science, an associative array, map, symbol table, or dictionary is an abstract data type composed of a collection of (key, value) pairs, such that each possible key appears just once in the collection.

Operations associated with this data type allow: - the addition of a pair to the collection - the removal of a pair from the collection - the modification of an existing pair - the lookup of a value associated with a particular key

Reference: https://en.wikipedia.org/wiki/Associative_array

Package sets provides an abstract Set interface.

In computer science, a set is an abstract data type that can store certain values and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

Reference: https://en.wikipedia.org/wiki/Set_%28abstract_data_type%29

Package stacks provides an abstract Stack interface.

In computer science, a stack is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the most recently added element that was not yet removed. The order in which elements come off a stack gives rise to its alternative name, LIFO (for last in, first out). Additionally, a peek operation may give access to the top without modifying the stack.

Reference: https://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29

Package trees provides an abstract Tree interface.

In computer science, a tree is a widely used abstract data type (ADT) or data structure implementing this ADT that simulates a hierarchical tree structure, with a root value and subtrees of children with a parent node, represented as a set of linked nodes.

Reference: https://en.wikipedia.org/wiki/Tree_%28data_structure%29

Package utils provides common utility functions.

Provided functionalities: - sorting - comparators

Index

Constants

View Source
const (
	// DateFormat  日期格式 yyyy-MM-dd
	DateFormat = "2006-01-02"
	// DateFormat2 日期格式 yyyyMMdd
	DateFormat2 = "20060102"
	// DateFormat3 日期格式 yyMMdd
	DateFormat3 = "060102"

	// TimeFormat 时间格式 yyyy-MM-dd HH:mm:ss
	TimeFormat = "2006-01-02 15:04:05"
	// TimeFormat2 时间格式 yyyyMMddHHmmss
	TimeFormat2 = "20060102150405"

	// Timestamp 时间戳 - 毫秒 时间格式 yyyy-MM-dd HH:mm:ss.SSS
	Timestamp = "2006-01-02 15:04:05.000"
)

Variables

This section is empty.

Functions

func ByteComparator

func ByteComparator(a, b interface{}) int

ByteComparator provides a basic comparison on byte

func Float32Comparator

func Float32Comparator(a, b interface{}) int

Float32Comparator provides a basic comparison on float32

func Float64Comparator

func Float64Comparator(a, b interface{}) int

Float64Comparator provides a basic comparison on float64

func GetSortedValues

func GetSortedValues(container Container, comparator Comparator) []interface{}

GetSortedValues returns sorted container's elements with respect to the passed comparator. Does not effect the ordering of elements within the container.

func Int16Comparator

func Int16Comparator(a, b interface{}) int

Int16Comparator provides a basic comparison on int16

func Int32Comparator

func Int32Comparator(a, b interface{}) int

Int32Comparator provides a basic comparison on int32

func Int64Comparator

func Int64Comparator(a, b interface{}) int

Int64Comparator provides a basic comparison on int64

func Int8Comparator

func Int8Comparator(a, b interface{}) int

Int8Comparator provides a basic comparison on int8

func IntComparator

func IntComparator(a, b interface{}) int

IntComparator provides a basic comparison on int

func RuneComparator

func RuneComparator(a, b interface{}) int

RuneComparator provides a basic comparison on rune

func Sort

func Sort(values []interface{}, comparator Comparator)

Sort sorts values (in-place) with respect to the given comparator.

Uses Go's sort (hybrid of quicksort for large and then insertion sort for smaller slices).

func StringComparator

func StringComparator(a, b interface{}) int

StringComparator provides a fast comparison on strings

func TimeComparator

func TimeComparator(a, b interface{}) int

TimeComparator provides a basic comparison on time.Time

func ToString

func ToString(value interface{}) string

ToString converts a value to string.

func UInt16Comparator

func UInt16Comparator(a, b interface{}) int

UInt16Comparator provides a basic comparison on uint16

func UInt32Comparator

func UInt32Comparator(a, b interface{}) int

UInt32Comparator provides a basic comparison on uint32

func UInt64Comparator

func UInt64Comparator(a, b interface{}) int

UInt64Comparator provides a basic comparison on uint64

func UInt8Comparator

func UInt8Comparator(a, b interface{}) int

UInt8Comparator provides a basic comparison on uint8

func UIntComparator

func UIntComparator(a, b interface{}) int

UIntComparator provides a basic comparison on uint

Types

type BidiMap

type BidiMap interface {
	GetKey(value interface{}) (key interface{}, found bool)

	Map
}

BidiMap interface that all bidirectional maps implement (extends the Map interface)

type Comparator

type Comparator func(a, b interface{}) int

Comparator will make type assertion (see IntComparator for example), which will panic if a or b are not of the asserted type.

Should return a number:

negative , if a < b
zero     , if a == b
positive , if a > b

type Container

type Container interface {
	Empty() bool
	Size() int
	Clear()
	Values() []interface{}
}

Container is base interface that all data structures implement.

type EnumerableWithIndex

type EnumerableWithIndex interface {
	// Each calls the given function once for each element, passing that element's index and value.
	Each(func(index int, value interface{}))

	// Any passes each element of the container to the given function and
	// returns true if the function ever returns true for any element.
	Any(func(index int, value interface{}) bool) bool

	// All passes each element of the container to the given function and
	// returns true if the function returns true for all elements.
	All(func(index int, value interface{}) bool) bool

	// Find passes each element of the container to the given function and returns
	// the first (index,value) for which the function is true or -1,nil otherwise
	// if no element matches the criteria.
	Find(func(index int, value interface{}) bool) (int, interface{})
}

EnumerableWithIndex provides functions for ordered containers whose values can be fetched by an index.

type EnumerableWithKey

type EnumerableWithKey interface {
	// Each calls the given function once for each element, passing that element's key and value.
	Each(func(key interface{}, value interface{}))

	// Any passes each element of the container to the given function and
	// returns true if the function ever returns true for any element.
	Any(func(key interface{}, value interface{}) bool) bool

	// All passes each element of the container to the given function and
	// returns true if the function returns true for all elements.
	All(func(key interface{}, value interface{}) bool) bool

	// Find passes each element of the container to the given function and returns
	// the first (key,value) for which the function is true or nil,nil otherwise if no element
	// matches the criteria.
	Find(func(key interface{}, value interface{}) bool) (interface{}, interface{})
}

EnumerableWithKey provides functions for ordered containers whose values whose elements are key/value pairs.

type IteratorWithIndex

type IteratorWithIndex interface {
	// Next moves the iterator to the next element and returns true if there was a next element in the container.
	// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
	// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
	// Modifies the state of the iterator.
	Next() bool

	// Value returns the current element's value.
	// Does not modify the state of the iterator.
	Value() interface{}

	// Index returns the current element's index.
	// Does not modify the state of the iterator.
	Index() int

	// Begin resets the iterator to its initial state (one-before-first)
	// Call Next() to fetch the first element if any.
	Begin()

	// First moves the iterator to the first element and returns true if there was a first element in the container.
	// If First() returns true, then first element's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	First() bool
}

IteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.

type IteratorWithKey

type IteratorWithKey interface {
	// Next moves the iterator to the next element and returns true if there was a next element in the container.
	// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
	// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
	// Modifies the state of the iterator.
	Next() bool

	// Value returns the current element's value.
	// Does not modify the state of the iterator.
	Value() interface{}

	// Key returns the current element's key.
	// Does not modify the state of the iterator.
	Key() interface{}

	// Begin resets the iterator to its initial state (one-before-first)
	// Call Next() to fetch the first element if any.
	Begin()

	// First moves the iterator to the first element and returns true if there was a first element in the container.
	// If First() returns true, then first element's key and value can be retrieved by Key() and Value().
	// Modifies the state of the iterator.
	First() bool
}

IteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.

type JSONDeserializer

type JSONDeserializer interface {
	// FromJSON populates containers's elements from the input JSON representation.
	FromJSON([]byte) error
}

JSONDeserializer provides JSON deserialization

type JSONSerializer

type JSONSerializer interface {
	// ToJSON outputs the JSON representation of containers's elements.
	ToJSON() ([]byte, error)
}

JSONSerializer provides JSON serialization

type List

type List interface {
	Get(index int) (interface{}, bool)
	Remove(index int)
	Add(values ...interface{})
	Contains(values ...interface{}) bool
	Sort(comparator Comparator)
	Swap(index1, index2 int)
	Insert(index int, values ...interface{})
	Set(index int, value interface{})

	Container
}

List interface that all lists implement

type Map

type Map interface {
	Put(key interface{}, value interface{})
	Get(key interface{}) (value interface{}, found bool)
	Remove(key interface{})
	Keys() []interface{}

	Container
}

Map interface that all maps implement

type ReverseIteratorWithIndex

type ReverseIteratorWithIndex interface {
	// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
	// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	Prev() bool

	// End moves the iterator past the last element (one-past-the-end).
	// Call Prev() to fetch the last element if any.
	End()

	// Last moves the iterator to the last element and returns true if there was a last element in the container.
	// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
	// Modifies the state of the iterator.
	Last() bool

	IteratorWithIndex
}

ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.

Essentially it is the same as IteratorWithIndex, but provides additional:

Prev() function to enable traversal in reverse

Last() function to move the iterator to the last element.

End() function to move the iterator past the last element (one-past-the-end).

type ReverseIteratorWithKey

type ReverseIteratorWithKey interface {
	// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
	// If Prev() returns true, then previous element's key and value can be retrieved by Key() and Value().
	// Modifies the state of the iterator.
	Prev() bool

	// End moves the iterator past the last element (one-past-the-end).
	// Call Prev() to fetch the last element if any.
	End()

	// Last moves the iterator to the last element and returns true if there was a last element in the container.
	// If Last() returns true, then last element's key and value can be retrieved by Key() and Value().
	// Modifies the state of the iterator.
	Last() bool

	IteratorWithKey
}

ReverseIteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.

Essentially it is the same as IteratorWithKey, but provides additional:

Prev() function to enable traversal in reverse

Last() function to move the iterator to the last element.

type Set

type Set interface {
	Add(elements ...interface{})
	Remove(elements ...interface{})
	Contains(elements ...interface{}) bool

	Container
}

Set interface that all sets implement

type Stack

type Stack interface {
	Push(value interface{})
	Pop() (value interface{}, ok bool)
	Peek() (value interface{}, ok bool)

	Container
}

Stack interface that all stacks implement

type Tree

type Tree interface {
	Container
}

Tree interface that all trees implement

Directories

Path Synopsis
Package arraylist implements the array list.
Package arraylist implements the array list.
Package arraystack implements a stack backed by array list.
Package arraystack implements a stack backed by array list.
Package avltree implements an AVL balanced binary tree.
Package avltree implements an AVL balanced binary tree.
Package binaryheap implements a binary heap backed by array list.
Package binaryheap implements a binary heap backed by array list.
Package btree implements a B tree.
Package btree implements a B tree.
Package doublylinkedlist implements the doubly-linked list.
Package doublylinkedlist implements the doubly-linked list.
examples
Package hashbidimap implements a bidirectional map backed by two hashmaps.
Package hashbidimap implements a bidirectional map backed by two hashmaps.
Package hashmap implements a map backed by a hash table.
Package hashmap implements a map backed by a hash table.
Package hashset implements a set backed by a hash table.
Package hashset implements a set backed by a hash table.
Package linkedhashmap is a map that preserves insertion-order.
Package linkedhashmap is a map that preserves insertion-order.
Package linkedhashset is a set that preserves insertion-order.
Package linkedhashset is a set that preserves insertion-order.
Package linkedliststack implements a stack backed by a singly-linked list.
Package linkedliststack implements a stack backed by a singly-linked list.
Package redblacktree implements a red-black tree.
Package redblacktree implements a red-black tree.
Package singlylinkedlist implements the singly-linked list.
Package singlylinkedlist implements the singly-linked list.
Package treebidimap implements a bidirectional map backed by two red-black tree.
Package treebidimap implements a bidirectional map backed by two red-black tree.
Package treemap implements a map backed by red-black tree.
Package treemap implements a map backed by red-black tree.
Package treeset implements a tree backed by a red-black tree.
Package treeset implements a tree backed by a red-black tree.
Package uuid provides implementation of Universally Unique Identifier (UUID).
Package uuid provides implementation of Universally Unique Identifier (UUID).

Jump to

Keyboard shortcuts

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