lookup

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: MIT Imports: 3 Imported by: 0

README

lookup.go

A type for looking up the presence of a value. Either a slice or a set depending on the collection size.

For very small collections (around 10 or less items), it can be faster to find a value by iterating over a slice rather than using a map. The Lookup type abstracts slices and maps so that you have one interface for both.

When to use this

  • The size of the collection is known and not subject to much change.
  • The order of the elements does not matter.
  • You are primarily using the collection to look for the presence of a value.

When to not use this

  • You will be frequently adding or removing elements from the collection after populating its initial values.
  • You need to preserve the order of the elements in the collection.

Example

const sizeThreshold int = 10

// This will be backed by a slice because the number of items is below the threshold.
colorDisplayChoices := lookup.New[string](sizeThreshold, "auto", "on", "off")
colorDisplayChoice := getUserInput()
if !colorDisplayChoices.Has(colorDisplayChoice) {
	fmt.Printf("%q is not a valid choice!\n", colorDisplayChoice)
}

const minYear int = 1900
const maxYear int = 2025
const size int = maxYear - minYear + 1
// This will be backed by a map because the size is above the threshold.
yearChoices := lookup.WithSize[int](sizeThreshold, size)
for i := minYear; i <= maxYear; i++ {
	yearChoices.Add(i)
}
yearChoice := getUserIntInput()
if !yearChoices.Has(yearChoice) {
	fmt.Printf("%d is not a valid year!\n", yearChoice)
}

Documentation

Overview

Package lookup provides utilities for checking for the presence of a value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lookup

type Lookup[T comparable] interface {
	// Has returns true if the value exists in the lookup.
	Has(value T) bool
	// Add adds a value to the lookup, and returns true if it already existed.
	Add(value T) bool
	// Remove removes a matching value from the lookup if it exists. It returns true if
	// the value was found.
	Remove(value T) bool
	// Len gets the length of the collection.
	Len() int
	// Iter produces an iterator over all the values in the lookup.
	Iter() iter.Seq[T]
}

Lookup is a type that can check for the presence of a value.

func New

func New[T comparable](threshold int, elements []T) Lookup[T]

New creates a new Lookup. The threshold is compared to the number of elements passed to determine how the elements will be stored.

func WithSize

func WithSize[T comparable](threshold int, size int, elements ...T) Lookup[T]

WithSize creates a new Lookup. The threshold is compared to the size to determine how the elements will be stored. The backing slice or map will have a size set to size.

Jump to

Keyboard shortcuts

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