span buffer test now passes. Code ist still too complicated.

This commit is contained in:
Sascha L. Teichmann 2014-10-21 17:41:19 +02:00
parent e744390503
commit 269e63ea59
1 changed files with 21 additions and 57 deletions

View File

@ -118,67 +118,31 @@ func (sp *SpanPool) Insert(s *Span, pos, value int32) *Span {
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
next := s.Next
if next != nil {
if next.From == s.To-1 && value == next.Value {
s.To = next.To
s.Next = next.Next
sp.Free(next)
return head
}
// Extend next?
if pos == next.From-1 && value == next.Value {
next.From = pos
return head
}
// Before next -> New between current and next
if pos < next.From {
sn := sp.Alloc()
sn.From = pos
sn.To = pos
sn.Value = value
sn.Next = next
s.Next = sn
return head
}
} else { // No next -> new at tail
sn := sp.Alloc()
sn.From = pos
sn.To = pos
sn.Value = value
sn.Next = nil
s.Next = sn
return head
}
} else { // Not directly connected and/or values do not match.
next := s.Next
if next != nil {
if pos == next.From-1 && value == next.Value {
next.From = pos
return head
}
// Before next -> New between current and next
if pos < next.From {
sn := sp.Alloc()
sn.From = pos
sn.To = pos
sn.Value = value
sn.Next = next
s.Next = sn
return head
}
} else { // No next -> new at tail
sn := sp.Alloc()
sn.From = pos
sn.To = pos
sn.Value = value
sn.Next = nil
s.Next = sn
return head
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.