omap

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package omap provides an ordered map implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(cfg PoolConfig)

Init sets up a non-default pooling and allocation strategy. Should be run before serialization is done.

Types

type Buffer

type Buffer struct {

	// Buf is the current chunk that can be used for serialization.
	Buf []byte
	// contains filtered or unexported fields
}

Buffer is a buffer optimized for serialization without extra copying.

func (*Buffer) AppendByte

func (b *Buffer) AppendByte(data byte)

AppendByte appends a single byte to buffer.

func (*Buffer) AppendBytes

func (b *Buffer) AppendBytes(data []byte)

AppendBytes appends a byte slice to buffer.

func (*Buffer) AppendString

func (b *Buffer) AppendString(data string)

AppendString appends a string to buffer.

func (*Buffer) BuildBytes

func (b *Buffer) BuildBytes(reuse ...[]byte) []byte

BuildBytes creates a single byte slice with all the contents of the buffer. Data is copied if it does not fit in a single chunk. You can optionally provide one byte slice as argument that it will try to reuse.

func (*Buffer) DumpTo

func (b *Buffer) DumpTo(w io.Writer) (written int, err error)

DumpTo outputs the contents of a buffer to a writer and resets the buffer.

func (*Buffer) EnsureSpace

func (b *Buffer) EnsureSpace(s int)

EnsureSpace makes sure that the current chunk contains at least s free bytes, possibly creating a new chunk.

func (*Buffer) ReadCloser

func (b *Buffer) ReadCloser() io.ReadCloser

ReadCloser creates an io.ReadCloser with all the contents of the buffer.

func (*Buffer) Size

func (b *Buffer) Size() int

Size computes the size of a buffer by adding sizes of every chunk.

type Flags

type Flags int

Flags describe various encoding options. The behavior may be actually implemented in the encoder, but Flags field in Writer is used to set and pass them around.

const (
	// NilMapAsEmpty encodes nil map as '{}' rather than 'null'.
	NilMapAsEmpty Flags = 1 << iota
	// NilSliceAsEmpty encodes nil slice as '[]' rather than 'null'.
	NilSliceAsEmpty
)

type InitOption

type InitOption[K comparable, V any] func(config *initConfig[K, V])

InitOption is an option for initializing an OrderedMap.

func WithCapacity

func WithCapacity[K comparable, V any](capacity int) InitOption[K, V]

WithCapacity allows giving a capacity hint for the map, akin to the standard make(map[K]V, capacity).

func WithInitialData

func WithInitialData[K comparable, V any](initialData ...Pair[K, V]) InitOption[K, V]

WithInitialData allows passing in initial data for the map.

type KeyNotFoundError

type KeyNotFoundError[K comparable] struct {
	MissingKey K
}

KeyNotFoundError may be returned by functions in this package when they're called with keys that are not present in the map.

func (*KeyNotFoundError[K]) Error

func (e *KeyNotFoundError[K]) Error() string

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap is a generic ordered map.

func New

func New[K comparable, V any](options ...any) *OrderedMap[K, V]

New creates a new OrderedMap. options can either be one or several InitOption[K, V], or a single integer, which is then interpreted as a capacity hint, à la make(map[K]V, capacity).

func (*OrderedMap[K, V]) AddPairs

func (om *OrderedMap[K, V]) AddPairs(pairs ...Pair[K, V])

AddPairs allows setting multiple pairs at a time. It's equivalent to calling Set on each pair sequentially.

func (*OrderedMap[K, V]) Delete

func (om *OrderedMap[K, V]) Delete(key K) (val V, present bool)

Delete removes the key-value pair, and returns what `Get` would have returned on that key prior to the call to `Delete`.

func (*OrderedMap[K, V]) Get

func (om *OrderedMap[K, V]) Get(key K) (val V, present bool)

Get looks for the given key, and returns the value associated with it, or V's nil value if not found. The boolean it returns says whether the key is present in the map.

func (*OrderedMap[K, V]) GetAndMoveToBack

func (om *OrderedMap[K, V]) GetAndMoveToBack(key K) (val V, err error)

GetAndMoveToBack combines Get and MoveToBack in the same call. If an error is returned, it will be a KeyNotFoundError.

func (*OrderedMap[K, V]) GetAndMoveToFront

func (om *OrderedMap[K, V]) GetAndMoveToFront(key K) (val V, err error)

GetAndMoveToFront combines Get and MoveToFront in the same call. If an error is returned, it will be a KeyNotFoundError.

