goInterval

package module
v0.0.0-...-746b569 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

README

goInterval

A #golang library implementing the 13 interval base relations based on James F. Allen Interval Algebra, plus the 5 five additional relationships define by Alfred J. Reich.

Allen's original paper is stored in this repository. Reich's paper is also stored in this repository.

The library supports constraints.Ordered Intervals and Points (Start == End) via the struct:

type IInterval[T constraints.Ordered] interface {
	Start() T
	End() T

	Values() iter.Seq[Interval[T]] // Loop through BagOfIntervals if it's required.

	Kind() IntervalKind // P, I, BofI
}

in file ord_intervals.go.

Testing file org_intervals_test.go provides samples for integer and strings. Strings are particulary useful for time data if it's in a sortable format (ISO 8601 or Day of the Year)

If you need to support Time Intervals, the recommended solution is to transform time to a [constraints.Ordered] type via helper functions.


	dt := time.Now() 

	p1 := newPoint(dt.Format("2006-02-01")) // for string representation with day resolution

	p2 := newPoint(dt.Year * 1000 + dt.YearDay()) // for integer representation with day resolution

	p3 := newPoint(dt.Format(time.RFC3339)) // for string representation (RFC3339) with full date resolution

Current development is support of Bags of Intervals.

Relations implemented

For Allen's relationships:

image

Precedes/preceded_by, also called before/after and during/contains support Point

### For Reich's relationships:

image

Extensions

Function Relationship(i1 Interval, i2 Inteval) Relationship and its symmetric Related_by(i1 Interval, i2 Inteval) are provided to identify which of the 18 interval/point base relations meet intervals and points i1 and i2.

Documentation

Overview

A Go library implementing Allen's Algebra 13 interval base relations.

Index

Constants

View Source
const (
	P    = 1 << iota // Point
	I                // Interval
	BofI             // BagOfInterval
)

Variables

This section is empty.

Functions

func After

