{"id":17299901,"url":"https://github.com/twmb/go-sliceheap","last_synced_at":"2025-10-30T03:53:03.246Z","repository":{"id":70205871,"uuid":"202089683","full_name":"twmb/go-sliceheap","owner":"twmb","description":"A Go package to provide heaps on arbitrary slices.","archived":false,"fork":false,"pushed_at":"2023-04-05T04:31:19.000Z","size":10,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-01T03:43:16.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/twmb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-08-13T07:32:31.000Z","updated_at":"2024-07-01T17:12:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"fc3bfbb2-268b-415f-b65d-5c6fad819398","html_url":"https://github.com/twmb/go-sliceheap","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twmb%2Fgo-sliceheap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twmb%2Fgo-sliceheap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twmb%2Fgo-sliceheap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twmb%2Fgo-sliceheap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twmb","download_url":"https://codeload.github.com/twmb/go-sliceheap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245743435,"owners_count":20665093,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T11:24:25.207Z","updated_at":"2025-10-30T03:53:03.147Z","avatar_url":"https://github.com/twmb.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"go-sliceheap\n============\n\nPackage sliceheap returns a quick heap given a pointer to a slice and a less\nfunction (akin to sort.Slice for sorting slices).\n\nThis package is for that rare time when you need a heap and do not want to make\nan arbitrary type to provide `Push` and `Pop`.\n\nDocumentation\n-------------\n\n[![GoDoc](https://godoc.org/github.com/twmb/go-sliceheap?status.svg)](https://godoc.org/github.com/twmb/go-sliceheap)\n\nExample\n-------\n\n```go\n// we can create heap in one function and return it.\ninner := func() sliceheap.Heap[int] {\n\ta := []int{3, 2, 4, 5, 1, 0, 6}\n\th := sliceheap.On(\u0026a, func(i, j int) bool { return a[i] \u003e a[j] })\n\theap.Init(h)\n\treturn h\n}\nh := inner()\n// We can see the heap sort itself by checking the backing slice.\nfmt.Println(h.View()) // prints [6 5 4 2 1 0 3]\n\nheap.Push(h, 8) // push a few more elements...\n\n// If we want to observe pushes and pops from the slice, we must save a\n// pointer to the slice. This is only necessary if working on a slice\n// that you did not pass directly to sliceheap.On, i.e., if you have lost\n// the original pointer you used.\nptr := h.Pointer()\nfmt.Println(*ptr) // prints [8 6 4 5 1 0 3 2]\nheap.Push(h, 7)\nfmt.Println(*ptr) // prints [8 7 4 6 1 0 3 2 5]\n\nheap.Push(h, 7)\nheap.Push(h, 9)\n// Pop everything off, printing as we pop largest to smallest.\nfor h.Len() \u003e 0 {\n\tlargest := heap.Pop(h).(int)\n\tfmt.Println(largest) // prints 9, 8, 7...\n}\n```\n\nBenchmarks\n----------\n\nThe following benchmark shows creating a slice of N nodes, and then constantly\npushing a new largest element onto it and popping the smallest. The new largest\nelement constantly sifts to the bottom, then smallest sifts out to be popped.\n\nOn go1.20.2\n\n```\nBenchmarkPushPop/1_nodes-8         \t18511695\t        65.84 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/2_nodes-8         \t17404094\t        68.47 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/4_nodes-8         \t15398985\t        77.69 ns/op\t      48 B/op\t       3 allocs/op\nBenchmarkPushPop/8_nodes-8         \t13573598\t        87.86 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/16_nodes-8        \t11884834\t        99.30 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/32_nodes-8        \t10706637\t       111.5 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/64_nodes-8        \t 9599702\t       125.0 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/128_nodes-8       \t 8897252\t       135.6 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/256_nodes-8       \t 8381521\t       146.2 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/512_nodes-8       \t 8005512\t       153.4 ns/op\t      47 B/op\t       3 allocs/op\nBenchmarkPushPop/1024_nodes-8      \t 7690998\t       162.2 ns/op\t      47 B/op\t       3 allocs/op\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwmb%2Fgo-sliceheap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwmb%2Fgo-sliceheap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwmb%2Fgo-sliceheap/lists"}