copyonwrite

package
v0.0.0-...-c2c6062 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package copyonwrite providers helpers for modifying slices in Copy-on-Write way.

Index

Constants

This section is empty.

Variables

View Source
var Deletion = deletion{}

Deletion is special return value for Modifier to imply deletion.

Functions

This section is empty.

Types

type Modifier

type Modifier func(any) any

Modifier takes a pointer to an existing COW object and returns:

  • special DeleteMe, if this object should be deleted from Slice;
  • the same object, if there are no modifications;
  • new object, if there were modifications. If working with SortedSlice, the new object must have the same sorting key.

type Slice

type Slice interface {
	Len() int
	At(index int) any
	Append(v any) Slice
	// CloneShallow returns new slice of the given capacity with elements copied
	// from [:length] of this slice.
	CloneShallow(length, capacity int) Slice
}

Slice abstracts out copy-on-write slices of pointers to objects.

func Update

func Update(in Slice, modifier Modifier, toAdd Slice) (Slice, bool)

Update modifies existing elements and adds new ones to Slice.

If given Slice implements SortedSlice, assumes and preserves its sorted order.

COWModifier, if not nil, is called on each existing object to modify or possible delete it.

Sorts toAdd slice if using SortedSlice.

Returns (new slice, true) if it was modified. Returns (old slice, false) otherwise.

type SortedSlice

type SortedSlice interface {
	Slice
	sort.Interface
	// LessElements returns true if element `a` must be before `b`.
	//
	// Unlike sort.Interface's Less, this compares arbitrary elements.
	//
	// Why not use sort.Interface's Less?
	// Because during merge phase of (newly created, updated) slices, it's
	// necessary to compare elements from different slices. OK, OK, it's possible
	// to move both elements to one slice and re-use Less. For example, a temp
	// slice of 2 elements can be always allocated for this very purpose, or one
	// can smartly re-use resulting slice for this. However, either way it's
	// slower and more complicated than requiring boilerplate in each SortedSlice
	// type, which can be re-used in Less() implementation.
	LessElements(a, b any) bool
}

SortedSlice is a sorted Slice.

Jump to

Keyboard shortcuts

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