{"id":13411483,"url":"https://github.com/Workiva/go-datastructures","last_synced_at":"2025-03-14T17:30:54.152Z","repository":{"id":22581622,"uuid":"25923208","full_name":"Workiva/go-datastructures","owner":"Workiva","description":"A collection of useful, performant, and threadsafe Go datastructures.","archived":false,"fork":false,"pushed_at":"2024-05-16T15:36:12.000Z","size":8607,"stargazers_count":7661,"open_issues_count":30,"forks_count":834,"subscribers_count":324,"default_branch":"master","last_synced_at":"2024-10-25T04:09:49.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Workiva.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":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-10-29T13:55:17.000Z","updated_at":"2024-10-24T07:30:45.000Z","dependencies_parsed_at":"2024-05-16T16:28:55.298Z","dependency_job_id":"cf1b968f-5b17-42a8-97dd-12ec0cf73b37","html_url":"https://github.com/Workiva/go-datastructures","commit_stats":{"total_commits":518,"total_committers":47,"mean_commits":11.02127659574468,"dds":0.5038610038610039,"last_synced_commit":"c466da296827daa1e1efba14c912e2802533fe7f"},"previous_names":[],"tags_count":60,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workiva%2Fgo-datastructures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workiva%2Fgo-datastructures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workiva%2Fgo-datastructures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Workiva%2Fgo-datastructures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Workiva","download_url":"https://codeload.github.com/Workiva/go-datastructures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243618633,"owners_count":20320269,"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-07-30T20:01:13.999Z","updated_at":"2025-03-14T17:30:53.377Z","avatar_url":"https://github.com/Workiva.png","language":"Go","readme":"go-datastructures\n=================\n\nGo-datastructures is a collection of useful, performant, and threadsafe Go\ndatastructures.\n\n### NOTE: only tested with Go 1.3+.\n\n#### Augmented Tree\n\nInterval tree for collision in n-dimensional ranges.  Implemented via a\nred-black augmented tree.  Extra dimensions are handled in simultaneous\ninserts/queries to save space although this may result in suboptimal time\ncomplexity.  Intersection determined using bit arrays.  In a single dimension,\ninserts, deletes, and queries should be in O(log n) time.\n\n#### Bitarray\n\nBitarray used to detect existence without having to resort to hashing with\nhashmaps.  Requires entities have a uint64 unique identifier.  Two\nimplementations exist, regular and sparse.  Sparse saves a great deal of space\nbut insertions are O(log n).  There are some useful functions on the BitArray\ninterface to detect intersection between two bitarrays. This package also\nincludes bitmaps of length 32 and 64 that provide increased speed and O(1) for\nall operations by storing the bitmaps in unsigned integers rather than arrays.\n\n#### Futures\n\nA helpful tool to send a \"broadcast\" message to listeners.  Channels have the\nissue that once one listener takes a message from a channel the other listeners\naren't notified.  There were many cases when I wanted to notify many listeners\nof a single event and this package helps.\n\n#### Queue\n\nPackage contains both a normal and priority queue.  Both implementations never\nblock on send and grow as much as necessary.  Both also only return errors if\nyou attempt to push to a disposed queue and will not panic like sending a\nmessage on a closed channel.  The priority queue also allows you to place items\nin priority order inside the queue.  If you give a useful hint to the regular\nqueue, it is actually faster than a channel.  The priority queue is somewhat\nslow currently and targeted for an update to a Fibonacci heap.\n\nAlso included in the queue package is a MPMC threadsafe ring buffer. This is a\nblock full/empty queue, but will return a blocked thread if the queue is\ndisposed while a thread is blocked.  This can be used to synchronize goroutines\nand ensure goroutines quit so objects can be GC'd.  Threadsafety is achieved\nusing only CAS operations making this queue quite fast.  Benchmarks can be found\nin that package.\n\n#### Fibonacci Heap\n\nA standard Fibonacci heap providing the usual operations. Can be useful in executing Dijkstra or Prim's algorithms in the theoretically minimal time. Also useful as a general-purpose priority queue. The special thing about Fibonacci heaps versus other heap variants is the cheap decrease-key operation. This heap has a constant complexity for find minimum, insert and merge of two heaps, an amortized constant complexity for decrease key and O(log(n)) complexity for a deletion or dequeue minimum. In practice the constant factors are large, so Fibonacci heaps could be slower than Pairing heaps, depending on usage. Benchmarks - in the project subfolder. The heap has not been designed for thread-safety.\n\n#### Range Tree\n\nUseful to determine if n-dimensional points fall within an n-dimensional range.\nNot a typical range tree however, as we are actually using an n-dimensional\nsorted list of points as this proved to be simpler and faster than attempting a\ntraditional range tree while saving space on any dimension greater than one.\nInserts are typical BBST times at O(log n^d) where d is the number of\ndimensions.\n\n#### Set\nOur Set implementation is very simple, accepts items of type `interface{}` and\nincludes only a few methods. If your application requires a richer Set\nimplementation over lists of type `sort.Interface`, see\n[xtgo/set](https://github.com/xtgo/set) and\n[goware/set](https://github.com/goware/set).\n\n#### Threadsafe\nA package that is meant to contain some commonly used items but in a threadsafe\nway.  Example: there's a threadsafe error in there as I commonly found myself\nwanting to set an error in many threads at the same time (yes, I know, but\nchannels are slow).\n\n#### AVL Tree\n\nThis is an example of a branch copy immutable AVL BBST.  Any operation on a node\nmakes a copy of that node's branch.  Because of this, this tree is inherently\nthreadsafe although the writes will likely still need to be serialized.  This\nstructure is good if your use case is a large number of reads and infrequent\nwrites as reads will be highly available but writes somewhat slow due to the\ncopying.  This structure serves as a basis for a large number of functional data\nstructures.\n\n#### X-Fast Trie\n\nAn interesting design that treats integers as words and uses a trie structure to\nreduce time complexities by matching prefixes.  This structure is really fast\nfor finding values or making predecessor/successor types of queries, but also\nresults in greater than linear space consumption.  The exact time complexities\ncan be found in that package.\n\n#### Y-Fast Trie\n\nAn extension of the X-Fast trie in which an X-Fast trie is combined with some\nother ordered data structure to reduce space consumption and improve CRUD types\nof operations.  These secondary structures are often BSTs, but our implementation\nuses a simple ordered list as I believe this improves cache locality.  We also\nuse fixed size buckets to aid in parallelization of operations.  Exact time\ncomplexities are in that package.\n\n#### Fast integer hashmap\n\nA datastructure used for checking existence but without knowing the bounds of\nyour data.  If you have a limited small bounds, the bitarray package might be a\nbetter choice.  This implementation uses a fairly simple hashing algorithm\ncombined with linear probing and a flat datastructure to provide optimal\nperformance up to a few million integers (faster than the native Golang\nimplementation).  Beyond that, the native implementation is faster (I believe\nthey are using a large -ary B-tree).  In the future, this will be implemented\nwith a B-tree for scale.\n\n#### Skiplist\n\nAn ordered structure that provides amortized logarithmic operations but without\nthe complication of rotations that are required by BSTs.  In testing, however,\nthe performance of the skip list is often far worse than the guaranteed log n\ntime of a BBST.  Tall nodes tend to \"cast shadows\", especially when large\nbitsizes are required as the optimum maximum height for a node is often based on\nthis.  More detailed performance characteristics are provided in that package.\n\n#### Sort\n\nThe sort package implements a multithreaded bucket sort that can be up to 3x\nfaster than the native Golang sort package.  These buckets are then merged using\na symmetrical merge, similar to the stable sort in the Golang package.  However,\nour algorithm is modified so that two sorted lists can be merged by using\nsymmetrical decomposition.\n\n#### Numerics\n\nEarly work on some nonlinear optimization problems.  The initial implementation\nallows a simple use case with either linear or nonlinear constraints.  You can\nfind min/max or target an optimal value.  The package currently employs a\nprobabilistic global restart system in an attempt to avoid local critical points.\nMore details can be found in that package.\n\n#### B+ Tree\n\nInitial implementation of a B+ tree.  Delete method still needs added as well as\nsome performance optimization.  Specific performance characteristics can be\nfound in that package.  Despite the theoretical superiority of BSTs, the B-tree\noften has better all around performance due to cache locality.  The current\nimplementation is mutable, but the immutable AVL tree can be used to build an\nimmutable version.  Unfortunately, to make the B-tree generic we require an\ninterface and the most expensive operation in CPU profiling is the interface\nmethod which in turn calls into runtime.assertI2T.  We need generics.\n\n#### Immutable B Tree\nA btree based on two principles, immutability and concurrency. \nSomewhat slow for single value lookups and puts, it is very fast for bulk operations.\nA persister can be injected to make this index persistent.\n\n#### Ctrie\n\nA concurrent, lock-free hash array mapped trie with efficient non-blocking\nsnapshots. For lookups, Ctries have comparable performance to concurrent skip\nlists and concurrent hashmaps. One key advantage of Ctries is they are\ndynamically allocated. Memory consumption is always proportional to the number\nof keys in the Ctrie, while hashmaps typically have to grow and shrink. Lookups,\ninserts, and removes are O(logn).\n\nOne interesting advantage Ctries have over traditional concurrent data\nstructures is support for lock-free, linearizable, constant-time snapshots.\nMost concurrent data structures do not support snapshots, instead opting for\nlocks or requiring a quiescent state. This allows Ctries to have O(1) iterator\ncreation and clear operations and O(logn) size retrieval.\n\n#### Dtrie\n\nA persistent hash trie that dynamically expands or shrinks to provide efficient\nmemory allocation. Being persistent, the Dtrie is immutable and any modification\nyields a new version of the Dtrie rather than changing the original. Bitmapped\nnodes allow for O(log32(n)) get, remove, and update operations. Insertions are\nO(n) and iteration is O(1).\n\n#### Persistent List\n\nA persistent, immutable linked list. All write operations yield a new, updated\nstructure which preserve and reuse previous versions. This uses a very\nfunctional, cons-style of list manipulation. Insert, get, remove, and size\noperations are O(n) as you would expect.\n\n#### Simple Graph\n\nA mutable, non-persistent undirected graph where parallel edges and self-loops are \nnot permitted. Operations to add an edge as well as retrieve the total number of \nvertices/edges are O(1) while the operation to retrieve the vertices adjacent to a\ntarget is O(n). For more details see [wikipedia](https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)#Simple_graph)\n\n### Installation\n\n 1. Install Go 1.3 or higher.\n 2. Run `go get github.com/Workiva/go-datastructures/...`\n\n### Updating\n\nWhen new code is merged to master, you can use\n\n\tgo get -u github.com/Workiva/go-datastructures/...\n\nTo retrieve the latest version of go-datastructures.\n\n### Testing\n\nTo run all the unit tests use these commands:\n\n\tcd $GOPATH/src/github.com/Workiva/go-datastructures\n\tgo get -t -u ./...\n\tgo test ./...\n\nOnce you've done this once, you can simply use this command to run all unit tests:\n\n\tgo test ./...\n\n\n### Contributing\n\nRequirements to commit here:\n\n - Branch off master, PR back to master.\n - `gofmt`'d code.\n - Compliance with [these guidelines](https://code.google.com/p/go-wiki/wiki/CodeReviewComments)\n - Unit test coverage\n - [Good commit messages](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)\n\n\n### Maintainers\n - Alexander Campbell \u003c[alexander.campbell@workiva.com](mailto:alexander.campbell@workiva.com)\u003e\n - Dustin Hiatt \u003c[dustin.hiatt@workiva.com](mailto:dustin.hiatt@workiva.com)\u003e\n - Ryan Jackson \u003c[ryan.jackson@workiva.com](mailto:ryan.jackson@workiva.com)\u003e\n","funding_links":[],"categories":["开源类库","Misc","Data Structures","Go","Data Structures and Algorithms","Open source library","0x11. Workiva/go-datastructures","数据结构与算法","数据结构`go语言实现的数据结构与算法`","数据结构","Uncategorized","Data Integration Frameworks","\u003cspan id=\"数据结构-data-structures\"\u003e数据结构 Data Structures\u003c/span\u003e","Generators"],"sub_categories":["数据结构","Advanced Console UIs","Standard CLI","Data Structure and Algorithm Collections","Data Structure","数据结构和算法集合","标准 CLI","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWorkiva%2Fgo-datastructures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FWorkiva%2Fgo-datastructures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWorkiva%2Fgo-datastructures/lists"}