span

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 5 Imported by: 1

README

Span

Go Reference Go Report Card Coverage Status

Purpose

Library that provides to divide a sequence of something into spans

Usage

Example:

package main

import (
    "fmt"

    "github.com/akramarenkov/span"
)

func main() {
    spans, err := span.Evenly(1, 8, 3)
    fmt.Println(err)
    fmt.Println(spans)
    // Output:
    // <nil>
    // [{1 3} {4 6} {7 8}]
}

Documentation

Overview

Library that provides to divide a sequence of something into spans.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrSpansDiffSequencing          = errors.New("spans have different types of sequences")
	ErrSpansDiscontinuous           = errors.New("spans is discontinuous")
	ErrSpanSequenceNotNonDecreasing = errors.New("span sequence is not non-decreasing")
	ErrSpanSequenceNotNonIncreasing = errors.New("span sequence is not non-increasing")
	ErrSpansIntersect               = errors.New("spans is intersect")
	ErrSpansQuantityNegative        = errors.New("spans quantity is negative")
	ErrSpansQuantityZero            = errors.New("spans quantity is zero")
	ErrSpanWidthNegative            = errors.New("span width is negative")
	ErrSpanWidthZero                = errors.New("span width is zero")
)

Functions

func CompareDec added in v0.4.0

func CompareDec[Type cmp.Ordered](first, second Span[Type]) int

Compare function for sorting of decreasing sequence of spans.

Partially detects spans intersections, but does not guarantee complete verification.

Partially detects the presence of spans that have a different sequence type, but does not guarantee complete verification.

func CompareInc added in v0.4.0

func CompareInc[Type cmp.Ordered](first, second Span[Type]) int

Compare function for sorting of increasing sequence of spans.

Partially detects spans intersections, but does not guarantee complete verification.

Partially detects the presence of spans that have a different sequence type, but does not guarantee complete verification.

func Even added in v0.7.0

func Even[Type constraints.Integer](begin, end, quantity Type) iter.Seq2[uint64, Span[Type]]

A range iterator used to iterating over a sequence of spans obtained by dividing a linear sequence of integers evenly from begin to end inclusive into a specified quantity of spans.

If begin is greater than end, the sequence of spans will be decreasing, otherwise - increasing.

Quantity of iterations can be less than the specified quantity of spans, but cannot be greater.

If a zero or negative quantity of spans is specified, the iterator will panic.

Works like Evenly but does not perform memory allocation.

func EvenSlices added in v0.9.0

func EvenSlices[Type any, TypeQ constraints.Integer](divisible []Type, quantity TypeQ) iter.Seq[[]Type]

A range iterator used to iterating over a subslices obtained by evenly dividing a main slice into a specified quantity of subslices.

Quantity of iterations can be less than the specified quantity of subslices, but cannot be greater.

If a zero or negative quantity of subslices is specified, the iterator will panic.

func IsContinuous added in v0.4.0

func IsContinuous[Type constraints.Integer](spans []Span[Type]) error

Checks that a sequence of spans is continuous and monotone.

func IsNonDecreasing added in v0.6.0

func IsNonDecreasing[Type cmp.Ordered](spans []Span[Type]) error

Checks that a spans sequence consists of only non-decreasing spans.

func IsNonIncreasing added in v0.6.0

func IsNonIncreasing[Type cmp.Ordered](spans []Span[Type]) error

Checks that a spans sequence consists of only non-increasing spans.

func IsNotDiffSequencing added in v0.4.0

func IsNotDiffSequencing[Type cmp.Ordered](spans []Span[Type]) error

Checks that a sequence of spans does not contain spans with different sequence types (increasing or decreasing).

func IsNotIntersect added in v0.4.0

func IsNotIntersect[Type cmp.Ordered](spans []Span[Type]) error

Checks that a sequence of spans does not contain intersect spans.

func SearchDec added in v0.5.0

func SearchDec[Type cmp.Ordered](item, target Span[Type]) int

Compare function for searching in decreasing sequence of spans.

Partially detects the presence of spans that have a different sequence type, but does not guarantee complete verification.

func SearchInc added in v0.5.0

func SearchInc[Type cmp.Ordered](item, target Span[Type]) int

Compare function for searching in increasing sequence of spans.

Partially detects the presence of spans that have a different sequence type, but does not guarantee complete verification.

Types

type Span

type Span[Type cmp.Ordered] struct {
	Begin Type
	End   Type
}

Describes span.

func Evenly added in v0.3.0

func Evenly[Type constraints.Integer](begin, end, quantity Type) ([]Span[Type], error)

Divides a linear sequence of integers evenly from begin to end inclusive into a specified quantity of spans.

If begin is greater than end, the sequence of integers will be considered decreasing, otherwise - increasing.

Length of the returned slice can be less than the specified quantity of spans, but cannot be greater.

If a zero or negative quantity of spans is specified, an error is returned.

Example
package main

import (
	"fmt"

	"github.com/akramarenkov/span"
)

func main() {
	spans, err := span.Evenly(1, 8, 3)
	fmt.Println(err)
	fmt.Println(spans)
}
Output:

<nil>
[{1 3} {4 6} {7 8}]

func Linear added in v0.3.0

func Linear[Type constraints.Integer](begin, end, width Type) ([]Span[Type], error)

Divides a linear sequence of integers from begin to end inclusive into spans of the specified width.

If begin is greater than end, the sequence of integers will be considered decreasing, otherwise - increasing.

If a zero or negative width of span is specified, an error is returned.

Example
package main

import (
	"fmt"

	"github.com/akramarenkov/span"
)

func main() {
	spans, err := span.Linear(1, 7, 2)
	fmt.Println(err)
	fmt.Println(spans)
}
Output:

<nil>
[{1 2} {3 4} {5 6} {7 7}]

Jump to

Keyboard shortcuts

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