example_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // CookieJar - A contestant's algorithm toolbox
  2. // Copyright (c) 2013 Peter Szilagyi. All rights reserved.
  3. //
  4. // CookieJar is dual licensed: you can redistribute it and/or modify it under
  5. // the terms of the GNU General Public License as published by the Free Software
  6. // Foundation, either version 3 of the License, or (at your option) any later
  7. // version.
  8. //
  9. // The toolbox is distributed in the hope that it will be useful, but WITHOUT
  10. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. // more details.
  13. //
  14. // Alternatively, the CookieJar toolbox may be used in accordance with the terms
  15. // and conditions contained in a signed written agreement between you and the
  16. // author(s).
  17. package prque_test
  18. import (
  19. "fmt"
  20. "gopkg.in/karalabe/cookiejar.v2/collections/prque"
  21. )
  22. // Insert some data into a priority queue and pop them out in prioritized order.
  23. func Example_usage() {
  24. // Define some data to push into the priority queue
  25. prio := []float32{77.7, 22.2, 44.4, 55.5, 11.1, 88.8, 33.3, 99.9, 0.0, 66.6}
  26. data := []string{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}
  27. // Create the priority queue and insert the prioritized data
  28. pq := prque.New()
  29. for i := 0; i < len(data); i++ {
  30. pq.Push(data[i], prio[i])
  31. }
  32. // Pop out the data and print them
  33. for !pq.Empty() {
  34. val, prio := pq.Pop()
  35. fmt.Printf("%.1f:%s ", prio, val)
  36. }
  37. // Output:
  38. // 99.9:seven 88.8:five 77.7:zero 66.6:nine 55.5:three 44.4:two 33.3:six 22.2:one 11.1:four 0.0:eight
  39. }