order

package
v0.0.46 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

order includes helpers to allow users to manually order lists of things efficiently. Allowing users to sort a list of things manually is harder than it seems. You could increment the order of everything after the item that was moved. But then how many items will you have to modify when something moves? A common tactic is to use floating point numbers. For instance to add an item x after an item y you could do y.pos = x.pos + x.pos / 2. The issue is that you will run out of precision at some point. Another less commonly used tactics is strings. They can be lexicographical sorted, and effectively have infinite precision. Someone on Stackoverflow designed an elegant algorithm that can be used for this: https://stackoverflow.com/questions/38923376 Someone else wrote a library that fleshed out the idea more: https://github.com/fasiha/mudderjs What follows is not my invention, merely a translation to Go.

Index

Constants

View Source
const Base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

Base62 is the symbols for a Base62 number system.

View Source
const Decimal = "0123456789"

Decimal is the symbols for a decimal number system.

View Source
const Hex = "0123456789abcdef"

Hex is the symbols for a hexadecimal number system.

Variables

View Source
var ErrBetweenOrderWrong = errors.New("b cannot be less than a")

ErrBetweenOrderWrong is returned when b < a for a call to Between.

View Source
var ErrSymbolsMustBeUnique = errors.New("symbols must be unique")

ErrSymbolsMustBeUnique is returned when duplicate symbols are provided for a numeric system.

Functions

func Between

func Between(previous string, next string) (string, error)

Between gets the order for an item between two items on the list.

func End

func End(previous string) (string, error)

End gets the order for an item at the end of the list.

func Initial

func Initial() (string, error)

Initial gets the initial order for an item in a list. Basically it is the midpoint of the address space.

func Start

func Start(next string) (string, error)

Start gets the order for an item at the start of a list.

Types

type SymbolTable

type SymbolTable struct {
	// contains filtered or unexported fields
}

SymbolTable is the primary underlying data structure for this algorithm. Effectively it lets you define a numeric system. You can then do some conversions with it: positive integers <-> digits <-> strings. The README for mudder has some fun examples of numeric systems. There are some simplification made here: - It only supports building a symbol table out of a string. - The base is not configurable (it is the number of symbols). - The symbols must be unique (this means the symbol table always has the prefix property).

func MakeSymbolTable

func MakeSymbolTable(symbols string) (SymbolTable, error)

MakeSymbolTable builds a symbol table from a string of symbols. Each character in the string will become a symbol.

func (SymbolTable) Between

func (table SymbolTable) Between(start string, end string, number int, divisions int, placesToKeep int) ([]string, error)

Between returns number strings between start and end. These strings should be evenly spaced in the address space between start and end. For example if you request 3 numbers between 0 and 10 in base10 you would get 3, 4, and 7. All parameters to this are optional: - start defaults to the first symbol in the symbol table. - end defaults to the last symbol in the symbol table repeated a number of times. - number defaults to 1. - divisions defaults to number + 1. This only works ascending so start must be > end.

func (SymbolTable) NumberToString

func (table SymbolTable) NumberToString(number int) (string, error)

NumberToString converts a number to a string.

func (SymbolTable) StringToNumber

func (table SymbolTable) StringToNumber(str string) (int, error)

StringToNumber converts a string to a number.

Jump to

Keyboard shortcuts

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