| 1 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"./a" |
| 9 |
) |
| 10 |
|
| 11 |
// Creates copy of set |
| 12 |
func Copy[T comparable](src MapSet[T]) (dst MapSet[T]) { |
| 13 |
dst = HashSet[T](src.Len()) |
| 14 |
Fill(src, dst) |
| 15 |
return |
| 16 |
} |
| 17 |
|
| 18 |
// Fill src from dst |
| 19 |
func Fill[T any](src, dst MapSet[T]) { |
| 20 |
src.Iterate(func(t T) bool { |
| 21 |
dst.Add(t) |
| 22 |
return true |
| 23 |
}) |
| 24 |
return |
| 25 |
} |
| 26 |
|
| 27 |
type MapSet[T any] struct { |
| 28 |
m a.Map[T, struct{}] |
| 29 |
} |
| 30 |
|
| 31 |
func HashSet[T comparable](capacity int) MapSet[T] { |
| 32 |
return FromMap[T](a.NewHashMap[T, struct{}](capacity)) |
| 33 |
} |
| 34 |
|
| 35 |
func FromMap[T any](m a.Map[T, struct{}]) MapSet[T] { |
| 36 |
return MapSet[T]{ |
| 37 |
m: m, |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func (s MapSet[T]) Add(t T) { |
| 42 |
s.m.Put(t, struct{}{}) |
| 43 |
} |
| 44 |
|
| 45 |
func (s MapSet[T]) Len() int { |
| 46 |
return s.m.Len() |
| 47 |
} |
| 48 |
|
| 49 |
func (s MapSet[T]) Iterate(cb func(T) bool) { |
| 50 |
s.m.Iterate(func(p a.Pair[T, struct{}]) bool { |
| 51 |
return cb(p.L) |
| 52 |
}) |
| 53 |
} |
| 54 |
|
| 55 |
func main() { |
| 56 |
x := FromMap[int](a.NewHashMap[int, struct{}](1)) |
| 57 |
Copy[int](x) |
| 58 |
} |