Go ordered map

284 views
Skip to first unread message

Tong Sun

unread,
Aug 15, 2017, 8:17:29 PM8/15/17
to golang-nuts
Sorry if this  topic has been discussed, but I'm looking for a Go ordered map implementation. 

I've found 

etc

But none seems to be being maintained. 

Anyone has used or can recommend a good Go ordered map implementation? 

Thx

Michael Jones

unread,
Aug 15, 2017, 9:08:12 PM8/15/17
to Tong Sun, golang-nuts
As a curiosity, is an ordered map really a hashtable or is it a different kind of self-organizing search structure? I would look to LLRB trees if order is an issue... (do a Google search for "Sedgewick LLRB") or start here:


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Michael T. Jones
michae...@gmail.com

Egon

unread,
Aug 16, 2017, 7:20:40 AM8/16/17
to golang-nuts
Do you mean ordered map or sorted map... either way, as long as the N is small, then you can use a slice to provide the extra functionality:

Tong Sun

unread,
Aug 16, 2017, 9:20:21 AM8/16/17
to golang-nuts
Oh, thanks a lot Egon, 

Your neat and elegant code is very helpful for me to get the idea and get started. 

Just FTA, in case someone is also reading this, more onto sorting the slice, here is my archive from this list:


I want to write three different go functions that sorts based on age, name and sal.


The traditional way to achieve this in go (as showcased here and here) is, to :


- declare a named type of slice

- implement 3 methods Len, Less, Swap

- call sort.Sort


(each attribute age, name, sal, needs its own type declaration and 3 method implementations).


Since Go 1.8, we can achieve basically the same with much less boilerplate code (as showcased here and here) :

- implement 1 func "less" having type  func(i, j int) bool

- call sort.Slice


(each attribute age, name, sal, needs its own "less" func).


I suggest the latter.

HTH

Valentin

Tong Sun

unread,
Aug 16, 2017, 9:29:31 AM8/16/17
to golang-nuts


On Wednesday, August 16, 2017 at 9:20:21 AM UTC-4, Tong Sun wrote:
Oh, thanks a lot Egon, 

Your neat and elegant code is very helpful for me to get the idea and get started. 

Just FTA, in case someone is also reading this, more onto sorting the slice, here is my archive from this list:

That's irrelevant to Egon's solution. Just to make it clear. 

Sokolov Yura

unread,
Aug 17, 2017, 5:14:51 AM8/17/17
to golang-nuts
I've implemented AVL tree as index for sort.Interface: https://github.com/funny-falcon/go-tree
(It is not strictly index, cause it could Swap elements).

So it could be used in conjunction with same type that is defined for sort.Sort .

среда, 16 августа 2017 г., 3:17:29 UTC+3 пользователь Tong Sun написал:
Reply all
Reply to author
Forward
0 new messages