mtsatellite/common/spans.go

187 lines
3.5 KiB
Go
Raw Normal View History

// Copyright 2014 by Sascha L. Teichmann
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package common
const chunkSize = 1024
type Span struct {
Value int32
From int32
To int32
Next *Span
}
type SpanPool struct {
freeList *Span
}
func NewSpanPool() *SpanPool {
return &SpanPool{}
}
func (sp *SpanPool) Alloc() *Span {
if sp.freeList != nil {
next := sp.freeList
sp.freeList = next.Next
return next
}
spans := make([]Span, chunkSize)
for i := chunkSize - 1; i > 0; i-- {
spans[i].Next = sp.freeList
sp.freeList = &spans[i]
}
return &spans[0]
}
func (sp *SpanPool) Free(s *Span) {
if s != nil {
s.Next = sp.freeList
sp.freeList = s
}
}
func (sp *SpanPool) FreeAll(s *Span) {
if s == nil {
return
}
head, prev := s, s
for ; s != nil; s = s.Next {
prev = s
}
prev.Next = sp.freeList
sp.freeList = head
}
func (sp *SpanPool) Insert(s *Span, pos, value int32) *Span {
// No head -> create.
if s == nil {
s = sp.Alloc()
s.From = pos
s.To = pos
s.Value = value
s.Next = nil
return s
}
if pos < s.From {
// Same value and directly neighbored -> extend head.
if value == s.Value && pos == s.From-1 {
s.From = pos
return s
}
// Disjunct -> create new head.
prev := sp.Alloc()
prev.From = pos
prev.To = pos
prev.Value = value
prev.Next = s
return prev
}
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
}
}
// Extend prev?
if prev.To == pos-1 && value == prev.Value {
prev.To = pos
return head
}
// New between prev and current.
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
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
}
}
default: // pos in span -> do not modify.
return head
}
prev = s
}
return head
}