PageInterval

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrMaxLessThanMin

type ErrMaxLessThanMin struct{}

ErrMaxLessThanMin is an error type that represents the condition where the maximum page number is less than the minimum page number. It implements the error interface.

func (*ErrMaxLessThanMin) Error

func (e *ErrMaxLessThanMin) Error() string

type ErrMinLessThanOne

type ErrMinLessThanOne struct{}

ErrMinLessThanOne is an error type that represents the condition where the minimum value is less than one. It implements the error interface.

func (*ErrMinLessThanOne) Error

func (e *ErrMinLessThanOne) Error() string

type ErrNoPagesHaveBeenSet

type ErrNoPagesHaveBeenSet struct{}

ErrNoPagesHaveBeenSet is an error type that represents the condition where no pages have been set in a book or document. It implements the error interface.

func (*ErrNoPagesHaveBeenSet) Error

func (e *ErrNoPagesHaveBeenSet) Error() string

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. 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 NewPageInterval

func NewPageInterval() PageInterval

NewPageInterval creates a new instance of PageInterval. It initializes the 'intervals' field with an empty slice and sets the 'pageCount' field to 0.

Example:

pi := NewPageInterval()
fmt.Println(pi.intervals) // Output: []
fmt.Println(pi.pageCount) // Output: 0

func (*PageInterval) AddPage

func (pi *PageInterval) AddPage(page int)

AddPage adds a page to the PageInterval. If the page number is less than 1, it is considered a no-op and the function returns. If the PageInterval is empty, a new interval is created with the given page number. If the page number falls within an existing interval, the interval is updated accordingly. If the page number does not fall within any existing interval, a new interval is created. After adding the page, the PageInterval is reduced to merge overlapping intervals.

Example:

