Documentation ¶
Overview ¶
Package PageInterval provides a data structure for managing page intervals.
Index ¶
- Variables
- func WithSep(sep string) ffs.Option
- func WithWS(ws string) ffs.Option
- type ErrNoPagesInInterval
- type PageInterval
- func (pi *PageInterval) AddPage(page int) error
- func (pi *PageInterval) AddPagesBetween(first, last int)
- func (pi *PageInterval) FString(trav *ffs.Traversor, opts ...ffs.Option) error
- func (pi *PageInterval) GetFirstPage() (int, error)
- func (pi *PageInterval) GetLastPage() (int, error)
- func (pi *PageInterval) HasPage(page int) bool
- func (pi *PageInterval) HasPages() bool
- func (pi *PageInterval) Intervals() []*PageRange
- func (pi *PageInterval) Iterator() uc.Iterater[int]
- func (pi *PageInterval) PageCount() int
- func (pi *PageInterval) RemovePage(page int)
- func (pi *PageInterval) RemovePagesBetween(first, last int)
- func (pi *PageInterval) ReverseIterator() uc.Iterater[int]
- func (pi *PageInterval) String() string
- type PageRange
Constants ¶
This section is empty.
Variables ¶
var ( // PageRangeIterator returns an iterator that iterates over the pages in the interval. // // Parameters: // - pr: The page range. // // Returns: // - uc.Iterater[int]: The iterator that iterates over the pages in the interval. PageRangeIterator func(pr *PageRange) uc.Iterater[int] = func(pr *PageRange) uc.Iterater[int] { return pr.Iterator() } )
Functions ¶
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 *uc.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) FString ¶ added in v0.3.13
FString is a method of the PageInterval type that returns the formatted string representation of the PageInterval using the given traversor and options.
Parameters:
- trav: The traversor to use for printing.
- opts: The options to use for formatting the string.
Returns:
- error: An error if the traversor encounters an error while printing.
Options:
- WithWS: Sets the whitespace to use between the intervals. By default, it is a single space.
- WithSep: Sets the separator to use between the start and end page numbers of an interval. By default, it is a colon. If the separator is an empty string, it is set to a colon instead.
Behaviors:
- If the traversor is empty, the function does nothing.
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 *uc.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 *uc.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() uc.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:
- uc.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() uc.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:
- uc.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
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) FString ¶ added in v0.3.13
FString returns the string representation of the PageRange using the given traversor and options.
Parameters:
- trav: The traversor to use for printing.
- ws: The whitespace to use between the elements. By default, it is a single space.
- sep: The separator to use between the key and value. By default, it is a colon.
Behaviors:
- If sep is an empty string, it is set to a colon.
- ws can be empty. The default value is a single space.
- The default call for AString is: AString(trav, " ", "").
- If trav is empty, the function does nothing.