elliotchance / orderedmap

🔃 An ordered map in Go with amortized O(1) for Set, Get, Delete and Len.
MIT License
849 stars 63 forks source link

Initialising an ordered map with data #43

Open pd93 opened 2 months ago

pd93 commented 2 months ago

Hi. I've been looking at using this in go-task instead of our naive implementation. Would you be open to a PR that added the ability to easily create an ordered map containing a set of initial data? since OrderedMap.kv and OrderedMap.ll are not exported, it is not possible to create an inline structure. I've been using .Set() for this instead, but this can be quite cumbersome when writing table-tests or similar.

I was thinking something along the lines of:

func NewOrderedMapWithElements[K comparable, V any](els ...*Element[K, V]) *OrderedMap[K, V] {
    om := &OrderedMap[K, V]{
        kv: make(map[K]*Element[K, V], len(els)),
    }
    for _, el := range els {
        om.Set(el.Key, el.Value)
    }
    return om
}

Or perhaps something using the functional options pattern if you prefer. If you're open to this, I'm happy to open a PR.

elliotchance commented 2 months ago

Sure, I'll be happy to access that as a PR.