Giter Site home page Giter Site logo

mapset's Introduction

MapSet - 一个泛型的 Map Set 类型库

使用

go get -u github.com/joeescn/mapset

示例

package main

import (
 "fmt"

 "github.com/joeescn/mapset"
)

func main() {
 s := mapset.NewSet[int]()
 s.Add(1)
 clone := s.Clone()
 clone.Add(2)

 fmt.Println(clone.IsSuperset(s), clone)
 // Output: true [1,2]

 m := mapset.NewMap[string, string]()
 m.Set("key", "value")
 fmt.Println(m.Get("key"))
 // Output: value true

}

注意

该实现不是线程安全的,如果需要需要线程安全,可以通过外部加锁实现。

package main

import (
 "log"
 "math/rand"
 "runtime"
 "sync"

 "github.com/joeescn/mapset"
)

func main() {
 runtime.GOMAXPROCS(2)
 ints := rand.Perm(1000)

 s := mapset.NewSet[int]()
 lock := sync.Mutex{}

 var wg sync.WaitGroup
 wg.Add(len(ints))
 for i := 0; i < len(ints); i++ {
  go func(i int) {
   lock.Lock()
   defer lock.Unlock()
   s.Add(i)
   wg.Done()
  }(i)
 }

 wg.Wait()
 for _, i := range ints {
  if !s.Contains(i) {
   log.Fatalf("Set is missing element: %v", i)
  }
 }
}

mapset's People

Contributors

ohva avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.