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 ¶
const Base62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Base62 is the symbols for a Base62 number system.
const Decimal = "0123456789"
Decimal is the symbols for a decimal number system.
const Hex = "0123456789abcdef"
Hex is the symbols for a hexadecimal number system.
Variables ¶
var ErrBetweenOrderWrong = errors.New("b cannot be less than a")
ErrBetweenOrderWrong is returned when b < a for a call to Between.
var ErrSymbolsMustBeUnique = errors.New("symbols must be unique")
ErrSymbolsMustBeUnique is returned when duplicate symbols are provided for a numeric system.
Functions ¶
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.