Core Framework Math Suite for Web3 Networking
Find a file
2025-11-03 19:09:40 +00:00
.github/workflows Add fuzz testing step to CI workflow 2025-11-03 19:09:40 +00:00
docs Add Cosine and Weighted Cosine distance metrics, enhance KDTree helpers, and update version to 0.3.0 2025-11-03 18:51:23 +00:00
examples Add lint configuration, benchmarks, and documentation for KDTree 2025-11-03 18:19:12 +00:00
.gitignore Bootstrap Go library with EUPL-1.2, goreleaser, mkdocs, and sorting functions 2025-11-03 15:37:45 +00:00
.golangci.yml Add examples for KDTree and enable errcheck linter in CI 2025-11-03 18:41:28 +00:00
.goreleaser.yaml Add GoReleaser configuration for GitHub Releases and update CI workflow 2025-11-03 18:58:44 +00:00
.goreleaser.yml Add GoReleaser configuration for GitHub Releases and update CI workflow 2025-11-03 18:58:44 +00:00
bench_kdtree_test.go Add lint configuration, benchmarks, and documentation for KDTree 2025-11-03 18:19:12 +00:00
CHANGELOG.md Fix example function names, stabilize tests, and relax floating-point comparisons 2025-11-03 19:07:08 +00:00
CODE_OF_CONDUCT.md Add lint configuration, benchmarks, and documentation for KDTree 2025-11-03 18:19:12 +00:00
CONTRIBUTING.md Add GoReleaser configuration for GitHub Releases and update CI workflow 2025-11-03 18:58:44 +00:00
doc.go Add Cosine and Weighted Cosine distance metrics, enhance KDTree helpers, and update version to 0.3.0 2025-11-03 18:51:23 +00:00
examples_test.go Fix example function names, stabilize tests, and relax floating-point comparisons 2025-11-03 19:07:08 +00:00
fuzz_kdtree_test.go Add Cosine and Weighted Cosine distance metrics, enhance KDTree helpers, and update version to 0.3.0 2025-11-03 18:51:23 +00:00
go.mod Add CI workflow and update documentation for KDTree version 2025-11-03 18:10:49 +00:00
kdtree.go Add Cosine and Weighted Cosine distance metrics, enhance KDTree helpers, and update version to 0.3.0 2025-11-03 18:51:23 +00:00
kdtree_helpers.go Add Cosine and Weighted Cosine distance metrics, enhance KDTree helpers, and update version to 0.3.0 2025-11-03 18:51:23 +00:00
kdtree_helpers_test.go Add normalization stats helpers and builders for KDTree 2025-11-03 18:36:09 +00:00
kdtree_nd_test.go Fix example function names, stabilize tests, and relax floating-point comparisons 2025-11-03 19:07:08 +00:00
kdtree_test.go Add KDTree implementation and example for finding the best DHT peer by ping 2025-11-03 16:49:31 +00:00
LICENSE Bootstrap Go library with EUPL-1.2, goreleaser, mkdocs, and sorting functions 2025-11-03 15:37:45 +00:00
mkdocs.yml Enhance CI workflow with coverage and benchmark reporting for KDTree 2025-11-03 18:26:40 +00:00
poindexter.go Add Cosine and Weighted Cosine distance metrics, enhance KDTree helpers, and update version to 0.3.0 2025-11-03 18:51:23 +00:00
poindexter_test.go Add Cosine and Weighted Cosine distance metrics, enhance KDTree helpers, and update version to 0.3.0 2025-11-03 18:51:23 +00:00
README.md Add GoReleaser configuration for GitHub Releases and update CI workflow 2025-11-03 18:58:44 +00:00
SECURITY.md Add lint configuration, benchmarks, and documentation for KDTree 2025-11-03 18:19:12 +00:00
sort.go Add CI workflow and update documentation for KDTree version 2025-11-03 18:10:49 +00:00
sort_test.go Bootstrap Go library with EUPL-1.2, goreleaser, mkdocs, and sorting functions 2025-11-03 15:37:45 +00:00

