dsGo

package module
v0.0.0-...-ffec269 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 0 Imported by: 0

README

dsGo

Data structures impletioned with Go

base or safe?

For each data structure, we give a base version, which will not be thread safe.

If you want a safe one, just make a simple wrapper of the base one, for example, let's have a look at the wrapper of set.

import (
	"sync"
 
	ds "github.com/zrcoder/dsGo"
	base "github.com/zrcoder/dsGo/set"
)
 
type Set struct {
	sync.RWMutex
	inner base.Set
}
 
func New() *Set {
	return &Set{inner: base.New()}
}
 
func NewWithCapacity(c int) *Set {
	return &Set{inner: base.NewWithCapacity(c)}
}
 
func (s *Set) Add(item ds.Any) {
	s.Lock()
	s.inner.Add(item)
	s.Unlock()
}
 
func (s *Set) Delete(item ds.Any) {
	s.Lock()
	s.inner.Delete(item)
	s.Unlock()
}
 
func (s *Set) Has(item ds.Any) bool {
	s.RLock()
	exist := s.inner.Has(item)
	s.RUnlock()
	return exist
}
 
func (s *Set) Size() int {
	s.RLock()
	length := s.inner.Size()
	s.RUnlock()
	return length
}
 
func (s *Set) AllItems() []ds.Any {
	s.RLock()
	items := s.inner.AllItems()
	s.RUnlock()
	return items
}

func (s *Set) Range(f func(item ds.Any) bool)  {
	s.Lock()
	s.inner.Range(f)
	s.Unlock()
}

the base version usually performances better than the safe one
and the safe version usually used for concurrent scenes

our data structures

queue

A queue gives you a FIFO or first-in firs-out order.

stack

A stack gives you a LIFO or last-in first-out order.

heap

A heap with APIs easy to use, different from container/heap in standard lib

set

A set can store unique values, without any particular order.

bit set

A bit set is a fixed-size sequence of n bits.

unionfind

union find

ring buffer

Also known as a circular buffer.
data structures in standard library

sync.Map

A thread safe hash map

container/list.List

A doubly linked list

container/heap

A heap is a tree with the property that each node is the minimum-valued node in its subtree.

container/ring.Ring

A ring is an element of a circular list, or ring.
references

swift-algorithm-club in GitHub

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any

type Any interface{}

type Empty

type Empty struct{}

Directories

Path Synopsis
BitSet is a fixed-size sequence of n bits.
BitSet is a fixed-size sequence of n bits.
Package list implements a doubly linked list.
Package list implements a doubly linked list.
A queue gives you a FIFO or first-in firs-out order.
A queue gives you a FIFO or first-in firs-out order.
Package ring implements operations on circular lists.
Package ring implements operations on circular lists.
A set can store unique values, without any particular order.
A set can store unique values, without any particular order.
A stack gives you a LIFO or last-in first-out order.
A stack gives you a LIFO or last-in first-out order.

Jump to

Keyboard shortcuts

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