Golang Implementing Set Sets

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Package Setimport ("bytes" "FMT") func main () {}type HashSet struct {m map[interface{}]bool}func newhashset () *hashset {ret Urn &hashset{m:make (map[interface{}]bool)}}//Add true Add succeeded False add failed func (set *hashset) Add (E interface{}) (b bool {if!set.m[e] {set.m[e] = Truereturn true}return false}//remove func (set *hashset) remove (e interface{}) {Delete (SET.M, E)}/  /clear Func (set *hashset) Clear () {set.m = make (Map[interface{}]bool)}//contains func (set *hashset) Contains (e interface{}) bool {return set.m[e]}//gets the number of elements func (set *hashset) len () int {return Len (SET.M)}//determines two sets when the same//true same false func (set *hash Set) Same (other *hashset) bool {if-other = nil {return false}if set. Len ()! = other. Len () {return false}for k, V: = Range set.m {if!other. Contains (k) {return False}}return true}//iteration func (set *hashset) Elements () []interface{} {initlen: = Len (set.m) Snaphot: = M Ake ([]interface{}, Initlen) Actuallen: = 0for k, V: = Range SET.M {if Actuallen < Initlen {Snaphot[actuallen] = k} else {Snaphot= Append (Snaphot, k)}actuallen++}if Actuallen < Initlen {snaphot = Snaphot[:actuallen]}return snaphot}//gets its own string func ( Set *hashset) string () string {var buf bytes. Bufferbuf.writestring ("set{") First: = Truefor k, V: = Range SET.M {If first {first = false} else {buf. WriteString ("")}buf. WriteString (FMT. Sprintf ("%v", K))}buf. WriteString ("}") return BUF. String ()}

 

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.