Documentation
¶
Overview ¶
Package go_persistent_ds implements several fully persistent data structures.
Available data structures are:
- Map
- Slice
- DoubleLinkedList
All structures are base on FatNodes.
Note that every structure can perform total of 2^65-1 modifications, and will panic on attempt to modify it for 2^65 time. If you need to continue editing structure, the good idea is to use appropriate method to dump structure for special version.
Index ¶
- Variables
- type DoubleLinkedList
- func (l *DoubleLinkedList[T]) Get(version uint64, index int) (T, error)
- func (l *DoubleLinkedList[T]) Len(version uint64) (int, error)
- func (l *DoubleLinkedList[T]) PushBack(version uint64, value T) (uint64, error)
- func (l *DoubleLinkedList[T]) PushFront(version uint64, value T) (uint64, error)
- func (l *DoubleLinkedList[T]) Remove(version uint64, index int) (uint64, error)
- func (l *DoubleLinkedList[T]) ToGoList(version uint64) (*list.List, error)
- func (l *DoubleLinkedList[T]) Update(version uint64, index int, value T) (uint64, error)
- type Map
- func (m *Map[TKey, TVal]) Delete(forVersion uint64, key TKey) (uint64, error)
- func (m *Map[TKey, TVal]) Get(version uint64, key TKey) (TVal, error)
- func (m *Map[TKey, TVal]) Len(forVersion uint64) (int, error)
- func (m *Map[TKey, TVal]) Set(forVersion uint64, key TKey, val TVal) (uint64, error)
- func (m *Map[TKey, TVal]) ToGoMap(version uint64) (map[TKey]TVal, error)
- type Slice
- func (s *Slice[TVal]) Append(version uint64, val TVal) (uint64, error)
- func (s *Slice[TVal]) Get(version uint64, index int) (TVal, error)
- func (s *Slice[TVal]) Len(version uint64) (int, error)
- func (s *Slice[TVal]) Range(forVersion uint64, startIndex, endIndex int) (uint64, error)
- func (s *Slice[TVal]) Set(forVersion uint64, index int, val TVal) (uint64, error)
- func (s *Slice[TVal]) ToGoSlice(forVersion uint64) ([]TVal, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMapInitialize is returned then there is a problem in creating new tree. ErrMapInitialize = errors.New("failed to init Map because version tree is damaged") // ErrNotFound is returned then value by key or for version is not found. ErrNotFound = errors.New("not found") )
var ( // ErrSliceInitialize is returned then there is a problem in creating new slice because of tree problems. ErrSliceInitialize = errors.New("failed to init Slice because version tree is damaged") // ErrIndexOutOfRange is returned then index is greater than len array for the version. ErrIndexOutOfRange = errors.New("array index out of range") )
var ErrListIndexOutOfRange = errors.New("index out of range")
Functions ¶
This section is empty.
Types ¶
type DoubleLinkedList ¶
type DoubleLinkedList[T any] struct { // contains filtered or unexported fields }
DoubleLinkedList is a persistent implementation of double linked list. While working with list you can add to the start and to the end, access elements by index and modify. Each change of the list creates new version. Versions can be retrieved by their number. Also, there is an opportunity to convert this list into Go list using ToGoList.
DoubleLinkedList can perform total of 2^65-1 modifications, and will panic on attempt to modify it for 2^65 time. If you need to continue editing DoubleLinkedList, the good idea is to use ToGoList method to dump list for special version.
Note that DoubleLinkedList implementation is not thread safe.
func NewDoubleLinkedList ¶
func NewDoubleLinkedList[T any]() (*DoubleLinkedList[T], uint64)
NewDoubleLinkedList creates new empty DoubleLinkedList.
func NewDoubleLinkedListWithAnyValues ¶
func NewDoubleLinkedListWithAnyValues() (*DoubleLinkedList[any], uint64)
NewDoubleLinkedListWithAnyValues creates DoubleLinkedList that can store values of any type.
func (*DoubleLinkedList[T]) Get ¶
func (l *DoubleLinkedList[T]) Get(version uint64, index int) (T, error)
Get retrieves value from the specified DoubleLinkedList version by index.
Complexity: O(n * log(m)), where n - DoubleLinkedList size and m - is number of changes in FatNode.
func (*DoubleLinkedList[T]) Len ¶
func (l *DoubleLinkedList[T]) Len(version uint64) (int, error)
Len returns DoubleLinkedList size.
Complexity: O(1).
func (*DoubleLinkedList[T]) PushBack ¶
func (l *DoubleLinkedList[T]) PushBack(version uint64, value T) (uint64, error)
PushBack adds new element to the tail of the DoubleLinkedList. Returns list's new version. Note: head->[1][2][3]<-tail.
Complexity: O(1).
func (*DoubleLinkedList[T]) PushFront ¶
func (l *DoubleLinkedList[T]) PushFront(version uint64, value T) (uint64, error)
PushFront adds new element to the head of the DoubleLinkedList. Returns list's new version. Note: head->[1][2][3]<-tail.
Complexity: O(n).
func (*DoubleLinkedList[T]) Remove ¶
func (l *DoubleLinkedList[T]) Remove(version uint64, index int) (uint64, error)
Remove removes element from specified version of DoubleLinkedList by index and returns new list's version. By removal, we mean delete of connection between specified element and his "neighbours".
Complexity: O(n * log(m)), where n - DoubleLinkedList size and m - is number of changes in FatNode.
func (*DoubleLinkedList[T]) ToGoList ¶
func (l *DoubleLinkedList[T]) ToGoList(version uint64) (*list.List, error)
ToGoList converts DoubleLinkedList into Go List.
Complexity: O(Get) * n, where n - DoubleLinkedList size.
func (*DoubleLinkedList[T]) Update ¶
func (l *DoubleLinkedList[T]) Update(version uint64, index int, value T) (uint64, error)
Update updates element of specified DoubleLinkedList version by index. Returns list's new version.
Complexity: O(n * log(m)), where n - DoubleLinkedList size and m - is number of changes in FatNode.
type Map ¶
type Map[TKey comparable, TVal any] struct { // contains filtered or unexported fields }
Map is a persistent implementation of go map. While working with map you can access and/or modify each previous version. Note that modifying version creates new one.
Map can perform total of 2^65-1 modifications, and will panic on attempt to modify it for 2^65 time. If you need to continue editing Map, the good idea is to use ToGoMap method to dump Map for special version.
Note that Map is not thread safe.
func NewMap ¶
func NewMap[TKey comparable, TVal any]() (*Map[TKey, TVal], uint64)
NewMap creates empty Map.
func NewMapWithAnyValues ¶
func NewMapWithAnyValues[TKey comparable]() (*Map[TKey, any], uint64)
NewMapWithAnyValues creates a Map, that can store values of any type.
func NewMapWithCapacity ¶
func NewMapWithCapacity[TKey comparable, TVal any](capacity int) (*Map[TKey, TVal], uint64)
NewMapWithCapacity creates empty Map with given capacity.
func (*Map[TKey, TVal]) Delete ¶
Delete the value from Map for given key for given version.
Complexity: same as for Get.
func (*Map[TKey, TVal]) Get ¶
Get returns a pair of value and error for provided version and key. If error is nil then the value for such key and version exists.
Complexity: O(log(m) * k) there:
- m - amount of modifications for current key from map creation.
- k - amount of modifications visible from current branch.
type Slice ¶
type Slice[TVal any] struct { // contains filtered or unexported fields }
Slice is a persistent implementation of go slice. While working with slice you can access and/or modify each previous version. Note that modifying version creates new one.
Slice can perform total of 2^65-1 modifications, and will panic on attempt to modify it for 2^65 time. If you need to continue editing Slice, the good idea is to use ToGoSlice method to dump Slice for special version.
Note that Slice is not thread safe.
func NewSliceWithAnyValues ¶
NewSliceWithAnyValues creates a Slice, that can store values of any type.
func NewSliceWithCapacity ¶
NewSliceWithCapacity creates empty Slice with given capacity.
func (*Slice[TVal]) Append ¶
Append adds the value to the end of Slice of given version.
Complexity: O(1).
func (*Slice[TVal]) Get ¶
Get returns a pair of value and error for provided version and index. If error is nil then value for such index and version exists.
Complexity: O(log(m) * k) there:
- m - amount of modifications for value by the index from slice creation.
- k - amount of modifications visible from current branch.
func (*Slice[TVal]) Range ¶
Range takes the range of Slice for given version from startIndex (inclusive) to endIndex (not inclusive).
Complexity: O(1).