Help us shape the future of Go! Take the 2026 Go Developer Survey

nwenc

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 6 Imported by: 0

README

nwenc

TravisCI CodeCov Go Report Card GoDoc

Package nwenc is the implementation of encoder and decoder for the nextword data.

PosEncoder/PosDecoder

The PosEncoder and PosDecoder can encode/decode between an int64 position value and a string. There are several implementation for PosMapper. They have different performance.

PosEncoder/PosDecoder example:

f, err := os.Open(filepath.Join("testdata", "words.txt"))
pm, err := NewAllReadPosMapper(f)

pm.PosEncode("a")   // 0 ("a" is at 0 byte in testdata/words.txt)
pm.PosEncode("bcd") // 43 ("bcd" is at 43 bytes in testdata/words.txt)

pm.PosDecode(0)  // "a"
pm.PosDecode(43) // "bcd"

Encoder/Decoder

Encoder and Decoder can encode/decode bwtween an int64 position value and bytes. Encoder/Decoder example:

f, err := os.Open(filepath.Join("testdata", "words.txt"))
pm, err := NewAllReadPosMapper(f)

buf := new(bytes.Buffer)

byteLen := 3 // It encodes/decodes 3 bytes (24 bits) data.
enc, err := NewEncoder(byteLen)

enc.Encode(buf, 0)               // writes "a" at pos == 0
enc.EncodeString(buf, pm, "bcd") // writes "bcd"

dec, err := NewDecoder(byteLen)

dec.Decode(buf)           // 0 ("a")
dec.DecodeString(buf, pm) // "bcd"

Documentation

Overview

Package nwenc is the implementation of encoder and decoder for the nextword data.

The PosEncoder and PosDecoder can encode/decode between an int64 position value and a string. There are several implementation for PosMapper. They have different performance.

Encoder and Decoder can encode/decode bwtween an int64 position value and bytes.

PosEncoder/PosDecoder example:

f, err := os.Open(filepath.Join("testdata", "words.txt"))
pm, err := NewAllReadPosMapper(f)

pm.PosEncode("a")   // 0 ("a" is at 0 byte in testdata/words.txt)
pm.PosEncode("bcd") // 43 ("bcd" is at 43 bytes in testdata/words.txt)

pm.PosDecode(0)  // "a"
pm.PosDecode(43) // "bcd"

Encoder/Decoder example:

f, err := os.Open(filepath.Join("testdata", "words.txt"))
pm, err := NewAllReadPosMapper(f)

buf := new(bytes.Buffer)

byteLen := 3 // It encodes/decodes 3 bytes (24 bits) data.
enc, err := NewEncoder(byteLen)

enc.Encode(buf, 0)               // writes "a" at pos == 0
enc.EncodeString(buf, pm, "bcd") // writes "bcd"

dec, err := NewDecoder(byteLen)

dec.Decode(buf)           // 0 ("a")
dec.DecodeString(buf, pm) // "bcd"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllReadPosMapper

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

AllReadPosMapper is one of the implementation of PosMapper. It reads all of io.Reader in advance in order to map fast.

func NewAllReadPosMapper

func NewAllReadPosMapper(r io.Reader) (*AllReadPosMapper, error)

NewAllReadPosMapper returns an AllReadPosMapper. This function reads all of io.Reader in advance in order to map fast.

func (*AllReadPosMapper) PosDecode

func (m *AllReadPosMapper) PosDecode(pos int64) (s string, err error)

PosDecode is the implementation of PosDecode. It works fast. When pos is not found, it will return PosDecodeError.

func (*AllReadPosMapper) PosEncode

func (m *AllReadPosMapper) PosEncode(s string) (pos int64, err error)

PosEncode is the implementation of PosEncoder. It works fast. When s is not found, it will return PosEncodeError.

type CachedSeekPosMapper

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

