Documentation
¶
Index ¶
- Constants
- func IsIn(page uint64, r Range) bool
- func Overlaps(r1, r2 Range) bool
- type PageInterval
- func (pi *PageInterval) AddPage(page uint64) error
- func (pi *PageInterval) AddPagesBetween(first, last uint64) error
- func (pi PageInterval) All() iter.Seq[uint64]
- func (pi PageInterval) Backward() iter.Seq[uint64]
- func (pi PageInterval) ByIdx(idx uint32) (Range, bool)
- func (pi PageInterval) GetFirstPage() (uint64, bool)
- func (pi PageInterval) GetLastPage() (uint64, bool)
- func (pi PageInterval) HasPage(page uint64) bool
- func (pi PageInterval) HasPages() bool
- func (pi PageInterval) IntervalOf(page uint64) (Range, bool)
- func (pi PageInterval) Intervals() []Range
- func (pi PageInterval) PageCount() int
- func (pi *PageInterval) RemovePage(page uint64) error
- func (pi *PageInterval) RemovePagesBetween(first, last uint64) error
- func (pi PageInterval) String() string
- type Range
Constants ¶
const ( // Base10 represents the base 10 number system. Base10 int = 10 )
Variables ¶
This section is empty.
Functions ¶
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 ¶
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 ¶
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 ¶
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 ¶
First returns the first number of the interval.
Returns:
- uint64: The first number of the interval.
func (Range) Second ¶
Second returns the second number of the interval.
Returns:
- uint64: The second number of the interval.