Socket
Book a DemoInstallSign in
Socket

github.com/njayp/heaps

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/njayp/heaps

v1.0.1
Source
Go
Version published
Created
Source

Heaps

Motivation

The goal is to build upon container/heap for easier usage. Specifically:

  • remove the need for boilerplate code
  • reduce the api footprint

To accomplish this, this library implements a generic array that satisfies the heap interface. This array is then wrapped with container/heap functionality.

Instillation

go get github.com/njayp/heaps

Usage

Create a heap of custom structs

// define custom struct
type item struct {
	value int
}

// a function for comparing structs
less := func(a, b item) bool {
	return a.value < b.value
}

// init heap with data
heap := heaps.NewHeap(less, 
item{value: 1},
item{value: 2},
)

// use heap
min := heap.Pop() // item{value: 1}

Or use a preset heap

// init heap with data
heap := heaps.NewMaxHeap(0, 1, 2)

// use heap
max := heap.Pop() // 2

Additional Functionality

Using a generic array allows us to add Peek functionality, as the root of the heap will always be at index zero.

FAQs

Package last updated on 03 Mar 2025

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.