Package intern

import "github.com/BurntSushi/intern"
Overview
Index

Overview ▾

Package intern provides a simple interface for interning strings. Strings are mapped to integers, which may be used to index a slice. A string interner is useful when any particular string key can be used many times (e.g., pairwise data corresponding to all combinations of some string identifiers).

func ExpandSquareTable

func ExpandSquareTable(slice interface{}, leastLen int) (interface{}, int)

ExpandSquareTable has a parametric type:

func ExpandSquareTable([]A, int) ([]A, int)

ExpandSquareTable takes any slice holding a square table of data (row-major) and expands the length of the table to at least the length provided. A new slice is returned (with data from `slice` copied to it) along with the length of the table.

This function is exported so that you may use it to build your own dense tables (since the Table in this package can only store floats).

type Atom

type Atom int

Atom represents a unique identifier for a particular string.

type Interner

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

Interner represents the state of a string interner, mapping strings to monotonically increasing integers. A new Interner value *must* be created with the NewInterner function.

An Interner may not be used from multiple goroutines simultaneously.

Interner satisfies all interfaces defined in the `encoding` standard library package.

func NewInterner

func NewInterner() *Interner

NewInterner creates a new interner.

func (*Interner) Atom

func (in *Interner) Atom(s string) Atom

Atom returns the atom of `s` if it exists. If `s` hasn't been interned yet, it is interned and its atom is returned. Therefore, Atom is useful when the set of strings to be interned isn't known at program initialization.

func (*Interner) Atoms

func (in *Interner) Atoms(ss ...string) []Atom

Atoms interns many strings at once. While it should be faster than a series of calls to Atom, it is mainly provided as a convenience function when the set of strings is known ahead of time.

func (*Interner) Exists

func (in *Interner) Exists(s string) bool

Exists returns true if and only if the given string has been interned.

func (*Interner) Len

func (in *Interner) Len() int

Len returns the number of strings that have been interned.

func (*Interner) MarshalBinary

func (in *Interner) MarshalBinary() ([]byte, error)

MarshalBinary satisfies the encoding.BinaryMarshaler interface.

func (*Interner) MarshalText

func (in *Interner) MarshalText() ([]byte, error)

MarshalText satisfies the encoding.TextMarshaler interface.

func (*Interner) Strings

func (in *Interner) Strings() []string

Strings returns a slice copy of all the strings that have been interned.

func (*Interner) UnmarshalBinary

func (in *Interner) UnmarshalBinary(text []byte) error

UnmarshalBinary satisfies the encoding.BinaryUnmarshaler interface.

func (*Interner) UnmarshalText

func (in *Interner) UnmarshalText(text []byte) error

UnmarshalText satisfies the encoding.TextUnmarshaler interface.

type Table

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

Table corresponds to a dense square table of data, where values are floats that correspond to pairwise string keys. Note that the table must by symmetric so that for all k1, k2 in Table, then Table[k1, k2] == Table[k2, k1].

A Table may not be used from multiple goroutines simultaneously.

Table satisfies all interfaces defined in the `encoding` standard library package.

func NewTable

func NewTable(numAtoms int) *Table

NewTable creates a new dense table keyed by string pairs with float values. numAtoms is a "hint" for the length of the table. It may be zero.

func NewTableInterner

func NewTableInterner(in *Interner) *Table

NewTableInterner creates a new dense table from a pre-existing string interner.

func (*Table) Atom

func (t *Table) Atom(s string) Atom

Atom interns a string and returns an Atom that may be used in the Get and Set methods.

func (*Table) Atoms

func (t *Table) Atoms(ss ...string) []Atom

Atoms is a convenience function to intern many strings at once. The slice returned is in correspondence to the strings given.

func (*Table) Get

func (t *Table) Get(a1, a2 Atom) float64

Get retrieves the value corresponding to the string pair (as atoms) given. The order of the string pair does not matter.

func (*Table) MarshalBinary

func (t *Table) MarshalBinary() ([]byte, error)

MarshalBinary satisfies the encoding.BinaryMarshaler interface.

func (*Table) MarshalText

func (t *Table) MarshalText() ([]byte, error)

MarshalText satisfies the encoding.TextMarshaler interface.

func (*Table) Set

func (t *Table) Set(a1, a2 Atom, v float64)

Set sets the value corresponding to the string pair (as atoms) given. The order of the string pair does not matter.

func (*Table) UnmarshalBinary

func (t *Table) UnmarshalBinary(text []byte) error

UnmarshalBinary satisfies the encoding.BinaryUnmarshaler interface.

func (*Table) UnmarshalText

func (t *Table) UnmarshalText(text []byte) error

UnmarshalText satisfies the encoding.TextUnmarshaler interface.