func After[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/Point1/BoI1 takes place After Interval2/Point2/BoI2.

.............................................. ├── Interval1/Point1 ──┤

├── Interval2/Point2 ──┤

func AsFloat

func AsFloat(value int) float64

function int --> float64

func AsString

func AsString(value int) string

func Before

func Before[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/Point1/BoI1 takes place Before Interval2/Point2/BoI2.

├── Interval1/Point1 ──┤

.............................................. ├── Interval2/Point2 ──┤

func CompareBofI

func CompareBofI[T constraints.Ordered](i1 BagOfIntervals[T], i2 BagOfIntervals[T]) bool

compare i1 & i2 cycling through their bags of intervals

func CompareBofI_SLOW

func CompareBofI_SLOW[T constraints.Ordered](i1 BagOfIntervals[T], i2 BagOfIntervals[T]) bool

compare i1 & i2 cycling through their bags of intervals

func Contains

func Contains[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/Point1 Contains Interval2/Point2.

├──────── Interval2/Point2 ──────────┤

.......├── Interval1/Point1 ──┤

func During

func During[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/Point1 occurs During Interval2/Point2.

.......├── Interval1/Point1 ──┤

├──────── Interval2/Point2 ──────────┤

func Finished_by

func Finished_by[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/BoI1 is finished by Interval2/BoI2.

├──────── Interval2 ──────────┤

..............├── Interval1 ──┤

func Finishes

func Finishes[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/BoI1 Finishes Interval2/BoI2.

..............├── Interval1 ──┤

├──────── Interval2 ──────────┤

func IfElse

func IfElse[T any](condition bool, yes T, no T) T

Returns yes if condition is true, otherwise returns no

func IntervalEqual

func IntervalEqual[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1 is IntervalEqual to Interval2.

......├── Interval1 ──┤

......├── Interval2 ──┤

func IntervalMean

func IntervalMean[T constraints.Ordered](i OrdInterval[T]) (float64, error)

func IsBofI

func IsBofI[T constraints.Ordered](i OrdInterval[T]) bool

func IsInterval

func IsInterval[T constraints.Ordered](i OrdInterval[T]) bool

func IsIntervalOrBofI

func IsIntervalOrBofI[T constraints.Ordered](i OrdInterval[T]) bool

func IsPoint

func IsPoint[T constraints.Ordered](i OrdInterval[T]) bool

func Meets

func Meets[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/BoI1 Meets Interval2/BoI2.

├── Interval1 ──┼── Interval2 ──┤

func Met_by

func Met_by[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/BoI1 is met by Interval2/BoI2.

├── Interval2 ──┼── Interval1 ──┤

func Overlapped_by

func Overlapped_by[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1 is overlapped by Interval2.

.........├── Interval1 ──┤

├── Interval2 ──┤ TODO: incorporate BoI

func Overlaps

func Overlaps[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1 Overlaps with Interval2.

├── Interval1 ──┤

.........├── Interval2 ──┤ TODO: incorporate BoI

func PointEqual

func PointEqual[T constraints.Ordered](p1 OrdInterval[T], p2 OrdInterval[T]) bool

True if Point1 is Equal to Point2. (From Reich's paper)

......| Point1

......| Point2

func PointFinishes

func PointFinishes[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Point1 is at Interval2/BoI2 finish (From Reich's paper)

................| Point 1

├── Interval2 ──┤

func PointFinishes_by

func PointFinishes_by[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval2/BoI2 finishes by Point1 (From Reich's paper)

├── Interval2 ──┤

................| Point 1

func PointStart

func PointStart[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Point1 is at Interval2/BoI2 start (From Reich's paper)

................| Point 1

├── Interval2 ──┤

func PointStarts_by

func PointStarts_by[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval2/Boi2 starts by Point1 (From Reich's paper)

├── Interval2 ──┤

................| Point 1

func Preceded_by

func Preceded_by[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/Point1/BoI1 takes place after Interval2/Point2/BoI2.

.............................................. ├── Interval1/Point1 ──┤

├── Interval2/Point2 ──┤

func Precedes

func Precedes[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/Point1/BoI1 takes place before Interval2/Point2/BoI2.

├── Interval1/Point1 ──┤

.............................................. ├── Interval2/Point2 ──┤

func Started_by

func Started_by[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/BoI1 is started by Interval2/BoI2.

├──────── Interval1 ──────────┤

├── Interval2 ──┤

func Starts

func Starts[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) bool

True if Interval1/BoI1 Starts Interval2/BoI2.

├── Interval1 ──┤

├──────── Interval2 ──────────┤

func Textual

func Textual(i RelationshipKind) (output string)

func ValidBofI

func ValidBofI[T constraints.Ordered](i BagOfIntervals[T]) bool

True if BofI is a valid (i.Start < i.End)

func ValidInterval

func ValidInterval[T constraints.Ordered](i Interval[T]) bool

True if Interval is a valid interval (i.Start < i.End)

func ValidPoint

func ValidPoint[T constraints.Ordered](i Point[T]) bool

True if Interval is a valid point (i.Start == i.End)

func ValueAsFloat

func ValueAsFloat(input any) (output float64, err error)

1 --> 1.0 1.0 --> 1.0 "1.0" --> 1.0

Types

type BagOfIntervals

type BagOfIntervals[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func NewBagOfIntervals

func NewBagOfIntervals[T constraints.Ordered](intervals ...Interval[T]) (output BagOfIntervals[T])

creates a bag of Intervals

BagofIntervals stores /intervals/ sorted if len(intervals) == 1 returns invervals[0] as an Inverval[T]

func (BagOfIntervals[T]) End

func (i BagOfIntervals[T]) End() T

func (BagOfIntervals[T]) Kind

func (i BagOfIntervals[T]) Kind() IntervalKind

func (BagOfIntervals[T]) Len

func (i BagOfIntervals[T]) Len() int

func (BagOfIntervals[T]) Start

func (i BagOfIntervals[T]) Start() T

func (BagOfIntervals[T]) String

func (i BagOfIntervals[T]) String() (output string)

func (BagOfIntervals[T]) Values

func (i BagOfIntervals[T]) Values() iter.Seq[Interval[T]]

type BofIReport

type BofIReport[T constraints.Ordered] struct {
	NumElems        int     // total number of elements
	NumDiffElems    int     // number of different elements
	FullCoverage    bool    // if the BofI has continuous intervals from .Start() to .End()
	Overlaps        bool    // if any of the intervals overlaps/is_overlaped by other interval.
	MeanLength      float64 // still wip
	ValidMeanLength bool    // wip
	Bofi            *BagOfIntervals[T]
}

func NewBofIAnalysis

func NewBofIAnalysis[T constraints.Ordered](i BagOfIntervals[T]) (output BofIReport[T])

func (BofIReport[T]) String

func (r BofIReport[T]) String() string

type Histogram

type Histogram[K comparable] struct {
	// contains filtered or unexported fields
}

Histogram calculates the frequency of value K treating any negative V as zero. So if we remove K when freq(K) == 0 we do not update its frequency.

func (Histogram[K]) Add

func (d Histogram[K]) Add(key K) (NewValue int)

func (Histogram[K]) Empty

func (d Histogram[K]) Empty() bool

func (Histogram[K]) Len

func (d Histogram[K]) Len() int

func (Histogram[K]) Remove

func (d Histogram[K]) Remove(key K) (NewValue int)

type HistogramPlus

type HistogramPlus[K comparable] struct {
	// contains filtered or unexported fields
}

HistogramPlus calculates the frequency of value K treating any negative V as negative. So if we remove K when freq(K) == 0 then freq(K) == -1

func (HistogramPlus[K]) Add

func (d HistogramPlus[K]) Add(key K) (NewValue int)

func (HistogramPlus[K]) IsZero

func (d HistogramPlus[K]) IsZero() bool

func (HistogramPlus[K]) Len

func (d HistogramPlus[K]) Len() int

func (HistogramPlus[K]) Remove

func (d HistogramPlus[K]) Remove(key K) (NewValue int)

type Interval

type Interval[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func NewInterval

func NewInterval[T constraints.Ordered](start T, end T) Interval[T]

func (Interval[T]) End

func (i Interval[T]) End() T

func (Interval[T]) Kind

func (i Interval[T]) Kind() IntervalKind

func (Interval[T]) Len

func (i Interval[T]) Len() int

func (Interval[T]) Start

func (i Interval[T]) Start() T

func (Interval[T]) String

func (i Interval[T]) String() string

func (Interval[T]) Values

func (i Interval[T]) Values() iter.Seq[Interval[T]]

type IntervalKind

type IntervalKind int

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

type OrdInterval

type OrdInterval[T constraints.Ordered] interface {
	Start() T
	End() T

	Values() iter.Seq[Interval[T]] // Loop through BagOfIntervals if it's required.

	Kind() IntervalKind // P, I, BofI

	Len() int

	String() string
}

type Parallel

type Parallel[T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*Parallel[T]) Add

func (p *Parallel[T]) Add(name string, fx func() T) (pid int)

func (*Parallel[T]) NumDone

func (p *Parallel[T]) NumDone() (num int)

func (*Parallel[T]) NumPending

func (p *Parallel[T]) NumPending() (num int)

func (*Parallel[T]) NumRunning

func (p *Parallel[T]) NumRunning() (num int)

func (*Parallel[T]) Status

func (p *Parallel[T]) Status() (report []WGTasks)

type Point

type Point[T constraints.Ordered] struct {
	// contains filtered or unexported fields
}

func NewPoint

func NewPoint[T int](value T) Point[T]

func (Point[T]) End

func (i Point[T]) End() T

func (Point[T]) Kind

func (i Point[T]) Kind() IntervalKind

func (Point[T]) Len

func (i Point[T]) Len() int

func (Point[T]) Start

func (i Point[T]) Start() T

func (Point[T]) String

func (i Point[T]) String() string

func (Point[T]) Values

func (i Point[T]) Values() iter.Seq[Interval[T]]

type RelationshipKind

type RelationshipKind int

The 13 relationships defined by Allen's interval algebra.

const (
	PRECEDES          RelationshipKind = 1 << iota // Relation (p), alias BEFORE
	PRECEDED_BY                                    // Converse (P), alias AFTER
	MEETS                                          // Relation (m)
	MET_BY                                         // Converse (M)
	OVERLAPS                                       // Relation (o)
	OVERLAPPED_BY                                  // Converse (O)
	STARTS                                         // Relation (s)
	STARTED_BY                                     // Converse (S)
	DURING                                         // Converse (d)
	CONTAINS                                       // Relation (D)
	FINISHES                                       // Converse (f)
	FINISHED_BY                                    // Relation (F)
	EQUAL_INTERVAL                                 // Relation/Converse (e)
	POINT_FINISHES                                 // Point Finishes (pf)
	POINT_FINISHES_BY                              // Point Finishes by (pfi)
	POINT_STARTS                                   // Point Starts (ps)
	POINT_STARTS_BY                                // Point Starts by (psi)
	EQUAL_POINT                                    // Point Equal (pe)
)
func Related_by[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) (output RelationshipKind)

reverse of Relationship

func Relationship

func Relationship[T constraints.Ordered](i1 OrdInterval[T], i2 OrdInterval[T]) (output RelationshipKind)

Returns one of the 13 Allen's relations between Interval1 and Interval2

func RelationshipSet

func RelationshipSet(relationships ...RelationshipKind) (set RelationshipKind)

OVERLAPS, STARTS, FINISHES, CONTAINS --> 0b11001010000

func (RelationshipKind) String

func (d RelationshipKind) String() (output string)

type WGTasks

type WGTasks struct {
	// contains filtered or unexported fields
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL