Подмножества в golang
Всем привет. Создать множество можно и заполнить можно так
func main() {
m := make(map[int]bool, 0)
for i := 0; i < 20; i++ {
m[i] = true
}
log.Println(m)
}
Так ли это и если да, то как создать для множества подмножество
Нужно что-тотипа такого
множество[подмножество1{член1, член2, член3...}, подмножество2{член1, член2, член3 ...}...etc]
package main
import (
"math"
"sync"
)
type Set struct {
sync.Mutex
mp map[*SubSet]bool
}
type SubSet struct {
sync.Mutex
mp map[float64]bool
}
func NewSet() *Set {
return &Set{
mp: map[*SubSet]bool{},
}
}
func NewSubSet() *SubSet {
return &SubSet{
mp: map[float64]bool{},
}
}
func (s *SubSet) InsertInSubset(key float64) *SubSet {
s.Lock()
defer s.Unlock()
s.mp[key] = true
return s
}
func (s *Set) InsertInSet(key *SubSet, value bool) *Set {
s.Lock()
defer s.Unlock()
s.mp[key] = true
return s
}
func CelcionSet(celcions []float64) {
celcions = QS(celcions)
lencelcions := len(celcions)
set := NewSet()
for i := 0; i < lencelcions; i++ {
rng := math.Trunc(celcions[i]/10) * 10
sub := NewSubSet()
set.InsertInSet(sub.InsertInSubset(celcions[i]), true)//это конечно же не сработает, но не понимаю как заставить работать чтобы в множество вставлялось подмножество
}
}
func QS(nums []float64) []float64 {
// find len of arr
lennums := len(nums)
// if arr contains less then 2 items, nothing to sort
if lennums < 2 {
return nums
}
toehold := nums[0] //or nums[lennums/2]
// items which less than toehold
left := make([]float64, 0)
// items which more than toehold
right := make([]float64, 0)
for _, num := range nums[1:] {
// move to right if item more than toehold
if num > toehold {
right = append(right, num)
} else {
// move to left if item less than toehold
left = append(left, num)
}
}
// recoursive sorting left side
nums = append(QS(left), toehold)
// recoursive sorting right side
nums = append(nums, QS(right)...)
return nums
}
Ответы (1 шт):
Автор решения: Alexander Pavlov
→ Ссылка
package main
import (
"fmt"
"math"
)
func main() {
s := NewSet()
s.Insert(-18.1)
s.Insert(-11.2)
s.Insert(23.6)
s.Insert(22.5)
s.Insert(28.3)
s.Insert(28.3)
fmt.Println(s.GetAll())
fmt.Println(s.GetSubsetFor(-10))
}
type measurement int64
type subsetId int
type Set struct {
subsets map[subsetId]*Subset
}
type Subset struct {
elements map[measurement]int
}
func NewSet() *Set {
return &Set{
subsets: map[subsetId]*Subset{},
}
}
const roundFactor = 10
func toStorage(val float64) (subsetId, measurement) {
m := measurement(math.Round(val* roundFactor))
id := subsetId((int(val) / 10 ) * 10)
return id, m
}
func fromStorage(m measurement, count int) []float64 {
val := float64(m) / float64(roundFactor)
var vals []float64
for i:=0; i < count; i++ {
vals = append(vals, val)
}
return vals
}
func (s *Set) Insert(val float64) {
id, m := toStorage(val)
if ss, found := s.subsets[id]; !found {
s.subsets[id] = &Subset{
elements: map[measurement]int{m: 1},
}
} else {
ss.elements[m]++
}
}
func (s *Set) DeleteOne(val float64) {
id, m := toStorage(val)
if ss, found := s.subsets[id]; found {
ss.elements[m]--
}
}
func (s *Set) DeleteAll(val float64) {
id, m := toStorage(val)
if ss, found := s.subsets[id]; found {
delete(ss.elements, m)
}
}
func (s *Set) Contains(val float64) bool {
id, m := toStorage(val)
if ss, found := s.subsets[id]; found {
if c, found := ss.elements[m]; found && c > 0 {
return true
}
}
return false
}
func (s *Set) GetSubsetFor(val float64) []float64 {
id, _ := toStorage(val)
if ss, found := s.subsets[id]; found {
var vals []float64
for m, count := range ss.elements {
vals = append(vals, fromStorage(m,count)...)
}
return vals
}
return nil
}
func (s *Set) GetAll() []float64 {
var vals []float64
for _,ss := range s.subsets {
for m, count := range ss.elements {
vals = append(vals, fromStorage(m,count)...)
}
}
return vals
}