Giter Club home page Giter Club logo

azhpq's Introduction

azhpq

PkgGoDev Go Report Card

golang heap priority queue

  • simple, quick, & lightweight
  • zero dependencies

USE:

  hpq := New()

  qn1 := &QueueNode{
    Value:    "andy",
    Priority: 1,
  }

  qn2 := &QueueNode{
    Value:    "john",
    Priority: 2,
  }

  qn3 := &QueueNode{
    Value:    "jim",
    Priority: 5,
  }
  
  hpq.Add(qn1)
  hpq.Add(qn2) 
  hpq.Add(qn3)
  // OR
  hpq.AddMany(qn1, qn2, qn3)

  hpq.ForEach(func (qn *QueueNode, i int) {
    log.Println(fmt.Sprintf("%d ==> %+v", i, qn))
  })

  // OUT: 
  // 0 ==> &{Value:jim Priority:5}
  // 1 ==> &{Value:john Priority:2}
  // 2 ==> &{Value:andy Priority:1}

  highestPrioNode := hpq.Poll()
  log.Println(fmt.Sprintf("removed highest prio node: %s", highestPrioNode.Value))

  // OUT: 
  // removed highest prio node: jim

  hpq.ForEach(func (qn *QueueNode, i int) {
    log.Println(fmt.Sprintf("%d ==> %+v", i, qn))
  })

  // OUT: 
  // 0 ==> &{Value:john Priority:2}
  // 1 ==> &{Value:andy Priority:1}

TEST:

  • go test -v -run TestHPQ azhpq.go azhpq_test.go
  === RUN   TestHPQ
  2021/08/17 10:54:59 0 ==> &{Value:jim Priority:5}
  2021/08/17 10:54:59 1 ==> &{Value:john Priority:2}
  2021/08/17 10:54:59 2 ==> &{Value:andy Priority:1}
  2021/08/17 10:54:59 0 ==> &{Value:jim Priority:7}
  2021/08/17 10:54:59 1 ==> &{Value:john Priority:4}
  2021/08/17 10:54:59 2 ==> &{Value:andy Priority:3}
  2021/08/17 10:54:59 removed highest prio node: jim
  2021/08/17 10:54:59 0 ==> &{Value:john Priority:4}
  2021/08/17 10:54:59 1 ==> &{Value:andy Priority:3}
  --- PASS: TestHPQ (0.00s)
  ok      command-line-arguments  0.138s

AUTHOR:

azhpq's People

Contributors

revzim avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.