package container

type IDSet map[uint64]int

func NewIDSet() IDSet {
	return IDSet{}
}

func (s IDSet) Add(id uint64) int {
	s[id] += 1
	return s[id]
}

func (s IDSet) AddList(idList []uint64) {
	for _, x := range idList {
		s.Add(x)
	}
}

func (s IDSet) Remove(id uint64) (bool, int) {
	c, ok := s[id]
	if !ok {
		return false, 0
	}

	c--
	if c == 0 {
		delete(s, id)

	} else {
		s[id] = c
	}

	return true, c
}

func (s IDSet) RemoveList(idList []uint64) {
	for _, x := range idList {
		s.Remove(x)
	}
}

func (s IDSet) RemoveAll(id uint64) int {
	delete(s, id)
	return 0
}

func (s IDSet) MakeList() []uint64 {
	list := make([]uint64, 0, len(s))
	for id, count := range s {
		for i := 0; i < count; i++ {
			list = append(list, id)
		}
	}

	return list
}
