MCPcopy Index your code
hub / github.com/elliotchance/orderedmap

github.com/elliotchance/orderedmap @v3.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.1.0 ↗ · + Follow
389 symbols 1,188 edges 11 files 68 documented · 17% 38 cross-repo links updated 2mo agov3.1.0 · 2025-01-02★ 1,02415 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

🔃 github.com/elliotchance/orderedmap/v3 GoDoc

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/v3"

func main() {
    m := orderedmap.NewOrderedMap[string, any]()

    m.Set("foo", "bar")
    m.Set("qux", 1.23)
    m.Set("123", true)

    m.Delete("qux")
}

[!NOTE]

  • v3 requires Go v1.23 - If you need to support Go 1.18-1.22, you can use v2.
  • 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

The following methods all return iterators that can be used to loop over elements in an ordered map:

  • AllFromFront()
  • AllFromBack()
  • Keys()
  • Values()
// Iterate through all elements from oldest to newest:
for key, value := range m.AllFromFront() {
    fmt.Println(key, value)
}

Iterators are 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.

If you want to get a slice of the map keys or values, you can use the standard slices.Collect method with the iterator returned from Keys() or Values():

fmt.Println(slices.Collect(m.Keys())
// [A B C]

Likewise, calling maps.Collect on the iterator returned from AllFromFront() will create a regular unordered map from the ordered one:

fmt.Println(maps.Collect(m.AllFromFront())
// [A:1 B:2 C:3]

If you don't want to use iterators, you can also manually loop over the elements using Front() or Back() with Next():

// 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)
}

Core symbols most depended-on inside this repo

Shape

Function 313
Method 64
Struct 12

Languages

Go100%

Modules by API surface

v2/orderedmap_test.go103 symbols
v3/orderedmap_test.go102 symbols
orderedmap_test.go101 symbols
v3/orderedmap.go19 symbols
v2/orderedmap.go16 symbols
orderedmap.go14 symbols
v3/list.go10 symbols
v2/list.go10 symbols
list.go10 symbols
v2/iterator_test.go2 symbols
v2/iterator.go2 symbols

For agents

$ claude mcp add orderedmap \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page