uniqueslice

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: MIT Imports: 2 Imported by: 2

README

uniqueslice

The uniqueslice package provides facilities for canonicalizing ("interning") slices of comparable values.

Documentation

GoDoc

Example

package main

import (
	"bufio"
	"fmt"
	"strings"

	"github.com/Snawoot/uniqueslice"
)

func main() {
	r := strings.NewReader(`123
1234
123`)
	scanner := bufio.NewScanner(r)
	var handles []uniqueslice.Handle[[]byte, byte]
	for scanner.Scan() {
		handles = append(handles, uniqueslice.Make(scanner.Bytes()))
	}
	if err := scanner.Err(); err != nil {
		panic(fmt.Errorf("scanner error: %v", err))
	}
	for i, h := range handles {
		fmt.Printf("handles[%d] = %v (%q)\n", i, h.Value(), h.Value())
	}
	if handles[0] != handles[2] {
		panic("handles[0] != handles[2]")
	}
	if handles[0] == handles[1] {
		panic("handles[0] == handles[1]")
	}
}

Documentation

Overview

Example
package main

import (
	"bufio"
	"fmt"
	"strings"

	"github.com/Snawoot/uniqueslice"
)

func main() {
	r := strings.NewReader(`123
1234
123`)
	scanner := bufio.NewScanner(r)
	var handles []uniqueslice.Handle[[]byte, byte]
	for scanner.Scan() {
		handles = append(handles, uniqueslice.Make(scanner.Bytes()))
	}
	if err := scanner.Err(); err != nil {
		panic(fmt.Errorf("scanner error: %v", err))
	}
	for i, h := range handles {
		fmt.Printf("handles[%d] = %v (%q)\n", i, h.Value(), h.Value())
	}
	if handles[0] != handles[2] {
		panic("handles[0] != handles[2]")
	}
	if handles[0] == handles[1] {
		panic("handles[0] == handles[1]")
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handle

type Handle[Slice ~[]T, T comparable] struct {
	// contains filtered or unexported fields
}

Handle is a globally unique identity for some slice of value of type T.

Two handles compare equal exactly if the two values used to create the handles would have also compared equal. The comparison of two handles is trivial and typically much more efficient than comparing the values used to create them.

func Make

func Make[Slice ~[]T, T comparable](s Slice) Handle[Slice, T]

Make returns a globally unique handle for a slice of values of type T. Handles are equal if and only if the values used to produce them are equal. Make is safe for concurrent use by multiple goroutines.

func (Handle[Slice, T]) Value

func (h Handle[Slice, T]) Value() Slice

Value returns a slice of shallow copies of the T value that produced the Handle. Value is safe for concurrent use by multiple goroutines.

Jump to

Keyboard shortcuts

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