goStringTable

package module
v0.0.0-...-9f6b208 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2018 License: MIT Imports: 7 Imported by: 0

README

Build Status GoDoc

goStringTable

A thread-safe symbol table (string table) for Go

Description

A string table turns strings into numbers, and back again. Popularised by LISP and Scheme, a string table is an efficient (and sometimes quick) way to store a lot of strings, when many of them are expected to be identical. e.g. when parsing a program, function and variable names are usually put into a symbol table.

It works like a hashmap, but it is lock-free to read from multiple threads(unlike Go hashmaps). Unlike a normal map, a string table works both ways, which is why it is restricted to strings.

Speed

Going from a symbol(a number) to string is an array lookup, so almost instant.

Going from a string to a number requires a lookup in a Patricia (radix) tree, which means the time should be roughly log(n), where n is the number of stored strings.

Adding a string is very slow, for various reasons, so it is best used in situations where the reads will vastly outnumber the writes.

Use

import "github.com/donomii/goStringTable"
s := goStringTable.New()
n := s.LookupOrCreate("Hello World")
fmt.Println(s.GetString(n))

or lookup a string without creating it

sym, err := s.Lookup("Hello World")

Similar projects

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SymTable

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

func New

func New() *SymTable

func (*SymTable) GetString

func (s *SymTable) GetString(index int) string

func (*SymTable) LockMe

func (s *SymTable) LockMe()

func (*SymTable) Lookup

func (s *SymTable) Lookup(aStr string) (int, error)

func (*SymTable) LookupOrCreate

func (s *SymTable) LookupOrCreate(aStr string) int

func (*SymTable) UnlockMe

func (s *SymTable) UnlockMe()

Jump to

Keyboard shortcuts

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