PageInterval

package
v0.2.38 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package PageInterval provides a data structure for managing page intervals.

Index

Constants

This section is empty.

Variables

View Source
var (
	// PageRangeIterator returns an iterator that iterates over the pages in the interval.
	//
	// Parameters:
	//   - pr: The page range.
	//
	// Returns:
	//   - itf.Iterater[int]: The iterator that iterates over the pages in the interval.
	PageRangeIterator func(pr *PageRange) itf.Iterater[int] = func(pr *PageRange) itf.Iterater[int] {
		return pr.Iterator()
	}
)

Functions

This section is empty.

Types

type ErrNoPagesInInterval added in v0.2.26

type ErrNoPagesInInterval struct{}

ErrNoPagesInInterval represents an error where there are no pages in the interval.

func NewErrNoPagesInInterval added in v0.2.26

func NewErrNoPagesInInterval() *ErrNoPagesInInterval

NewErrNoPagesInInterval creates a new instance of ErrNoPagesInInterval.

Returns:

  • error: An error of type *ErrNoPagesInInterval.

func (*ErrNoPagesInInterval) Error added in v0.2.26

func (e *ErrNoPagesInInterval) Error() string

Error is a method of the error interface.

Returns:

  • string: The error message.

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.

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 int) 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 of type *ers.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 int)

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.

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) GetFirstPage

func (pi *PageInterval) GetFirstPage() (int, error)

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

Returns:

  • int: The first page number in the PageInterval.
  • error: An error of type *ers.ErrNoPagesInInterval if no pages have been set.

func (*PageInterval) GetLastPage

func (pi *PageInterval) GetLastPage() (int, error)

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

Returns:

  • int: The last page number in the PageInterval.
  • error: An error of type *ers.ErrNoPagesInInterval if no pages have been set.

func (*PageInterval) HasPage

func (pi *PageInterval) HasPage(page int) 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) Intervals

func (pi *PageInterval) Intervals() []*PageRange

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) Iterator

func (pi *PageInterval) Iterator() itf.Iterater[int]

Iterator is a method of the PageInterval type that returns an iterator for iterating over the pages in the PageInterval.

Panics if an error occurs while creating the iterator.

Returns:

  • itf.Iterater[int]: An iterator for iterating over the pages 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:

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

func (*PageInterval) RemovePage

func (pi *PageInterval) RemovePage(page int)

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.

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 int)

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.

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) ReverseIterator

func (pi *PageInterval) ReverseIterator() itf.Iterater[int]

ReverseIterator is a method of the PageInterval type that returns a PageIntervalReverseIterator for iterating over the intervals in the PageInterval in reverse order.

Panics if an error occurs while creating the iterator.

Returns:

  • itf.Iterater[int]: An iterator for iterating over the intervals in the PageInterval in reverse order.

func (*PageInterval) String

func (pi *PageInterval) String() string

String is a method of the PageInterval type that returns a string representation of the PageInterval. Each interval is represented as "start : end" separated by a comma.

Returns:

  • string: A formatted string representation of the PageInterval.

type PageRange added in v0.2.9

type PageRange cdp.Pair[int, int]

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

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

func (*PageRange) Iterator added in v0.2.36

func (pr *PageRange) Iterator() itf.Iterater[int]

Iterator returns an iterator that iterates over the pages in the interval.

Returns:

  • itf.Iterater[int]: The iterator that iterates over the pages in the interval.

func (*PageRange) String added in v0.2.36

func (pr *PageRange) String() string

String returns the string representation of the PageRange.

Returns:

  • string: The string representation of the PageRange.

Jump to

Keyboard shortcuts

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