mappedslice

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 4 Imported by: 1

README

mappedslice GoDoc Status Go Report Card Go Test Coverage

mappedslice is a Go library for efficient, memory map-backed slices. It provides a way to work with large datasets by mapping files directly into memory, allowing for fast and efficient access and manipulation of data. Ideal for applications needing high-performance data handling without loading entire files into RAM.

Features:

  • Memory-mapped slices for efficient file access
  • Supports large datasets with minimal memory overhead
  • Easy-to-use API for integrating with existing Go applications

Usage

New
func ExampleNew() {
	var err error
	if exampleSlice, err = New[int]("myfile.bat", 32); err != nil {
		// Handle error here
		return
	}
}
Slice.Get
func ExampleSlice_Get() {
	var (
		v   int
		ok bool
	)

	if v, ok = exampleSlice.Get(0); !ok {
		// Missing entry here
		return
	}

	fmt.Println("Value", v)
}
Slice.Set
func ExampleSlice_Set() {
	var err error
	if err = exampleSlice.Set(0, 1337); err != nil {
		// Handle error here
		return
	}
}
Slice.Append
func ExampleSlice_Append() {
	var err error
	if err = exampleSlice.Append(1337); err != nil {
		// Handle error here
		return
	}
}
Slice.InsertAt
func ExampleSlice_InsertAt() {
	var err error
	if err = exampleSlice.InsertAt(0, 1337); err != nil {
		// Handle error here
		return
	}
}
Slice.RemoveAt
func ExampleSlice_RemoveAt() {
	var err error
	if err = exampleSlice.RemoveAt(0); err != nil {
		// Handle error here
		return
	}
}
Slice.ForEach
func ExampleSlice_ForEach() {
	exampleSlice.ForEach(func(v int) (end bool) {
		fmt.Println("Value", v)
		return
	})
}
Slice.Cursor

func ExampleSlice_Cursor() {
	cur := exampleSlice.Cursor()
	v, ok := cur.Seek(1337)
	if !ok {
		fmt.Println("index is missing")
		return
	}

	fmt.Println("My seek value!", v)

	for ok {
		v, ok = cur.Next()
		fmt.Println("My next value!", v)
	}
}
Slice.Cursor (using previous)
func ExampleSlice_Cursor_prev() {
	cur := exampleSlice.Cursor()
	v, ok := cur.Seek(1337)
	if !ok {
		fmt.Println("index is missing")
		return
	}

	fmt.Println("My seek value!", v)

	for ok {
		v, ok = cur.Prev()
		fmt.Println("My previous value!", v)
	}
}
Slice.Len
func ExampleSlice_Len() {
	fmt.Println("Length", exampleSlice.Len())
}
Slice.Slice
func ExampleSlice_Slice() {
	fmt.Println("Slice copy", exampleSlice.Slice())
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoundsError added in v0.7.0

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

func (*BoundsError) Error added in v0.7.0

func (b *BoundsError) Error() string

type Cursor added in v0.2.2

type Cursor[T any] interface {
	Seek(index int) (T, error)
	Next() (T, error)
	Prev() (T, error)
	Close() error
}

type Slice

type Slice[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](filepath string, initialCapacity int64) (ref *Slice[T], err error)
Example
var err error
if exampleSlice, err = New[int]("myfile.bat", 32); err != nil {
	// Handle error here
	return
}

func (*Slice[T]) Append

func (s *Slice[T]) Append(t T) (err error)
Example
var err error
if err = exampleSlice.Append(1337); err != nil {
	// Handle error here
	return
}

func (*Slice[T]) Close

func (s *Slice[T]) Close() (err error)

func (*Slice[T]) Cursor added in v0.2.2

func (s *Slice[T]) Cursor() (out Cursor[T])
Example
cur := exampleSlice.Cursor()
v, err := cur.Seek(1337)
if err != nil {
	fmt.Println("index is missing")
	return
}

fmt.Println("My seek value!", v)

for err == nil {
	v, err = cur.Next()
	fmt.Println("My next value!", v)
}
Example (Prev)
cur := exampleSlice.Cursor()
v, err := cur.Seek(1337)
if err != nil {
	fmt.Println("index is missing")
	return
}

fmt.Println("My seek value!", v)

for err == nil {
	v, err = cur.Prev()
	fmt.Println("My previous value!", v)
}

func (*Slice[T]) ForEach

func (s *Slice[T]) ForEach(fn func(T) (end bool)) (ended bool)
Example
exampleSlice.ForEach(func(v int) (end bool) {
	fmt.Println("Value", v)
	return
})

func (*Slice[T]) Get

func (s *Slice[T]) Get(index int) (kv T, err error)
Example
var (
	v   int
	err error
)

if v, err = exampleSlice.Get(0); err != nil {
	// Missing entry here
	return
}

fmt.Println("Value", v)

func (*Slice[T]) InsertAt

func (s *Slice[T]) InsertAt(index int, value T) (err error)
Example
var err error
if err = exampleSlice.InsertAt(0, 1337); err != nil {
	// Handle error here
	return
}

func (*Slice[T]) Len

func (s *Slice[T]) Len() int
Example
fmt.Println("Length", exampleSlice.Len())

func (*Slice[T]) RemoveAt

func (s *Slice[T]) RemoveAt(index int) (err error)
Example
var err error
if err = exampleSlice.RemoveAt(0); err != nil {
	// Handle error here
	return
}

func (*Slice[T]) Set

func (s *Slice[T]) Set(index int, t T) (err error)
Example
var err error
if err = exampleSlice.Set(0, 1337); err != nil {
	// Handle error here
	return
}

func (*Slice[T]) Slice

func (s *Slice[T]) Slice() (out []T)
Example
fmt.Println("Slice copy", exampleSlice.Slice())

Jump to

Keyboard shortcuts

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