elliotchance / orderedmap

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

Need GetElement method #15

Open jyotisj opened 4 years ago

jyotisj commented 4 years ago

Hi Elliot,

Thank you so much for implementing an awesome module. Your orderedmap module has helped me in two projects.

One of my project's requirement is to get previous and next values when a specific key is provided. Next() and Prev() are implemented for Element. However there is no GetElement method to get a specific Element when a key if provided.

How about adding a GetElement method in orderedmap package?

// GetElement returns the element for a key. If the key does not exist, the // second return parameter will be false and the pointer will be nil. func (m OrderedMap) GetElement(key interface{}) (Element, bool) { value, ok := m.kv[key] if ok { element := value.Value.(*orderedMapElement) return &Element{ element: value, Key: element.key, Value: element.value, }, true }

return nil, false

}

I am currently writing tests for the GetElement method. Thank you for writing detailed tests for the Get method. I'm following those to write tests for GetElement.

If you are fine with adding GetElement method then I'll create a pull request.

Thank you, Jyoti

elliotchance commented 4 years ago

Hi @jyotisj, I had to think about it for a bit. My gut tells me that this may cause unexpected behaviour if you start copying the element outside of its iterator implementation (in case it changes). However, this library gives no guarantees on concurrent use, so it should be OK.

jyotisj commented 4 years ago

Thanks, Elliot. I'll submit the pull request shortly.