Skip to content

Commit

Permalink
refactor list
Browse files Browse the repository at this point in the history
  • Loading branch information
pilot committed Oct 9, 2023
1 parent 3ad969f commit 16dc9d2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
14 changes: 7 additions & 7 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ type Event struct {
base *EventBase

// ele is the element in the total event list.
ele *element
ele *element[*Event]
// activeEle is the element in the active event list.
activeEle *element
activeEle *element[*Event]
// index is the index in the event heap.
index int

Expand Down Expand Up @@ -167,9 +167,9 @@ type EventBase struct {
// poller is the event poller to watch events.
poller *poller
// evList is the list of all events.
evList *list
evList *list[*Event]
// activeEvList is the list of active events.
activeEvLists []*list
activeEvLists []*list[*Event]
// eventHeap is the min heap of timeout events.
evHeap *eventHeap
// nowTimeCache is the cache of now time.
Expand All @@ -185,8 +185,8 @@ func NewBase() (*EventBase, error) {

return &EventBase{
poller: poller,
evList: newList(),
activeEvLists: []*list{newList(), newList(), newList()},
evList: newList[*Event](),
activeEvLists: []*list[*Event]{newList[*Event](), newList[*Event](), newList[*Event]()},
evHeap: new(eventHeap),
nowTimeCache: time.Time{},
}, nil
Expand Down Expand Up @@ -300,7 +300,7 @@ func (bs *EventBase) handleActiveEvents() {
for i := range bs.activeEvLists {
for e := bs.activeEvLists[i].front(); e != nil; {
next := e.nextEle()
ev := e.value.(*Event)
ev := e.value
if ev.events&EvPersist != 0 {
bs.eventQueueRemove(ev, evListActive)
if ev.events&EvTimeout != 0 {
Expand Down
27 changes: 14 additions & 13 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,41 @@

package event

type element struct {
next, prev *element
list *list
value any
type element[T any] struct {
next, prev *element[T]
list *list[T]
value T
}

func (e *element) nextEle() *element {
func (e *element[T]) nextEle() *element[T] {
if p := e.next; e.list != nil && p != &e.list.root {
return p
}
return nil
}

type list struct {
root element
type list[T any] struct {
root element[T]
len int
}

func newList() *list {
l := new(list)
func newList[T any]() *list[T] {
l := new(list[T])
l.root.next = &l.root
l.root.prev = &l.root
l.len = 0
return l
}

func (l *list) front() *element {
func (l *list[T]) front() *element[T] {
if l.len == 0 {
return nil
}
return l.root.next
}

func (l *list) pushBack(v any) *element {
e := new(element)
func (l *list[T]) pushBack(v T) *element[T] {
e := new(element[T])
e.list = l
e.value = v
n := &l.root
Expand All @@ -50,7 +51,7 @@ func (l *list) pushBack(v any) *element {
return e
}

func (l *list) remove(e *element) {
func (l *list[T]) remove(e *element[T]) {
e.prev.next = e.next
e.next.prev = e.prev
e.next = nil
Expand Down

0 comments on commit 16dc9d2

Please sign in to comment.