fix some bugs, there is yet another bug

This commit is contained in:
MahnoKropotkinvich 2024-12-26 11:46:31 +08:00
parent 2174afc82c
commit bc58c9df5b

50
ds.go
View File

@ -58,6 +58,42 @@ const (
DELETE
)
type Queue[T any] struct {
items []T
}
func NewQueue[T any]() *Queue[T] {
return &Queue[T]{
items: make([]T, 0),
}
}
func (s *Queue[T]) Push(item T) {
s.items = append(s.items, item)
}
func (s *Queue[T]) Pop() T {
l := len(s.items)
if l == 0 {
panic("Stack: pop")
}
ret := s.items[0]
s.items = s.items[1:]
return ret
}
func (s Queue[T]) Peek() T {
l := len(s.items)
if l == 0 {
panic("Stack: peek")
}
return s.items[l]
}
func (s Queue[T]) IsEmpty() bool {
return len(s.items) == 0
}
func (s Queue[T]) Length() int {
return len(s.items)
}
type modify_t[T constraints.Ordered] struct {
op op
id int
@ -86,7 +122,7 @@ func NewDynamicMultiChan[T constraints.Ordered](reply bool, m int) *DynamicMulti
list := make([]chan T, 0)
selectCases := make([]reflect.SelectCase, 1)
viewCnt := 0
addChan := make(chan int, 1)
addQue := NewQueue[int]()
go func() {
for {
modification, _ := <-ret.modify
@ -104,7 +140,7 @@ func NewDynamicMultiChan[T constraints.Ordered](reply bool, m int) *DynamicMulti
Send: reflect.Value{},
}
if len(unused) == 0 {
addChan <- len(list)
addQue.Push(len(list))
used[len(list)] = true
list = append(list, ch)
selectCases = append(selectCases, nelem)
@ -114,7 +150,7 @@ func NewDynamicMultiChan[T constraints.Ordered](reply bool, m int) *DynamicMulti
which = x
break
}
addChan <- which
addQue.Push(which)
used[which] = true
delete(unused, which)
list[which] = ch
@ -179,14 +215,18 @@ func NewDynamicMultiChan[T constraints.Ordered](reply bool, m int) *DynamicMulti
}()
if mode == 2 {
go func() {
viewCnt++
msgList := make([]T, 0)
for {
select {
case <-reload:
// reloadRX <- struct{}{} //I'm currently not dealing with other chan
<-reload //waiting for you finished
delta, _ := <-addChan
if delta != -1 {
if addQue.Length() == 0 {
continue
}
for !addQue.IsEmpty() {
delta := addQue.Pop()
for _, x := range msgList {
list[delta] <- x
}