I have struct for order list. I want order map
and find it way. But i don't want create this for all types, that i have. Maybe I need another code? Or how I can rewrite it for universal, that I can use it with all types, that have Prev
and Next
.
type Post struct { //for example
P string
O int
Prev, Next *Post
}
type Act struct {
I int
Prev, Next *Act
}
type Map struct {
Act map[int]*Act
First, Last *Act
}
func (m *Map) New(a *Act) int {
Ida++
f := Ida
a.Id = Ida
m.Act[Ida] = a
if m.First == nil {
m.First = a
} else {
m.Last.Next = a
a.Prev = m.Last
}
m.Last = a
return f
}
func (m *Map) Del(s int) {
if _, ok := m.Act[s]; ok {
if m.Last == m.First {
m.Last = nil
m.First = nil
delete(m.Act, s)
return
}
if m.Last == m.Act[s] {
m.Last = m.Act[s].Prev
m.Act[s].Prev.Next = nil
delete(m.Act, s)
return
}
if m.First == m.Act[s] {
m.First = m.Act[s].Next
m.Act[s].Next.Prev = nil
delete(m.Act, s)
return
}
m.Act[s].Prev.Next = m.Act[s].Next
m.Act[s].Next.Prev = m.Act[s].Prev
delete(m.Act, s)
return
}
}