mtsatellite/common/spans.go

159 lines
2.6 KiB
Go
Raw Normal View History

2015-07-26 16:44:51 +02:00
// Copyright 2014, 2015 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
import (
"bytes"
"fmt"
)
const chunkSize = 1024
2024-01-07 03:38:36 +01:00
type span struct {
Value int32
From int32
To int32
2024-01-07 03:38:36 +01:00
Next *span
}
2024-01-07 03:38:36 +01:00
type spanPool struct {
freeList *span
}
2024-01-07 03:38:36 +01:00
func newSpanPool() *spanPool {
return &spanPool{}
}
2024-01-07 03:38:36 +01:00
func (sp *spanPool) Alloc() *span {
if sp.freeList != nil {
next := sp.freeList
sp.freeList = next.Next
return next
}
2024-01-07 03:38:36 +01:00
spans := make([]span, chunkSize)
for i := chunkSize - 1; i > 0; i-- {
spans[i].Next = sp.freeList
sp.freeList = &spans[i]
}
return &spans[0]
}
2024-01-07 03:38:36 +01:00
func (sp *spanPool) free(s *span) {
if s != nil {
s.Next = sp.freeList
sp.freeList = s
}
}
2024-01-07 03:38:36 +01:00
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
}
2024-01-07 03:38:36 +01:00
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 ; 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
2024-01-07 03:38:36 +01:00
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
}
}
return head
}
2024-01-07 03:38:36 +01:00
func (s *span) visit(v func(*span)) {
for ; s != nil; s = s.Next {
v(s)
}
}
2024-01-07 03:38:36 +01:00
func (s *span) len() int {
n := 0
for ; s != nil; s = s.Next {
n++
}
return n
}
2024-01-07 03:38:36 +01:00
func (s *span) top() int32 {
2024-01-06 16:12:19 +01:00
for s.Next != nil {
s = s.Next
}
return s.To
}
2024-01-07 03:38:36 +01:00
func (s *span) String() string {
var buf bytes.Buffer
first := true
2024-01-07 03:38:36 +01:00
s.visit(func(s1 *span) {
if !first {
buf.WriteString(", ")
} else {
first = false
}
buf.WriteString(fmt.Sprintf("(%d, %d)", s1.From, s1.To))
})
return buf.String()
}