Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Fast generic Heap data structure in Golang

License

NotificationsYou must be signed in to change notification settings

amirhnajafiz/pyramid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

logo

go versionversionversion

Pyramid

Fast genericHeap in Golang. Create your Heap data structure with any data type. Set yourcompression function to sort your data with any field that you want. Push, Pop and Update yourdata in heap without any problems. Write your equality function to filterthe elements that you want to update.

Pyramid is built withGolang internal libraries and does not include any externallibraries.

How to use?

Getpyramid package.

go get github.com/amirhnajafiz/pyramid

Now you can use pyramid to build any type ofHeap.

Example

Creating amax-heap of type Integer.

package mainimport ("fmt""github.com/amirhnajafiz/pyramid")funcmain() {// initializing a heap with comparator functionh:= pyramid.NewHeap[int](func(aint,bint)bool {returna>b})// Using push methodh.Push(2)h.Push(12)h.Push(4)h.Push(90)h.Push(20)// Using length methodforh.Length()>0 {// Using pop methodfmt.Printf("%d ",h.Pop().(int))}}

Creating amin-heap of type customData.

// Creating a custom Data struct type.typeDatastruct {PriorityintDatastring}funcmain() {// Creating a heap of Data type.h:= pyramid.NewHeap[Data](func(aData,bData)bool {returna.Priority<b.Priority})// Push data into heap.fori:=0;i<10;i++ {h.Push(Data{Priority:i,Data:fmt.Sprintf("data: %d",i+1)})}forh.Length()>0 {fmt.Printf("%s\n",h.Pop().(Data).Data)}}

Updating a reference

You can update any item in the list, also you can define aequal function to check the equality of your objects.

special:=675h.Push(special)fori:=2;i<100;i++ {h.Push(i)}h.Update(special,0,func(aint,bint)bool {returna==b})

Load test

If we have 10000 items, and we want to update them 1 Million times, it wouldonly take 12 seconds:

go run load-test/update/main.go -push 10000 -update 1000000
testing: 10000 numbersstart: Oct 16 14:42:34.752done: Oct 16 14:42:52.064final size: 10000min: 2

[8]ページ先頭

©2009-2025 Movatter.jp