collection

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanCompare

func CanCompare[E any](e1 E, e2 E) bool

func Compare

func Compare[E any](e1 E, e2 E) int

Compare First, use comparator for comparison. If not, use Compare again. If not, use Golang's default sorting rules

func Equal

func Equal[E any](e1 E, e2 E) bool

func QuickSort

func QuickSort[T any](slice []T, compareFunc func(o1, o2 T) int)

QuickSort quick sort

func Sort

func Sort[T any](slice []T, compareFunc ...CompareFunc[T])

Types

type Comparable

type Comparable[E any] interface {
	// CompareTo define compare rule
	CompareTo(element E) int
}

Comparable is a function type used to define a comparison interface in GoKit collection.

type CompareFunc

type CompareFunc[T any] func(o1 T, o2 T) int

CompareFunc is a function type used to define a comparison function in GoKit collection. It imposes a total ordering on a collection of objects. Comparators can be passed to sorting methods to provide precise control over the sort order. Comparators can also be used to control the order of certain data structures, such as sorted sets or sorted maps, or to provide an ordering for collections of objects that don't have a natural ordering.

The comparison function takes two arguments, 'o1' and 'o2', of type 'T' (a comparable type), and returns an integer value that represents the comparison result: - A negative value if 'o1' is less than 'o2'. - Zero if 'o1' is equal to 'o2'. - A positive value if 'o1' is greater than 'o2'.

Example usage:
func CompareInt(a, b int) int {
	if a < b {
		return -1
	} else if a > b {
		return 1
	}
		return 0
	}

var compareFn Compare[int] = CompareInt

Using the comparison function in a sort operation: someInts := []int{3, 1, 4, 1, 5, 9, 2, 6, 5} sort.Slice(someInts, func(i, j int) bool { return compareFn(someInts[i], someInts[j]) < 0 })

type Iterable

type Iterable interface {
	// Iterator returns an iterator for the collection.
	Iterator() Iterator
}

Iterable represents a collection that can be iterated over.

type Iterator

type Iterator interface {
	// Next returns true if there are more elements to iterate, or false otherwise.
	Next() bool

	// Value returns the current value of the iterator.
	Value() interface{}
}

Iterator represents an iterator over a collection.

type JSONDeserializer added in v0.1.2

type JSONDeserializer interface {
	// FromJSON populates collection's elements from the input JSON representation.
	FromJSON([]byte) error
	// UnmarshalJSON @implements json.Unmarshaler
	UnmarshalJSON([]byte) error
}

JSONDeserializer provides JSON deserialization

type JSONSerializer added in v0.1.2

type JSONSerializer interface {
	// ToJSON outputs the JSON representation of collection's elements.
	ToJSON() ([]byte, error)
	// MarshalJSON @implements json.Marshaler
	MarshalJSON() ([]byte, error)
}

JSONSerializer provides JSON serialization

type ToString

type ToString interface {
	ToString() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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