{"id":13411654,"url":"https://github.com/gammazero/deque","last_synced_at":"2025-05-13T19:03:45.157Z","repository":{"id":37851879,"uuid":"130787782","full_name":"gammazero/deque","owner":"gammazero","description":"Fast ring-buffer deque (double-ended queue)","archived":false,"fork":false,"pushed_at":"2025-03-17T17:46:12.000Z","size":82,"stargazers_count":673,"open_issues_count":6,"forks_count":59,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-04-28T00:42:19.574Z","etag":null,"topics":["circular-buffer","circular-queue","deque","queue","ring-buffer"],"latest_commit_sha":null,"homepage":"","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/gammazero.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":"2018-04-24T02:57:55.000Z","updated_at":"2025-04-26T07:51:00.000Z","dependencies_parsed_at":"2024-01-30T04:06:18.154Z","dependency_job_id":"c967e17a-c096-4053-85a5-21762fc045c7","html_url":"https://github.com/gammazero/deque","commit_stats":{"total_commits":56,"total_committers":5,"mean_commits":11.2,"dds":0.5178571428571428,"last_synced_commit":"610179f8334623677273c5dc8c63ec699e263c0e"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gammazero%2Fdeque","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gammazero%2Fdeque/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gammazero%2Fdeque/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gammazero%2Fdeque/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gammazero","download_url":"https://codeload.github.com/gammazero/deque/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254010793,"owners_count":21998993,"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":["circular-buffer","circular-queue","deque","queue","ring-buffer"],"created_at":"2024-07-30T20:01:15.442Z","updated_at":"2025-05-13T19:03:45.134Z","avatar_url":"https://github.com/gammazero.png","language":"Go","funding_links":[],"categories":["Data Integration Frameworks","数据结构与算法","Data Structures and Algorithms","Data Structures","Go","数据结构","Generators","数据结构`go语言实现的数据结构与算法`","Uncategorized"],"sub_categories":["Queues","队列","Advanced Console UIs","Standard CLI","标准 CLI"],"readme":"# deque\n\n[![GoDoc](https://pkg.go.dev/badge/github.com/gammazero/deque)](https://pkg.go.dev/github.com/gammazero/deque)\n[![Build Status](https://github.com/gammazero/deque/actions/workflows/go.yml/badge.svg)](https://github.com/gammazero/deque/actions/workflows/go.yml)\n[![Go Report Card](https://goreportcard.com/badge/github.com/gammazero/deque)](https://goreportcard.com/report/github.com/gammazero/deque)\n[![codecov](https://codecov.io/gh/gammazero/deque/branch/master/graph/badge.svg)](https://codecov.io/gh/gammazero/deque)\n[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)\n\nFast ring-buffer deque ([double-ended queue](https://en.wikipedia.org/wiki/Double-ended_queue)) implementation.\n\nFor a pictorial description, see the [Deque diagram](https://github.com/gammazero/deque/wiki)\n\n## Installation\n\n```\n$ go get github.com/gammazero/deque\n```\n\n## Deque data structure\n\nDeque generalizes a queue and a stack, to efficiently add and remove items at either end with O(1) performance. [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) (FIFO) operations are supported using `PushBack` and `PopFront`. [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) (LIFO) operations are supported using `PushBack` and `PopBack`.\n\n## Ring-buffer Performance\n\nThis deque implementation is optimized for CPU and GC performance. The circular buffer automatically re-sizes by powers of two, growing when additional capacity is needed and shrinking when only a quarter of the capacity is used, and uses bitwise arithmetic for all calculations. Since growth is by powers of two, adding elements will only cause O(log n) allocations. A base capacity can be set, with `SetBaseCap`, so that there is no resizing at or below that specified amount. The Deque can also be grown, using `Grow`, to ensure sufficient storage for n additional items, to prevent resizing when adding a number of itmes.\n\nThe ring-buffer implementation improves memory and time performance with fewer GC pauses, compared to implementations based on slices or linked lists. By wrapping around the buffer, previously used space is reused, making allocation unnecessary until all buffer capacity is used. The ring buffer implementation performs best when resizes are infrequest, as is the case when items moving in and out of the Deque are balanced or when the base capacity is large enough to rarely require a resize.\n\nFor maximum speed, this deque implementation leaves concurrency safety up to the application to provide, however the application chooses, if needed at all.\n\n## Reading Empty Deque\n\nSince it is OK for the deque to contain a `nil` value, it is necessary to either panic or return a second boolean value to indicate the deque is empty, when reading or removing an element. This deque panics when reading from an empty deque. This is a run-time check to help catch programming errors, which may be missed if a second return value is ignored. Simply check `Deque.Len()` before reading from the deque.\n\n## Generics\n\nDeque uses generics to create a Deque that contains items of the type specified. To create a Deque that holds a specific type, provide a type argument with the `Deque` variable declaration. For example:\n```go\n    stringDeque := new(deque.Deque[string])\n    var intDeque deque.Deque[int]\n```\n\n## Example\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/gammazero/deque\"\n)\n\nfunc main() {\n    var q deque.Deque[string]\n    q.PushBack(\"foo\")\n    q.PushBack(\"bar\")\n    q.PushBack(\"baz\")\n\n    fmt.Println(q.Len())   // Prints: 3\n    fmt.Println(q.Front()) // Prints: foo\n    fmt.Println(q.Back())  // Prints: baz\n\n    q.PopFront() // remove \"foo\"\n    q.PopBack()  // remove \"baz\"\n\n    q.PushFront(\"hello\")\n    q.PushBack(\"world\")\n\n    // Consume deque and print elements.\n    for q.Len() != 0 {\n        fmt.Println(q.PopFront())\n    }\n}\n```\n\n## Uses\n\nDeque can be used as both a:\n- [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) using `PushBack` and `PopFront`\n- [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) using `PushBack` and `PopBack`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgammazero%2Fdeque","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgammazero%2Fdeque","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgammazero%2Fdeque/lists"}