Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/rp-3/gheap

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/rp-3/gheap

  • v0.0.0-20210123142657-5d4e65717274
  • Source
  • Go
  • Socket score

Version published
Created
Source

Gheap — a generic heap in Golang

Initialization

heap := gheap.NewHeap(-1) // unbounded size

// add anything you want to the heap
type itemToHeapify struct {
    // any properties you want
}

// so long as it conforms to the orderable interface, which looks like this:
type Orderable interface {
	// Order dictates the internal ordering of the items in the heap. Heap is
	// min-ordered, so lowest-order items have the highest priority
	Order() int
}

// make your itemToHeapify conform to the interface by adding a method named
// `Order`, like this:
func (i itemToHeapify) Order int {
    // Returns the relative order of this item as an int
}

Basic Usage

// 1. make sure you're conforming to the protocol
type itemToHeapify struct {
    key int
    val []byte // use whatever you want
}

func (i itemToHeapify) Order int {
    return i.key
}

// 2. create a heap
heap := gheap.NewHeap(-1) // unbounded size

// 3. Push and Pop at will
a, ok := heap.Push(itemToHeapify{ key: 1, val: []byte{} })
b, ok := heap.Pop()
  • If the heap is at capacity (as specified on initialization) then pushing items will cause the lowest-ordered item to be ejected
  • If the heap is empty, then popping will return (nil, false)

FAQs

Package last updated on 23 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc