Top Related Projects
B-tree implementation for Go
GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more
A collection of useful, performant, and threadsafe Go datastructures.
Quick Overview
Error generating quick overview
Competitor Comparisons
B-tree implementation for Go
Pros of btree
- Implements a B-tree data structure, offering better performance for large datasets
- Supports both in-memory and on-disk storage options
- Provides more advanced features like range queries and iteration
Cons of btree
- More complex implementation, potentially harder to understand and maintain
- May have higher memory overhead for smaller datasets
- Less straightforward API compared to orderedmap
Code Comparison
orderedmap:
m := orderedmap.NewOrderedMap()
m.Set("key", "value")
value, _ := m.Get("key")
btree:
tr := btree.New(2)
tr.Set("key", "value")
value, _ := tr.Get("key")
Both libraries provide similar basic functionality for key-value storage, but btree offers more advanced features and potentially better performance for larger datasets. orderedmap focuses on simplicity and maintaining insertion order, while btree provides a more robust and scalable solution for complex use cases. The choice between the two depends on specific project requirements, dataset size, and desired features.
GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more
Pros of gods
- Offers a wider variety of data structures (trees, lists, stacks, queues, etc.)
- More comprehensive documentation and examples
- Higher number of stars and contributors, indicating a larger community
Cons of gods
- May be overkill for projects that only need an ordered map
- Potentially higher memory footprint due to the inclusion of multiple data structures
- Slightly more complex API due to the broader scope of the library
Code Comparison
orderedmap:
m := orderedmap.NewOrderedMap()
m.Set("key", "value")
value, _ := m.Get("key")
gods:
m := treemap.NewWithStringComparator()
m.Put("key", "value")
value, _ := m.Get("key")
Both libraries provide similar functionality for ordered maps, but gods offers a broader range of data structures and utilities. orderedmap is more focused and may be simpler to use for projects that only require an ordered map implementation. The choice between the two depends on the specific needs of your project and whether you require additional data structures beyond an ordered map.
A collection of useful, performant, and threadsafe Go datastructures.
Pros of go-datastructures
- Offers a wider variety of data structures, including trees, queues, and sets
- Provides concurrent implementations for better performance in multi-threaded environments
- More actively maintained with regular updates and contributions
Cons of go-datastructures
- Larger codebase and dependencies, potentially increasing complexity
- May have a steeper learning curve due to the broader range of structures
- Not specifically optimized for ordered maps, which is the focus of orderedmap
Code Comparison
orderedmap:
m := orderedmap.NewOrderedMap()
m.Set("key", "value")
value, exists := m.Get("key")
go-datastructures:
tree := btree.New(2)
tree.Insert(btree.Int(1), "value")
value, exists := tree.Get(btree.Int(1))
Summary
go-datastructures offers a comprehensive collection of data structures with concurrent implementations, making it suitable for complex applications. orderedmap, on the other hand, provides a simpler, focused solution for maintaining order in key-value pairs. The choice between the two depends on the specific requirements of your project, such as the need for additional data structures, concurrency support, and the importance of simplicity versus feature richness.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ưĀĀĀ github.com/elliotchance/orderedmap/v2 
Basic Usage
An *OrderedMap is a high performance ordered map that maintains amortized O(1)
for Set, Get, Delete and Len:
import "github.com/elliotchance/orderedmap/v2"
func main() {
m := orderedmap.NewOrderedMap[string, any]()
m.Set("foo", "bar")
m.Set("qux", 1.23)
m.Set("123", true)
m.Delete("qux")
}
Note: v2 requires Go v1.18 for generics. If you need to support Go 1.17 or below, you can use v1.
Internally an *OrderedMap uses the composite type
map combined with a
trimmed down linked list to maintain the order.
Iterating
Be careful using Keys() as it will create a copy of all of the keys so it's
only suitable for a small number of items:
for _, key := range m.Keys() {
value, _:= m.Get(key)
fmt.Println(key, value)
}
For larger maps you should use Front() or Back() to iterate per element:
// Iterate through all elements from oldest to newest:
for el := m.Front(); el != nil; el = el.Next() {
fmt.Println(el.Key, el.Value)
}
// You can also use Back and Prev to iterate in reverse:
for el := m.Back(); el != nil; el = el.Prev() {
fmt.Println(el.Key, el.Value)
}
In case you're using Go 1.23, you can also iterate with
range by using Iterator() or
ReverseIterator() methods:
for key, value := range m.Iterator() {
fmt.Println(key, value)
}
for key, value := range m.ReverseIterator() {
fmt.Println(key, value)
}
The iterator is safe to use bidirectionally, and will return nil once it goes
beyond the first or last item.
If the map is changing while the iteration is in-flight it may produce unexpected behavior.
Top Related Projects
B-tree implementation for Go
GoDS (Go Data Structures) - Sets, Lists, Stacks, Maps, Trees, Queues, and much more
A collection of useful, performant, and threadsafe Go datastructures.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot