intervals

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Base10 represents the base 10 number system.
	Base10 int = 10
)

Variables

This section is empty.

Functions

func IsIn

func IsIn(page uint64, r Range) bool

IsIn checks if the given page number is in the interval.

Parameters:

  • page: The page number to check.
  • r: The interval to check.

Returns:

  • bool: True if the page number is in the interval, otherwise false.

func Overlaps

func Overlaps(r1, r2 Range) bool

Overlaps checks if the given intervals overlap.

Parameters:

  • r1: The first interval to check.
  • r2: The second interval to check.

Returns:

  • bool: True if the intervals overlap, otherwise false.

Types

type PageInterval

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

PageInterval represents a collection of page intervals, where each interval is represented by a pair of integers.

func NewPageInterval

func NewPageInterval() PageInterval

NewPageInterval creates a new instance of PageInterval with empty intervals and a page count of 0. This is not necessary as var pi PageInterval is equivalent to NewPageInterval().

Returns:

  • PageInterval: The new PageInterval.

The PageInterval ensures non-overlapping, non-duplicate intervals and reduces the amount of intervals by merging two consecutive intervals into one.

Example:

pi := NewPageInterval()
pi.AddPagesBetween(1, 5)
pi.AddPagesBetween(10, 15)

fmt.Println(pi.Intervals()) // Output: [[1 5] [10 15]]
fmt.Println(pi.PageCount()) // Output: 11

func (*PageInterval) AddPage

func (pi *PageInterval) AddPage(page uint64) error

AddPage is a method of the PageInterval type that adds a page to the PageInterval, maintaining the non-overlapping, non-duplicate intervals.

Parameters:

  • page: The page number to add to the PageInterval.

Returns:

  • error: An error if the function fails to add the page, otherwise nil.

Errors:

  • errors.NilReceiver: If the receiver is nil.
  • *errors.ErrInvalidParameter: If the page number is less than 1.

Example:

pi := PageInterval{
    intervals: []PageRange{{1, 5}, {10, 15}},
    pageCount: 11,
}

pi.AddPage(6)
fmt.Println(pi.intervals) // Output: [[1 6] [10 15]]
fmt.Println(pi.pageCount) // Output: 12

func (*PageInterval) AddPagesBetween

func (pi *PageInterval) AddPagesBetween(first, last uint64) error

AddPagesBetween is a method of the PageInterval type that adds pages between the first and last page numbers to the PageInterval.

However, if the first page number is less than 1, it is set to 1 to remove invalid pages, same goes for the last page number. Finally, if the last page number is less than the first page number, the values are swapped.

Parameters:

  • first: The first page number to add to the PageInterval.
  • last: The last page number to add to the PageInterval.

Returns:

  • error: An error if the function fails to add pages to the PageInterval.

Errors:

  • gcers.NilReceiver: If the receiver is nil.
  • *gcint.ErrWhileAt: If an error occurs while adding a page.

Example:

pi := PageInterval{
    intervals: []PageRange{{1, 5}, {10, 15}},
    pageCount: 11,
}

pi.AddPagesBetween(6, 9)
fmt.Println(pi.intervals) // Output: [[1 15]]
fmt.Println(pi.pageCount) // Output: 15

func (PageInterval) All

func (pi PageInterval) All() iter.Seq[uint64]

All returns an iterator that iterates over the numbers in the PageInterval from the first number to the last number.

Returns:

  • itr.Seq[uint64]: The iterator. Never returns nil.

func (PageInterval) Backward

func (pi PageInterval) Backward() iter.Seq[uint64]

Backward returns an iterator that iterates over the numbers in the PageInterval from the last number to the first number.

Returns:

  • itr.Seq[uint64]: The iterator. Never returns nil.

func (PageInterval) ByIdx

func (pi PageInterval) ByIdx(idx uint32) (Range, bool)

ByIdx returns the interval at the given index in the intervals slice.

Parameters:

  • idx: The index of the interval.

Returns:

  • Range: The interval at the given index.
  • bool: True if the index is valid, otherwise false.

func (PageInterval) GetFirstPage

func (pi PageInterval) GetFirstPage() (uint64, bool)

GetFirstPage is a method of the PageInterval type that returns the first page number in the PageInterval.

Returns:

  • uint64: The first page number in the PageInterval.
  • bool: True if the PageInterval has pages, and false otherwise.

func (PageInterval) GetLastPage

func (pi PageInterval) GetLastPage() (uint64, bool)

GetLastPage is a method of the PageInterval type that returns the last page number in the PageInterval.

Returns:

  • uint64: The last page number in the PageInterval.
  • bool: True if the PageInterval has pages, and false otherwise.

func (PageInterval) HasPage

func (pi PageInterval) HasPage(page uint64) bool

HasPage is a method of the PageInterval type that checks if the given page exists in the PageInterval.