CachedSeekPosMapper is the implementation of PosMapper. It seeks io.ReaderAt when methods are called, but caches the results. It's takes shorter time rather than SeekPosMapper.

func NewCachedSeekPosMapper

func NewCachedSeekPosMapper(r io.ReaderAt, size int64) *CachedSeekPosMapper

NewCachedSeekPosMapper returns a CachedSeekPosMapper. The size is total bytes of r. It seeks io.ReaderAt when methods are called, but caches the results. It's takes shorter time rather than SeekPosMapper.

func (*CachedSeekPosMapper) PosDecode

func (pm *CachedSeekPosMapper) PosDecode(pos int64) (s string, err error)

PosDecode is the implementation of PosDecoder.

func (*CachedSeekPosMapper) PosEncode

func (pm *CachedSeekPosMapper) PosEncode(s string) (pos int64, err error)

PosEncode is the implementation of PosEncoder. This method makes cache when it is called.

type Decoder

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

Decoder decodes bytes to an int64 or a string.

func NewDecoder

func NewDecoder(byteLen int) (*Decoder, error)

NewDecoder returns Decoder. The byteLen is the length of bytes. The byteLen must be 1 <= bytelen <= 8.

func (*Decoder) Decode

func (d *Decoder) Decode(r io.Reader) (pos int64, err error)

Decode reads r and decodes to pos.

func (*Decoder) DecodeString

func (d *Decoder) DecodeString(r io.Reader, pd PosDecoder) (s string, err error)

DecodeString reads r and decodes to s.

type Encoder

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

Encoder encodes an int64 to bytes.

func NewEncoder

func NewEncoder(byteLen int) (*Encoder, error)

NewEncoder returns an Encoder. The byteLen is the length of encoded bytes. The byteLen must be 1 <= byteLen <= 8.

func (*Encoder) Encode

func (e *Encoder) Encode(w io.Writer, pos int64) error

Encode encodes pos to bytes and writes it to w.

func (*Encoder) EncodeString

func (e *Encoder) EncodeString(w io.Writer, pd PosEncoder, s string) error

EncodeString encodes s to bytes and writes it to w.

type PosDecodeError

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

PosDecodeError is returned when PosDecode fails.

func (*PosDecodeError) Error

func (e *PosDecodeError) Error() string

type PosDecoder

type PosDecoder interface {
	PosDecode(pos int64) (s string, err error)
}

PosDecoder is the interface which can map an int64 position to a string. The position is the offset where the string appears in a text file.

type PosEncodeError

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

PosEncodeError is returned when PosEncode fails.

func (*PosEncodeError) Error

func (e *PosEncodeError) Error() string

type PosEncoder

type PosEncoder interface {
	PosEncode(s string) (pos int64, err error)
}

PosEncoder is the interface which can map a string to an int64 position. The position is the offset where the string appears in a text file.

type PosMapper

type PosMapper interface {
	PosEncoder
	PosDecoder
}

PosMapper implements both PosEncoder and PosDecoder.

type SeekPosMapper

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

SeekPosMapper is the implementation of PosMapper. It seeks file each time when PosEncode or PosDecode are called, so it works slowly.

func NewSeekPosMapper

func NewSeekPosMapper(r io.ReaderAt, size int64) *SeekPosMapper

NewSeekPosMapper returns an NewSeekPosMapper. The size is the total bytes of r. It seeks file each time when PosEncode or PosDecode are called, so it works slowly.

func (*SeekPosMapper) PosDecode

func (pm *SeekPosMapper) PosDecode(pos int64) (s string, err error)

PosDecode is the implementation of PosDecoder. This function works slow because it needs io.ReadAt seeking each time.

func (*SeekPosMapper) PosEncode

func (pm *SeekPosMapper) PosEncode(s string) (pos int64, err error)

PosEncode is the implementation of PosEncoder. This function works slow because it needs io.ReadAt seeking each time.

Jump to

Keyboard shortcuts

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