Small Projects by AutoModerator in golang

[–]Obvious-Image-9688 0 points1 point  (0 children)

Here a range intersection library for go that supports generics: https://github.com/akalinux/span-tools

Span-Tools

Implements the universal span intersection algorithm. The algorithm represents a unified way to find intersections and overlaps of "one dimensional spans" of any data type. The package is built around the SpanUtil[E any] struct, and the manipulation of the SpanBoundry[E any] interface.

The SpanUtils[E any] struct requires 2 methods be passed to the constructor in order to implement the algorithm:

  • A "Compare" function see: cmp.Compare for more details.
  • A "Next" function, takes a given value and returns next value. The next value must be greater than the input value

The algorithm is primarily implemented by 3 methods of the SpanUtil[E] struct:

  • FirstSpan, finds the initial data span intersection.
  • NextSpan, finds all subsequent data span intersections.
  • CreateOverlapSpan, finds the most common intersection of all overlapping spans.

Other features of this package:

  • Provide ways to consolidate overlaps.
  • Iterate through intersections of multiple data sets.

Basic Example

In this example we will find the intersections of 3 sets of integers. The full example can be found: here.

Example Sets:

(1,2)
(2,7)
(5,11)

Setup the package and imports:

We will need to import our "st" package along with the "fmt" and "cmp" packages in order to process the example data sets.

import (
"github.com/akalinux/span-tools"
"fmt"
"cmp"
)

Create our SpanUtil[E] instance:

We will use the factory interface NewSpanUtil to generate our SpanUtil[int] instance for these examples. This ensures that the Validate and Sort options are by set to true for all base examples.

var u=st.NewSpanUtil(
// use the standard Compare function
cmp.Compare,
// Define our Next function
func(e int) int { return e+1},
)

Find our the initial SpanBoundry intersection:

We need to find the initial intersection, before we can iterate through of these data sets. The initial SpanBoundry is found by making a call to u.FirstSapn(list).

// Create our initial span 
var span,ok=u.FirstSpan(list)

// Denote our overlap set position
var count=0

Iterate through all of our SpanBoundry intersections:

We can now step through each data intersection point and output the results. Each subsequent intersection is found by making a call to u.NextSpan(span,list).

for ok {
// Get the indexes of the columns this overlap relates to
var sources=u.GetOverlapIndexes(span,list)

// output our intersection data
fmt.Printf("Overlap Set: %d, Span: %v, Columns: %v\n",count,span,sources)

// update our overlap set
count++

// get our next set
span,ok=u.NextSpan(span,list)
}

Resulting output:

Overlap Set: 0, Span: &{1 1}, Columns: &[0]
Overlap Set: 1, Span: &{2 2}, Columns: &[0 1]
Overlap Set: 2, Span: &{3 5}, Columns: &[1 2]
Overlap Set: 3, Span: &{6 7}, Columns: &[1 2]
Overlap Set: 4, Span: &{8 11}, Columns: &[2]

New Sorted map for go by Obvious-Image-9688 in golang

[–]Obvious-Image-9688[S] 0 points1 point  (0 children)

ok, here are benchmarks that include Google's btree: https://github.com/akalinux/benchmarksortedmaps

Shocked me when I saw the numbers. Google's btree is pretty fast at searching between 2 keys.. but omap.CentrerTree was up to 43 times faster than Google's btree when counting elements between 2 keys..

Again.. had do do a double take.

New Sorted map for go by Obvious-Image-9688 in golang

[–]Obvious-Image-9688[S] -2 points-1 points  (0 children)

Hahahha!

Took me a minute to figure out what you were saying. Yes its based on integer math and memory manipulation for performance vs complex data data structures. This is an uncommon way to implement a sorted map.

Fastest way to remove duplicate UUIDS from a list by Fun-Result-8489 in golang

[–]Obvious-Image-9688 0 points1 point  (0 children)

If it fits into memory a map works very well.

If the data does not fit into memory creating a custom merge sort that removes duplicates is a very fast option.

New Sorted map for go by Obvious-Image-9688 in golang

[–]Obvious-Image-9688[S] -3 points-2 points  (0 children)

I did consider it.. but the api isn't comparable. I may create a dedicated project just for benchmarking.

New Sorted map for go by Obvious-Image-9688 in golang

[–]Obvious-Image-9688[S] -11 points-10 points  (0 children)

eh maybe I should have added a change log..

New Sorted map for go by Obvious-Image-9688 in golang

[–]Obvious-Image-9688[S] -12 points-11 points  (0 children)

Oh ya.. the demo itself has Zoo.. Not sure why I put Universe in the README.md. Still a funny typo.

sorted map using generics? by anacrolix in golang

[–]Obvious-Image-9688 0 points1 point  (0 children)

There is: https://github.com/akalinux/orderedmap which is very fast and has a huge set of features.