Some helper types for go1: priority queue, slice wrapper.

go-villa GoSearch

Package villa contains some helper types for Go: priority queue, slice wrapper, binary-search, merge-sort.

GoDoc Link: http://godoc.org/github.com/daviddengcn/go-villa (packages that import villa)

Priority Queues

A generic struct, named PriorityQueue, whose element is an interface{} and some structs whose element is a specific number type.

Using a priority queue requires a less function, with two elements to be compared as the arguments.

PriorityQueue

It encapsulates the heap package using the Slice struct.

Usage:

pq := villa.NewPriorityQueue(
    func (a, b interface{}) int {
        if a.(int) < b.(int) {
            return -1
        } else if a.(int) > b.(int) {
            return 1
        }
        return 0
    })
pq.Push(10)
pq.Push(20)

vl := pq.Pop()

IntPriorityQueue

It reimplements the algorithm in heap package. Integers are internally stored in an int slice. Usage:

pq := villa.NewIntPriorityQueue(villa.IntValueCompare)
pq.Push(10)
pq.Push(20)

vl := pq.Pop()

Slice Wrappers

Slice is a wrapper for go slices. It is useful when one needs some modification options on a slice. Implemented methods include: Add, AddSlice, Insert, Swap, Remove, RemoveRange, Fill, Clear

Slice

Usage:

type A struct {
    B, C int
}

var s Slice
s.Add(10, "20", 30)
s.InsertSlice(len(s), []A{A{50, 60}, A{70, 80}})
s.Insert(1, 40, 50)
s.Swap(1, len(s) - 1)
s.RemoveRange(1, 3)
s.Fill(0, len(s), 55)
s.Clear()

StringSlice

StringSlice is a wrapper for []string. Using Add/Insert/InsertSlice methods, the values of other type can be converted and added to the string slice. Usage:

type A struct {
    B string
    C int
}

var s StringSlice
s.Add(10, "B", 30)
s.InsertSlice(len(s), []A{A{"E", 60}, A{"G", 80}})
s.Insert(1, "D", "E")
s.Swap(1, len(s) - 1)
s.RemoveRange(1, 3)
s.Fill(0, len(s), "EE")
s.Clear()

IntSlice/FloatSlice/ComplexSlice

The following int can be replace with float or complex types(complex compare function needs rewriting). Usage(of IntSlice):

var s IntSlice
s.Add(10, 20, 30)
s.Insert(1, 40, 50)
s.Swap(1, len(s) - 1)
s.RemoveRange(1, 3)
s.Fill(0, len(s), 55)
s.Clear()

Comparator functions

The common comparator function which compares elements and return the value <0, =0 or >0, if a < b, a==b, or a > b respectively.

Some algorithms that needs a comparator are defined in their methods, including sort(using build-in sort package algorithm), binary-search, and merge. Cast your own comparator function to the corresponding comparator type to use them:

func MyCmp(a, b int) int {
   ...
}

var s, l []int
cmp := IntCmpFunc(MyCmp)
cmp.Sort(s)
cmp.BinarySearch(s, e)

cmp.Sort(l)
sl := cmp.Merge(s, l)

Two comparators are defined for natual orders of ints and floats.

var IntValueCompare IntCmpFunc
var FloatValueCompare FloatCmpFunc

IntMatrix

IntMatrix is 2D array of integers. Elements are stored in a single int slice and slices of each row are created.

LICENSE

This library is distributed under BSD license.

Similar Resources

Go types of schema.org ontology

schema.org for golang The library declares Go types of https://schema.org ontology. Inspiration Schema.org is a collaborative, community activity with

Jun 7, 2022

Go package that adds marshal and unmarshal features to nullable sql types.

#Nullable Very simple Go module to handle nullable fields. Basically, it adds to sql package types the JSON marshal and unmarshal features. It has 100

Jan 20, 2022

Mimetype - This package provides mime-types as constants.

mimetype This package provides mimetypes as constants. The constants are generated for the IONA website: https://www.iana.org/assignments/media-types/

Dec 27, 2022

Some utilities for Persian language in Go (Golang)

persian Some utilities for Persian language in Go (Golang). Installation go get github.com/mavihq/persian API .ToPersianDigits Converts all English d

Oct 22, 2022

Functional programming library for Go including a lazy list implementation and some of the most usual functions.

functional A functional programming library including a lazy list implementation and some of the most usual functions. import FP "github.com/tcard/fun

May 21, 2022

Some gophers 🐻

Some gophers 🐻

Gophers 🐻 Hey 👋 ! Here are some drawings I have been using for talks about Golang. These are mainly gophers, but also some dukes, and miscellaneous.

Nov 15, 2022

dropspy is a (POC-quality) reworking of the C-language dropwatch tool in Go, with some extra features.

dropspy is a (POC-quality) reworking of the C-language dropwatch tool in Go, with some extra features.

Dec 12, 2022

Some examples of testing techniques and commonly used frameworks

golang-test-examples Some examples of testing techniques and commonly used frameworks Test frameworks Testify Ginkgo Convey godog (cucumber) Test exam

Oct 29, 2021

Some convenient string functions.

str Some convenient string functions. What This package containsa couple of functions to remove duplicates from string slices and optionally sort them

Dec 27, 2021
Via Cep Wrapper is a api wrapper used to find address by zipcode (Brazil only)
Via Cep Wrapper is a api wrapper used to find address by zipcode (Brazil only)

Viacep Wrapper Viacep Wrapper is an API wrapper built with Golang used to find address by zipcode (Brazil only). This project was developed for study

Jan 25, 2022
Go 1.18 Generics based slice package

The missing slice package A Go-generics (Go 1.18) based functional library with no side-effects that adds the following functions to a slice package:

Jan 8, 2023
Golang Github Template Helper

ggth Golang Github Template Helper Usage ggth PreBuilt Binaries Grab Binaries from The Releases Page Install Homebrew brew install Jmainguy/tap/ggth

Jan 4, 2022
Go jackc/pgx helper to write proper transactions

Go jackc/pgx helper to write proper transactions.

Dec 16, 2022
traning helper. Reading real METARs

pptrain Train reading real METARs Example: $ pptrain

Jan 23, 2022
traefik-certs-cleaner is a simple helper to clean the Traefik acme.json file by removing certificates

traefik-certs-cleaner If you appreciate this project: Description traefik-certs-cleaner is a simple helper to clean the Traefik acme.json file by remo

Oct 31, 2022
A minimalistic message queue server, in Go

lineup, a tiny queue server lineup is a very simple queue server. all it does is serve a binary-safe priority queue with a simple protocol insp

Aug 17, 2022
Fast conversions across various Go types with a simple API.

Go Package: conv Get: go get -u github.com/cstockton/go-conv Example: // Basic types if got, err := conv.Bool(`TRUE`); err == nil { fmt.Printf("conv.

Nov 29, 2022
[TOOL, CLI] - Filter and examine Go type structures, interfaces and their transitive dependencies and relationships. Export structural types as TypeScript value object or bare type representations.

typex Examine Go types and their transitive dependencies. Export results as TypeScript value objects (or types) declaration. Installation go get -u gi

Dec 6, 2022
Library to work with MimeHeaders and another mime types. Library support wildcards and parameters.

Mime header Motivation This library created to help people to parse media type data, like headers, and store and match it. The main features of the li

Nov 9, 2022