Poindexter

Go Reference CI Go Report Card Vulncheck codecov Release

A Go library package providing utility functions including sorting algorithms with custom comparators.

Features

  • 🔢 Sorting Utilities: Sort integers, strings, and floats in ascending or descending order
  • 🎯 Custom Sorting: Sort any type with custom comparison functions or key extractors
  • 🔍 Binary Search: Fast search on sorted data
  • 🧭 KDTree (NN Search): Build a KDTree over points with generic payloads; nearest, k-NN, and radius queries with Euclidean, Manhattan, Chebyshev, and Cosine metrics
  • 📦 Generic Functions: Type-safe operations using Go generics
  • Well-Tested: Comprehensive test coverage
  • 📖 Documentation: Full documentation available at GitHub Pages

Installation

go get github.com/Snider/Poindexter

Quick Start

package main

import (
    "fmt"
    poindexter "github.com/Snider/Poindexter"
)

func main() {
    // Basic sorting
    numbers := []int{3, 1, 4, 1, 5, 9}
    poindexter.SortInts(numbers)
    fmt.Println(numbers) // [1 1 3 4 5 9]

    // Custom sorting with key function
    type Product struct {
        Name  string
        Price float64
    }

    products := []Product{{"Apple", 1.50}, {"Banana", 0.75}, {"Cherry", 3.00}}
    poindexter.SortByKey(products, func(p Product) float64 { return p.Price })

    // KDTree quick demo
    pts := []poindexter.KDPoint[string]{
        {ID: "A", Coords: []float64{0, 0}, Value: "alpha"},
        {ID: "B", Coords: []float64{1, 0}, Value: "bravo"},
        {ID: "C", Coords: []float64{0, 1}, Value: "charlie"},
    }
    tree, _ := poindexter.NewKDTree(pts, poindexter.WithMetric(poindexter.EuclideanDistance{}))
    nearest, dist, _ := tree.Nearest([]float64{0.9, 0.1})
    fmt.Println(nearest.ID, nearest.Value, dist) // B bravo ~0.141...
}

Documentation

Full documentation is available at https://snider.github.io/Poindexter/

Explore runnable examples in the repository:

  • examples/dht_ping_1d
  • examples/kdtree_2d_ping_hop
  • examples/kdtree_3d_ping_hop_geo
  • examples/kdtree_4d_ping_hop_geo_score

KDTree performance and notes

  • Current KDTree queries are O(n) linear scans, which are great for small-to-medium datasets or low-latency prototyping. For 1e5+ points and low/medium dimensions, consider swapping the internal engine to gonum.org/v1/gonum/spatial/kdtree (the API here is compatible by design).
  • Insert is O(1) amortized; delete by ID is O(1) via swap-delete; order is not preserved.
  • Concurrency: the KDTree type is not safe for concurrent mutation. Protect with a mutex or share immutable snapshots for read-mostly workloads.
  • See multi-dimensional examples (ping/hops/geo/score) in docs and examples/.
  • Performance guide: see docs/Performance for benchmark guidance and tips: docs/perf.md • Hosted: https://snider.github.io/Poindexter/perf/

Choosing a metric (quick tips)

  • Euclidean (L2): smooth trade-offs across axes; solid default for blended preferences.
  • Manhattan (L1): emphasizes per-axis absolute differences; good when each unit of ping/hop matters equally.
  • Chebyshev (L∞): dominated by the worst axis; useful for strict thresholds (e.g., reject high hop count regardless of ping).
  • Cosine: angle-based for vector similarity; pair it with normalized/weighted features when direction matters more than magnitude.

See the multi-dimensional KDTree docs for end-to-end examples and weighting/normalization helpers: Multi-Dimensional KDTree (DHT).

License

This project is licensed under the European Union Public Licence v1.2 (EUPL-1.2). See LICENSE for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.