Parameters:

  • page: The page number to check for in the PageInterval.

Returns:

  • bool: A boolean value that is true if the page exists in the PageInterval, and false otherwise.

Example:

pi := PageInterval{
    intervals: []PageRange{{1, 5}, {10, 15}},
    pageCount: 11,
}

hasPage := pi.HasPage(3)
fmt.Println(hasPage) // Output: true

func (PageInterval) HasPages

func (pi PageInterval) HasPages() bool

HasPages is a method of the PageInterval type that checks if the PageInterval has any pages.

Returns:

  • bool: A boolean value that is true if the PageInterval has pages, and false otherwise.

func (PageInterval) IntervalOf

func (pi PageInterval) IntervalOf(page uint64) (Range, bool)

IntervalOf returns the interval that contains the given number in the PageInterval.

Parameters:

  • page: The page number to search for in the PageInterval.

Returns:

  • Range: The interval that contains the page number.
  • bool: True if the page is found, otherwise false.

func (PageInterval) Intervals

func (pi PageInterval) Intervals() []Range

Intervals is a method of the PageInterval type that returns the intervals stored in the PageInterval. Each interval is represented as a pair of integers, where the first integer is the start page number and the second integer is the end page number.

Returns:

  • []PageRange: A slice of integer pairs representing the intervals in the PageInterval.

func (PageInterval) PageCount

func (pi PageInterval) PageCount() int

PageCount is a method of the PageInterval type that returns the total number of pages across all intervals in the PageInterval.

Returns:

  • int: The total number of pages across all intervals in the PageInterval.

func (*PageInterval) RemovePage

func (pi *PageInterval) RemovePage(page uint64) error

RemovePage is a method of the PageInterval type that removes the specified page from the PageInterval. No changes are made if the page number is less than 1 or not found in the PageInterval.

Parameters:

  • page: The page number to remove from the PageInterval.

Returns:

  • bool: True if the receiver is not nil, otherwise false.

Example:

pi := PageInterval{
    intervals: []PageRange{{1, 5}, {10, 15}},
    pageCount: 11,
}

pi.RemovePage(5)
fmt.Println(pi.intervals) // Output: [[1 4] [10 15]]
fmt.Println(pi.pageCount) // Output: 10

func (*PageInterval) RemovePagesBetween

func (pi *PageInterval) RemovePagesBetween(first, last uint64) error

RemovePagesBetween is a method of the PageInterval type that removes pages between the specified first and last page numbers from the PageInterval.

However, if the first page number is less than 1, it is set to 1 to remove invalid pages, same goes for the last page number. Finally, if the last page number is less than the first page number, the values are swapped.

Parameters:

  • first, last: The first and last page numbers to remove from the PageInterval, respectively.

Returns:

  • bool: True if the receiver is not nil, false otherwise.

Example:

pi := PageInterval{
    intervals: []PageRange{{1, 5}, {10, 15}},
    pageCount: 11,
}

pi.RemovePagesBetween(3, 4)
fmt.Println(pi.intervals) // Output: [[1 2] [5 5] [10 15]]
fmt.Println(pi.pageCount) // Output: 9

func (PageInterval) String

func (pi PageInterval) String() string

String implements the fmt.Stringer interface.

Format:

<from>:<to>, <from>:<to>, ... // for example, 1:5, 6:10

type Range

type Range [2]uint64

Range represents a pair of integers that represent the start and end numbers of an interval. The first integer is the start number and the second integer is the end number of the interval. (both inclusive)

For instance, the Range [1, 5] represents the interval from 1 to 5.

func NewPageRange

func NewPageRange(start, end uint64) Range

NewPageRange creates a new instance of PageRange with the given start and end numbers. If start is greater than end, the start and end are swapped.

Parameters:

  • start: The start number of the interval.
  • end: The end number of the interval.

Returns:

  • PageRange: The new PageRange.

func (Range) All

func (pr Range) All() iter.Seq[uint64]

All returns an iterator that iterates over the numbers in the interval from the first number to the second number.

Returns:

  • iter.Seq[uint64]: The iterator. Never returns nil.

func (Range) Backward

func (pr Range) Backward() iter.Seq[uint64]

Backward returns an iterator that iterates over the in the interval from the second number to the first number.

Returns:

  • iter.Seq[uint64]: The iterator. Never returns nil.

func (Range) First

func (r Range) First() uint64

First returns the first number of the interval.

Returns:

  • uint64: The first number of the interval.

func (Range) Second

func (r Range) Second() uint64

Second returns the second number of the interval.

Returns:

  • uint64: The second number of the interval.

func (Range) Size

func (r Range) Size() uint64

Size returns the number of pages in the interval.

Returns:

  • uint64: The number of pages in the interval.

func (Range) String

func (pr Range) String() string

String implements fmt.Stringer.

Jump to

Keyboard shortcuts

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