mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2025-12-28 10:25:34 +01:00
span buffers: Simplified code by throwing away cases that cannot happen. Extended unit tests with more pattern to check.
This commit is contained in:
@@ -90,65 +90,33 @@ func (sp *SpanPool) Insert(s *Span, pos, value int32) *Span {
|
||||
}
|
||||
|
||||
head := s
|
||||
for prev := s; s != nil; s = s.Next {
|
||||
switch {
|
||||
case pos < s.From: // before span
|
||||
if pos == s.From-1 && value == s.Value { // directly neighbored
|
||||
s.From = pos
|
||||
// Check if a gap has to be closed.
|
||||
if prev.To == s.From-1 && value == prev.Value {
|
||||
prev.To = s.To
|
||||
prev.Next = s.Next
|
||||
sp.Free(s)
|
||||
return head
|
||||
}
|
||||
for ; s != nil && pos > s.To; s = s.Next {
|
||||
next := s.Next
|
||||
if pos == s.To+1 && value == s.Value { // directly neighbored
|
||||
s.To = pos
|
||||
// Check if a gap has to be closed
|
||||
if next != nil && next.From == s.To+1 && value == next.Value {
|
||||
s.To = next.To
|
||||
s.Next = next.Next
|
||||
sp.Free(next)
|
||||
}
|
||||
// Extend prev?
|
||||
if prev.To == pos-1 && value == prev.Value {
|
||||
prev.To = pos
|
||||
return head
|
||||
}
|
||||
// New between prev and current.
|
||||
return head
|
||||
}
|
||||
// Extend next?
|
||||
if next != nil && pos == next.From-1 && value == next.Value {
|
||||
next.From = pos
|
||||
return head
|
||||
}
|
||||
// Before next -> New between current and next
|
||||
if next == nil || pos < next.From {
|
||||
sn := sp.Alloc()
|
||||
sn.From = pos
|
||||
sn.To = pos
|
||||
sn.Value = value
|
||||
sn.Next = s
|
||||
prev.Next = sn
|
||||
return head
|
||||
|
||||
case pos > s.To: // after span
|
||||
next := s.Next
|
||||
if pos == s.To+1 && value == s.Value { // directly neighbored
|
||||
s.To = pos
|
||||
// Check if a gap has to be closed
|
||||
if next != nil && next.From == s.To+1 && value == next.Value {
|
||||
s.To = next.To
|
||||
s.Next = next.Next
|
||||
sp.Free(next)
|
||||
}
|
||||
return head
|
||||
}
|
||||
// Extend next?
|
||||
if next != nil && pos == next.From-1 && value == next.Value {
|
||||
next.From = pos
|
||||
return head
|
||||
}
|
||||
// Before next -> New between current and next
|
||||
if next == nil || pos < next.From {
|
||||
sn := sp.Alloc()
|
||||
sn.From = pos
|
||||
sn.To = pos
|
||||
sn.Value = value
|
||||
sn.Next = next
|
||||
s.Next = sn
|
||||
return head
|
||||
}
|
||||
|
||||
default: // pos in span -> do not modify.
|
||||
sn.Next = next
|
||||
s.Next = sn
|
||||
return head
|
||||
}
|
||||
prev = s
|
||||
}
|
||||
|
||||
return head
|
||||
|
||||
Reference in New Issue
Block a user