containers

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 1 Imported by: 0

README

containers

Static Badge GitHub License

base data structure and algorithm implement by go genericity

install

$ go get -u github.com/246859/containers@latest

usage

Import the package and use the structure. Here is a simple example about ArrayList.

import (
    "fmt"
    "github.com/246859/containers/lists"
)

func main() {
    arrayList := lists.NewArrayList[int](10)
    arrayList.Add(1)
    arrayList.Add(2)
    arrayList.Add(3, 4, 5)

    ele, has := arrayList.Get(0)
    if !has {
       panic("element not found")
    }
    fmt.Println(ele)
}

Here is a simple example about PriorityQueue.

package main

import (
    "cmp"
    "fmt"
    "github.com/246859/containers/queues"
)

func main() {
    priorityQueue := queues.NewPriorityQueue[int](20, cmp.Compare[int])
    priorityQueue.Push(2)
    priorityQueue.Push(1)
    priorityQueue.Push([]int{0, 6, 3, 2, 5}...)

    top, has := priorityQueue.Peek()
    fmt.Println(top, has)
}

know more information to see ToDo List.

contribute

  1. fork this repository
  2. checkout your feature branch
  3. commit changes, know more about commit-messages-guide
  4. create a pull request to this repository

Documentation

Index

Constants

View Source
const (
	LessThan = -1 + iota
	EqualTo
	GreaterThan
)

Variables

This section is empty.

Functions

func OrderedCompare

func OrderedCompare[T cmp.Ordered](a, b T) int

func OrderedEqual

func OrderedEqual[T cmp.Ordered](a, b T) bool

func OrderedLess

func OrderedLess[T cmp.Ordered](a, b T) bool

Types

type Compare

type Compare[T any] func(a, b T) int

Compare compares e with self, if a is less than b, returns LessThan if a is greater than b, returns GreaterThan if a is equal to b, returns EqualTo

type Container

type Container[T any] interface {
	// Values returns all the values of the container
	Values() []T
	// Size returns the size of the container elements
	Size() int
	// Clear clears all the elements of the container
	Clear()
	// String returns the string representation of the container
	String() string
}

Container is the base interface represent of all data structures

type Equal

type Equal[T any] func(a, b T) bool

Equal checks if a is equals to b

type IndexIterable

type IndexIterable[V any] interface {
	Iterator() IndexIterator[V]
}

IndexIterable is the base interface of all data structures that can be iterated over by slice index

type IndexIterator

type IndexIterator[V any] interface {
	Iterator[int, V]
}

IndexIterator is base interface of iterator which use slice index

type Iterable

type Iterable[K any, V any] interface {
	Iterator() Iterator[K, V]
}

Iterable is the base interface of all data structures that can be iterated over

type Iterator

type Iterator[K any, V any] interface {
	// Rewind set the iterator index to the initial state
	Rewind()

	// Reverse changes the iterator orientation
	Reverse()

	// Valid returns true if the iterator has not reaches the end
	Valid() bool

	// Next move the index to the next element
	Next()

	// Index returns iterator current index
	Index() K

	// Value returns value of iterator current index
	Value() V

	// SeekTo move current index to the given index, returns true if success, or returns false
	SeekTo(index K) bool
}

Iterator is the base interface of the all data structures iterators iterator is readonly, represents of data snapshot at a certain moment

type Less

type Less[T any] func(a, b T) bool

Less check if a is less than b

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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