func (*OrderedMap[K, V]) GetPair

func (om *OrderedMap[K, V]) GetPair(key K) *Pair[K, V]

GetPair looks for the given key, and returns the pair associated with it, or nil if not found. The Pair struct can then be used to iterate over the ordered map from that point, either forward or backward.

func (*OrderedMap[K, V]) Len

func (om *OrderedMap[K, V]) Len() int

Len returns the length of the ordered map.

func (*OrderedMap[K, V]) Load

func (om *OrderedMap[K, V]) Load(key K) (V, bool)

Load is an alias for Get, mostly to present an API similar to `sync.Map`'s.

func (*OrderedMap[K, V]) MarshalJSON

func (om *OrderedMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*OrderedMap[K, V]) MoveAfter

func (om *OrderedMap[K, V]) MoveAfter(key, markKey K) error

MoveAfter moves the value associated with key to its new position after the one associated with markKey. Returns an error iff key or markKey are not present in the map. If an error is returned, it will be a KeyNotFoundError.

func (*OrderedMap[K, V]) MoveBefore

func (om *OrderedMap[K, V]) MoveBefore(key, markKey K) error

MoveBefore moves the value associated with key to its new position before the one associated with markKey. Returns an error iff key or markKey are not present in the map. If an error is returned, it will be a KeyNotFoundError.

func (*OrderedMap[K, V]) MoveToBack

func (om *OrderedMap[K, V]) MoveToBack(key K) error

MoveToBack moves the value associated with key to the back of the ordered map, i.e. makes it the newest pair in the map. Returns an error iff key is not present in the map. If an error is returned, it will be a KeyNotFoundError.

func (*OrderedMap[K, V]) MoveToFront

func (om *OrderedMap[K, V]) MoveToFront(key K) error

MoveToFront moves the value associated with key to the front of the ordered map, i.e. makes it the oldest pair in the map. Returns an error iff key is not present in the map. If an error is returned, it will be a KeyNotFoundError.

func (*OrderedMap[K, V]) Newest

func (om *OrderedMap[K, V]) Newest() *Pair[K, V]

Newest returns a pointer to the newest pair. It's meant to be used to iterate on the ordered map's pairs from the newest to the oldest, e.g.: for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) }

func (*OrderedMap[K, V]) Oldest

func (om *OrderedMap[K, V]) Oldest() *Pair[K, V]

Oldest returns a pointer to the oldest pair. It's meant to be used to iterate on the ordered map's pairs from the oldest to the newest, e.g.: for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) }

func (*OrderedMap[K, V]) Set

func (om *OrderedMap[K, V]) Set(key K, value V) (val V, present bool)

Set sets the key-value pair, and returns what `Get` would have returned on that key prior to the call to `Set`.

func (*OrderedMap[K, V]) Store

func (om *OrderedMap[K, V]) Store(key K, value V) (V, bool)

Store is an alias for Set, mostly to present an API similar to `sync.Map`'s.

func (*OrderedMap[K, V]) UnmarshalJSON

func (om *OrderedMap[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*OrderedMap[K, V]) Value

func (om *OrderedMap[K, V]) Value(key K) (val V)

Value returns the value associated with the given key or the zero value.

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
	// contains filtered or unexported fields
}

Pair is a generic pair.

func (*Pair[K, V]) Next

func (p *Pair[K, V]) Next() *Pair[K, V]

Next returns a pointer to the next pair.

func (*Pair[K, V]) Prev

func (p *Pair[K, V]) Prev() *Pair[K, V]

Prev returns a pointer to the previous pair.

type PoolConfig

type PoolConfig struct {
	StartSize  int // Minimum chunk size that is allocated.
	PooledSize int // Minimum chunk size that is reused, reusing chunks too small will result in overhead.
	MaxSize    int // Maximum chunk size that will be allocated.
}

PoolConfig contains configuration for the allocation and reuse strategy.

type Writer

type Writer struct {
	Flags Flags

	Error        error
	Buffer       Buffer
	NoEscapeHTML bool
}

Writer is a JSON writer.

func (*Writer) Base64Bytes

func (w *Writer) Base64Bytes(data []byte)

Base64Bytes appends data to the buffer after base64 encoding it

func (*Writer) Bool

func (w *Writer) Bool(v bool)

Bool appends a bool to the buffer.

func (*Writer) BuildBytes

func (w *Writer) BuildBytes(reuse ...[]byte) ([]byte, error)

BuildBytes returns writer data as a single byte slice. You can optionally provide one byte slice as argument that it will try to reuse.

