applejag / typ

Generic types and functions that are missing from Go, including sets, linked lists, trees, etc.
https://pkg.go.dev/gopkg.in/typ.v4
MIT License
32 stars 2 forks source link
go go-generics go-lib go-library go118 golang

go-typ

Codacy Badge REUSE status Go Reference

Generic types and functions that are missing from Go, including sets, trees, linked lists, etc.

All code is implemented with 0 dependencies and in pure Go code (no CGo).

Background

Go v1.18 is about to be released now in February 2022, and with it comes some features that has been talked about for a really long time. One of which being generics! (Go 1.18 beta release notes)

They have moved generics from the Go v2.0 milestone over to Go v1.18, which means they have to stay backwards compatible and cannot alter any existing types. On top of this, they do not seem to plan on releasing any generic data types in the Go standard library until Go v1.19. All in all, to use generic data types with Go v1.18, you'll have to either write your own, or use a third-party package, like this one :)

This repository includes those generic functions and types that I find are missing from the release of Go v1.18-beta1, as well as a number of other data structures and utility functions I think should've been included in the standard library a long time ago. But now with generics, we can finally have sensible implementations of sets, trees, stacks, etc without excessive casting.

Compatibility

Requires Go v1.18rc1 or later as the code makes heavy use of generics.

Installation and usage

go get -u gopkg.in/typ.v4
import (
    "fmt"

    "gopkg.in/typ.v4/avl"
    "gopkg.in/typ.v4/maps"
)

func UsingSets() {
    set1 := make(maps.Set[string])
    set1.Add("A")
    set1.Add("B")
    set1.Add("C")
    fmt.Println("set1:", set1) // {A B C}

    set2 := make(maps.Set[string])
    set2.Add("B")
    set2.Add("C")
    set2.Add("D")
    fmt.Println("set2:", set2) // {B C D}

    fmt.Println("union:", set1.Union(set2))         // {A B C D}
    fmt.Println("intersect:", set1.Intersect(set2)) // {B C}
    fmt.Println("set diff:", set1.SetDiff(set2))    // {A}
    fmt.Println("sym diff:", set1.SymDiff(set2))    // {A D}
}

func UsingAVLTree() {
    tree := avl.NewOrdered[string]()

    // Unordered input
    tree.Add("E")
    tree.Add("B")
    tree.Add("D")
    tree.Add("C")
    tree.Add("A")

    // Sorted output
    fmt.Println(tree.Len(), tree) // 5 [A B C D E]
}

Features

Explanation:

  • Forked type: Copied their code and modified it so it uses generic types down to the backing struct layer. This benefits the most from generics support.

  • Wrapped type: Code depends on the underlying non-generic type, and adds abstraction to hide the type casting. Less performant than full generic support, but is done to reduce excessive complexity in this repository.

  • Neither forked nor wrapped: Original code written by yours truly.

Development

Please read the CONTRIBUTING.md for information about development environment and guidelines.

Similar projects

All the below include multiple data structure implementations each, all with Go 1.18 generics support.

Official Go packages:

License

This project is primarily licensed under the MIT license:

Copyright © Kalle Fagerberg