rope

package
v0.1.33 Latest Latest
Warning

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

Go to latest
Published: May 11, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// RootId is the zero ID for all ropes.
	RootId = Id(0)
)

Functions

func JSLength

func JSLength(s string) (count int)

JSLength returns the JS length of the given string (which in Go, is always UTF-8). We don't care about raw bytes because we probably only have this string because we got it _from_ JSON, which is UTF-8.

Types

type Id

type Id int

Id is the opaque ID given when you add to a Rope.

type Info

type Info[T any] struct {
	Id, Next, Prev Id
	Length         int
	Data           T
}

Info is a holder for info looked up in a Rope.

type Rope

type Rope[T any] interface {
	// Returns the total sum of the parts of the rope. O(1).
	Len() int

	// Returns the number of parts here. O(1).
	Count() int

	// Finds the position of the given Id.
	// This lookup costs ~O(logn).
	Find(id Id) int

	// Finds info on the given Id.
	// This lookup costs O(1).
	Info(id Id) Info[T]

	// Finds the Id/info at the position in the Rope.
	// This costs ~O(logn).
	// Either stops before or skips after zero-length content based on biasAfter.
	// e.g., with 0/false, this will aways return Id=0 (the root).
	ByPosition(position int, biasAfter bool) (id Id, offset int)

	// Compare the two Id in this Rope.
	// Costs ~O(logn).
	Compare(a, b Id) (cmp int, ok bool)

	// Is the first Id in this Rope before the other. For sorting.
	// Costs ~O(logn).
	Before(a, b Id) bool

	// Iter reads IDs from after the given Id.
	Iter(afterId Id) iter.Seq2[Id, T]

	// Insert data after the prior Id.
	// Length must be zero or positive.
	// Costs ~O(logn).
	InsertAfter(id Id, length int, data T) Id

	// DeleteTo deletes after the given ID until the target Id.
	// Pass zero/root for all content after.
	// Costs ~O(logn+m), where m is the number of nodes being deleted.
	DeleteTo(afterId, untilId Id)
}

Rope is a skip list. It supports zero-length entries. It is not goroutine-safe.

func New

func New[T any]() Rope[T]

New builds a new Rope[T].

Jump to

Keyboard shortcuts

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