func (*Writer) DumpTo

func (w *Writer) DumpTo(out io.Writer) (written int, err error)

DumpTo outputs the data to given io.Writer, resetting the buffer.

func (*Writer) Float32

func (w *Writer) Float32(n float32)

Float32 appends a float32 to the buffer.

func (*Writer) Float32Str

func (w *Writer) Float32Str(n float32)

Float32Str appends a float32 to the buffer as a quoted string.

func (*Writer) Float64

func (w *Writer) Float64(n float64)

Float64 appends a float64 to the buffer.

func (*Writer) Float64Str

func (w *Writer) Float64Str(n float64)

Float64Str appends a float64 to the buffer as a quoted string.

func (*Writer) Int

func (w *Writer) Int(n int)

Int appends an int to the buffer.

func (*Writer) Int8

func (w *Writer) Int8(n int8)

Int8 appends an int8 to the buffer.

func (*Writer) Int8Str

func (w *Writer) Int8Str(n int8)

Int8Str appends an int8 to the buffer as a quoted string.

func (*Writer) Int16

func (w *Writer) Int16(n int16)

Int16 appends an int16 to the buffer.

func (*Writer) Int16Str

func (w *Writer) Int16Str(n int16)

Int16Str appends an int16 to the buffer as a quoted string.

func (*Writer) Int32

func (w *Writer) Int32(n int32)

Int32 appends an int32 to the buffer.

func (*Writer) Int32Str

func (w *Writer) Int32Str(n int32)

Int32Str appends an int32 to the buffer as a quoted string.

func (*Writer) Int64

func (w *Writer) Int64(n int64)

Int64 appends an int64 to the buffer.

func (*Writer) Int64Str

func (w *Writer) Int64Str(n int64)

Int64Str appends an int64 to the buffer as a quoted string.

func (*Writer) IntStr

func (w *Writer) IntStr(n int)

IntStr appends an int to the buffer as a quoted string.

func (*Writer) Raw

func (w *Writer) Raw(data []byte, err error)

Raw appends raw binary data to the buffer or sets the error if it is given. Useful for calling with results of MarshalJSON-like functions.

func (*Writer) RawByte

func (w *Writer) RawByte(c byte)

RawByte appends raw binary data to the buffer.

func (*Writer) RawString

func (w *Writer) RawString(s string)

RawString appends raw binary data to the buffer.

func (*Writer) RawText

func (w *Writer) RawText(data []byte, err error)

RawText encloses raw binary data in quotes and appends in to the buffer. Useful for calling with results of MarshalText-like functions.

func (*Writer) ReadCloser

func (w *Writer) ReadCloser() (io.ReadCloser, error)

ReadCloser returns an io.ReadCloser that can be used to read the data. ReadCloser also resets the buffer.

func (*Writer) Size

func (w *Writer) Size() int

Size returns the size of the data that was written out.

func (*Writer) String

func (w *Writer) String(s string)

func (*Writer) Uint

func (w *Writer) Uint(n uint)

Uint appends an uint to the buffer.

func (*Writer) Uint8

func (w *Writer) Uint8(n uint8)

Uint8 appends an uint8 to the buffer.

func (*Writer) Uint8Str

func (w *Writer) Uint8Str(n uint8)

Uint8Str appends an uint8 to the buffer as a quoted string.

func (*Writer) Uint16

func (w *Writer) Uint16(n uint16)

Uint16 appends an uint16 to the buffer.

func (*Writer) Uint16Str

func (w *Writer) Uint16Str(n uint16)

Uint16Str appends an uint16 to the buffer as a quoted string.

func (*Writer) Uint32

func (w *Writer) Uint32(n uint32)

Uint32 appends an uint32 to the buffer.

func (*Writer) Uint32Str

func (w *Writer) Uint32Str(n uint32)

Uint32Str appends an uint32 to the buffer as a quoted string.

func (*Writer) Uint64

func (w *Writer) Uint64(n uint64)

Uint64 appends an uint64 to the buffer.

func (*Writer) Uint64Str

func (w *Writer) Uint64Str(n uint64)

Uint64Str appends an uint64 to the buffer as a quoted string.

func (*Writer) UintStr

func (w *Writer) UintStr(n uint)

UintStr appends an uint to the buffer as a quoted string.

func (*Writer) UintptrStr

func (w *Writer) UintptrStr(n uintptr)

UintptrStr appends an uintptr to the buffer as a quoted string.

Jump to

Keyboard shortcuts

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