On Wed, Jan 10, 2018 at 7:57 AM,  <l...@pinkfroot.com> wrote:
> I'm trying to come up with a data structure that I can search for map values
> quickly without having to iterate an entire (and large) map to find a value.
>
> Essentially I have a map that contains lots of data and the key is the
> primary identifier where most of the lookups go to.  This is very quick to
> lookup and/or fail.  I also have some very frequent queries to search for
> values inside the map.  This is slow at the moment as I have to iterate the
> entire map to look for a value.
>
> What I am trying to do is to create a "shadow" map that has a reference to
> the pointer.  This way when the original is deleted from the primary map it
> should/will fall off from the shadow map.  However as you cannot set a
> struct to nil this doesn't seem to work.

One way of doing this could be:

type SomeStruct struct {
   Field1 string
   Field2 string
   ...
}

type SomeStructMap struct {
    f1Map map[string]*SomeStruct
    f2Map map[string]*SomeStruct
}

func (m SomeStructMap) GetByF1(s string) (*SomeStruct,bool) {return m.f1Map[s]}
func (m SomeStructMap( GetByF2(s string) (*SomeStruct,bool) {return m.f2Map[s]}
func (m SomeStructMap) Set(x *SomeStruct) {
  m.f1Map[x.Field1]=x
  m.f2Map[x.Field2]=x
}

Then use instances of SomeStructMap to store and lookup data.

>
> https://play.golang.org/p/xUP-LEruwVX
>
> Any ideas on how I could approach this?
>
> Thanks!!
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to