GO-CONCURRENTMAP implementation

Source: Internet
Author: User
Tags sprintf
This is a creation in Article, where the information may have evolved or changed.

Implement Concurrentmap with Go

///generalization of the map's interface typetypeGenericmapInterface{//Gets the element value corresponding to the given key value. Nil is returned if there is no corresponding element value.        Get(KeyInterface{})Interface{}//Add a key-value pair and return the old element value corresponding to the given key value. Returns (nil, true) if no old element value is used.        Put(KeyInterface{}, ElemInterface{}) (Interface{}, BOOL)//Delete the key-value pair corresponding to the given key value, and return the old element value. Returns nil if no old element value is used.        Remove(KeyInterface{})Interface{}//Clears all key-value pairs.        Clear()//Gets the number of key-value pairs.        Len()int       //Determines whether the given key value is included.        Contains(KeyInterface{})BOOL       //Gets the slice value consisting of the sorted key value.        Keys() []Interface{}//Gets the slice value consisting of the sorted element value.        Elems() []Interface{}//Gets the dictionary value consisting of the key-value pairs that are already included.        Tomap()Map[Interface{}]Interface{}//Gets the type of the key.        KeyType() reflect. Type//Gets the type of the element.        Elemtype() reflect. Type}typeConcurrentmapInterface{GenericmapString()string}typeMyconcurrentmapstruct{mMap[Interface{}]Interface{} keyType reflect. Type elemtype reflect. Type Rwmutex sync. Rwmutex}func(CMAP *Myconcurrentmap)Get(KeyInterface{})Interface{} {Cmap.rwmutex.Rlock()deferCmap.rwmutex.Runlock()returnCmap.m[key]}func(CMAP *Myconcurrentmap)Isacceptablepair(k, EInterface{})BOOL{ifK = =Nil|| Reflect.TypeOf(k)! = Cmap.keytype {returnfalse       }offE = =Nil|| Reflect.TypeOf(e)! = Cmap.elemtype {returnfalse       }returntrue}func(CMAP *Myconcurrentmap)Put(KeyInterface{}, ElemInterface{}) (Interface{}, BOOL) {if!cmap.Isacceptablepair(Key, Elem) {returnNil, false       } Cmap.rwmutex.Lock()deferCmap.rwmutex.Unlock() Oldelem: = Cmap.m[key] cmap.m[key] = ElemreturnOldelem, true}func(CMAP *Myconcurrentmap)Remove(KeyInterface{})Interface{} {Cmap.rwmutex.Lock()deferCmap.rwmutex.Unlock() Oldelem: = Cmap.m[key]Delete(cmap.m, KeyreturnOldelem}func(CMAP *Myconcurrentmap)Clear() {Cmap.rwmutex.Lock()deferCmap.rwmutex.Unlock() cmap.m = Make(Map[Interface{}]Interface{})}func(CMAP *Myconcurrentmap)Len()int{Cmap.rwmutex.Rlock()deferCmap.rwmutex.Runlock()returnLen(CMAP.M)}func(CMAP *Myconcurrentmap)Contains(KeyInterface{})BOOL{Cmap.rwmutex.Rlock()deferCmap.rwmutex.Runlock()       _, OK: = Cmap.m[key]returnOkfunc(CMAP *Myconcurrentmap)Keys() []Interface{} {Cmap.rwmutex.Rlock()deferCmap.rwmutex.Runlock() Initiallen: =Len(cmap.m) Keys: = Make([]Interface{}, Initiallen) Index: =0        forK, _ :=Rangecmap.m {Keys[index] = k index++}returnKeysfunc(CMAP *Myconcurrentmap)Elems() []Interface{} {Cmap.rwmutex.Rlock()deferCmap.rwmutex.Runlock() Initiallen: =Len(cmap.m) Elems: = Make([]Interface{}, Initiallen) Index: =0        for_, V: =Rangecmap.m {Elems[index] = v index++}returnElemsfunc(CMAP *Myconcurrentmap)Tomap()Map[Interface{}]Interface{} {Cmap.rwmutex.Rlock()deferCmap.rwmutex.Runlock() Replica: = Make(Map[Interface{}]Interface{}) forK, V: =Rangecmap.m {Replica[k] = v}returnReplicafunc(CMAP *Myconcurrentmap)KeyType() reflect. Type {returnCmap.keytype}func(CMAP *Myconcurrentmap)Elemtype() reflect. Type {returnCmap.elemtype}func(CMAP *Myconcurrentmap)String()string{varbuf bytes. Buffer buf.writestring("concurrentmap<") buf.writestring(Cmap.keytype.Kind().String()) buf.writestring(",") buf.writestring(Cmap.elemtype.Kind().String()) buf.writestring(">{") First: =true        forK, V: =Rangecmap.m {ifFirst {first =false              }Else{buf.writestring(" ")} buf.writestring(FMT.Sprintf("%v", k)) buf.writestring(":") buf.writestring(FMT.Sprintf("%v", V)} buf.writestring("}")returnBuf.String()}funcNewconcurrentmap(KeyType, Elemtype reflect. Type) Concurrentmap {return&myconcurrentmap{Keytype:keytype,              Elemtype:elemtype,              M: Make(Map[Interface{}]Interface{})}}funcMain() {//var cmap concurrentmap = &myconcurrentmap{///M:make (map[interface{}]interface{}),//Keytype:reflect. TypeOf (int (2)),//Elemtype:reflect. TypeOf (String)}       CMAP: =Newconcurrentmap(Reflect.TypeOf(int(2)), Reflect.TypeOf(string( $)) Fmt.Println(CMAP) cmap.Put( the, " A") FMT.Println(CMAP.String())}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.