pi := PageInterval{
    intervals: [][2]int{{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 adds pages between the first and last page numbers to the PageInterval. If the first page number is less than 1, it is set to 1 to remove invalid pages. If the last page number is less than 1, it is set to 1 to remove invalid pages. If the last page number is less than the first page number, the values are swapped.

Example:

pi := PageInterval{
    intervals: [][2]int{{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 returns the first page of the PageInterval. It returns the page number and an error if no pages have been set. If pages have been set, it returns the start page number of the first interval and nil error.

Example:

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

firstPage, err := pi.GetFirstPage()
if err != nil {
    fmt.Println(err) // Output: no pages have been set
} else {
    fmt.Println(firstPage) // Output: 1
}

func (*PageInterval) GetLastPage

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

GetLastPage returns the last page of the PageInterval. It returns the page number and an error if no pages have been set. If pages have been set, it returns the end page number of the last interval and nil error.

Example:

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

lastPage, err := pi.GetLastPage()
if err != nil {
    fmt.Println(err) // Output: no pages have been set
} else {
    fmt.Println(lastPage) // Output: 15
}

func (*PageInterval) HasPage

func (pi *PageInterval) HasPage(page int) bool

HasPage checks if the given page exists in the PageInterval. It returns true if the page is found, otherwise false.

Example:

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

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

func (*PageInterval) HasPages

func (pi *PageInterval) HasPages() bool

HasPages checks if the PageInterval has any pages. It returns true if the page count is greater than 0, otherwise false.

Example:

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

hasPages := pi.HasPages()
fmt.Println(hasPages) // Output: true

func (*PageInterval) Intervals

func (pi *PageInterval) Intervals() [][2]int

Intervals 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.

Example:

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

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

func (*PageInterval) Iterator

func (pi *PageInterval) Iterator() PageIntervalIterator

Iterator returns a PageIntervalIterator for iterating over the intervals in the PageInterval. The iterator starts from the first interval and moves forward.

Example:

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

iterator := pi.Iterator()
for iterator.HasNext() {
    fmt.Println(iterator.Next()) // Output: 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15
}

func (*PageInterval) MustGetFirstPage

func (pi *PageInterval) MustGetFirstPage() int

MustGetFirstPage returns the first page number in the PageInterval. It panics with ErrNoPagesHaveBeenSet if no pages have been set.

Example:

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

firstPage := pi.MustGetFirstPage()
fmt.Println(firstPage) // Output: 1

func (*PageInterval) MustGetLastPage

func (pi *PageInterval) MustGetLastPage() int

MustGetLastPage returns the last page number in the PageInterval. It panics with ErrNoPagesHaveBeenSet if no pages have been set.

Example:

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

lastPage := pi.MustGetLastPage()
fmt.Println(lastPage) // Output: 15

func (*PageInterval) PageCount

func (pi *PageInterval) PageCount() int

PageCount returns the total number of pages across all intervals in the PageInterval. The pageCount is precalculated and stored in the PageInterval struct.

Example:

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

count := pi.PageCount()
fmt.Println(count) // Output: 11

func (*PageInterval) RemovePage

func (pi *PageInterval) RemovePage(page int)

RemovePage removes the specified page from the PageInterval. If the page number is less than 1, it is considered a no-op and no changes are made. If the page number is not found in the PageInterval, no changes are made. After removing the page, the page count is decremented and the intervals are updated accordingly. Finally, the PageInterval is reduced if necessary.

Example:

pi := PageInterval{
    intervals: [][2]int{{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 removes pages between the specified first and last page numbers from the PageInterval. If the first page number is less than 1, it is set to 1 to remove invalid pages. If the last page number is less than 1, it is set to 1 to remove invalid pages. If the last page number is less than the first page number, the values are swapped. Pages between the first and last page numbers (inclusive) are removed using the RemovePage method.

Example:

pi := PageInterval{
    intervals: [][2]int{{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() PageIntervalReverseIterator

ReverseIterator returns a reverse iterator for the PageInterval. The reverse iterator allows iterating over the intervals in reverse order.

Example:

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

reverseIterator := pi.ReverseIterator()
for reverseIterator.HasPrev() {
    fmt.Println(reverseIterator.Prev()) // Output: 15, 14, 13, 12, 11, 10, 5, 4, 3, 2, 1
}

func (*PageInterval) String

func (pi *PageInterval) String() string

String returns a string representation of the PageInterval. It concatenates all intervals in the PageInterval and returns them as a formatted string. Each interval is represented as "[start : end]".

Example:

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

fmt.Println(pi.String()) // Output: "[1 : 5][10 : 15]"

Note: This method does not include the pageCount in the string representation.

func (*PageInterval) ToSlice

func (pi *PageInterval) ToSlice() []int

ToSlice converts the PageInterval into a slice of integers. It iterates through the intervals in the PageInterval and adds each page number to the slice. The resulting slice is returned.

Example:

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

slice := pi.ToSlice()
fmt.Println(slice) // Output: [1 2 3 4 5 10 11 12 13 14 15]

type PageIntervalIterator

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

PageIntervalIterator is a struct that allows iterating over the intervals in a PageInterval. It contains a slice of intervals, and an index and page number to keep track of the current position.

func (*PageIntervalIterator) Next

func (pii *PageIntervalIterator) Next() bool

Next advances the iterator to the next page in the PageInterval. It returns true if there is a next page, otherwise false. On the first call, it sets the page to the first page of the first interval. On subsequent calls, it increments the page number and checks if it is within the current interval. If the page number exceeds the current interval, it moves to the next interval.

func (*PageIntervalIterator) Value

func (pii *PageIntervalIterator) Value() int

Value returns the current page number in the iterator. It should be called after Next to get the current page number.

type PageIntervalReverseIterator

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

PageIntervalReverseIterator is a struct that allows iterating over the intervals in a PageInterval in reverse order. It contains a slice of intervals, and an index and page number to keep track of the current position.

func (*PageIntervalReverseIterator) Previous

func (piri *PageIntervalReverseIterator) Previous() bool

Previous moves the iterator to the previous page in the PageInterval. It returns true if there is a previous page, otherwise false. On the first call, it sets the page to the last page of the last interval. On subsequent calls, it decrements the page number and checks if it is within the current interval. If the page number is less than the current interval, it moves to the previous interval.

func (*PageIntervalReverseIterator) Value

func (piri *PageIntervalReverseIterator) Value() int

Value returns the current page number in the iterator. It should be called after Previous to get the current page number.

Jump to

Keyboard shortcuts

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