Class Range
Range is a class that defines an interval of items, such as an interval of characters in a string or ints in a vector.
Range has members first and size. They uniquely define the interval. First is the position (or index) of the first item belonging to the interval,
and size is the length of the interval. First and size must be non-negative numbers. You may omit giving the size parameter, which means that you want
the interval span till the string (or vector etc.) end.
Range is an auxiliar class. For instance, it is given as a parameter in string methods to limit the search in a specific range,
or it is given as a parameter for vector method remove, to specify the range to be removed.
CONSTRUCTION
COMPARISION
OTHER METHODS
main
string s = "abcdef"
// Specify range consisting of characters 'b', 'c' and 'd'
range r = 1 size 3
// Specify range consisting of all characters starting from 'c'
r = 2 to s.get size
// Use range in the string methods for instance
s[r] = ""
print s // LOG: ab
(range) <int first> size <int size>
Creates a new range with the given first position and size.
Parameters
- first - First position
- size - Size
Exceptions
- InvalidArgumentException - If any given value is negative
main
range r = 0 size 10 // Includes 10 elements, starting from the first element
Creates a new range from the given first position up to the last element.
Parameters
- first - First position
- last - The index of last element in the range.
Exceptions
- InvalidArgumentException - If any given value is negative or last comes before first
main
range r = 5 to 10 // Includes 6 elements, starting from element at index 5.
(range) <int first> to end
Creates a new range with the given first position. The range consists all elements starting at 'first' till end
Parameters
- first - The first position.
Exceptions
- InvalidArgumentException - If the given value is negative
(range) <range sourceRange>.copy
Creates a new range that is an exact copy of sourceRange.
Parameters
- sourceRange - The range object that will be copied
Return value
main
range r1 = 0 size 10
range r2 = r1.copy // Includes 10 elements, starting from the first element
Comparision
Parameters
- r1 - Operand
- r2 - Operand
Return value
- true, if operands are equal, otherwise false.
main
range r1 = 0 size 10
range r2 = 5 to 10
bool result = r1 == r2 // result == false
Comparision
Parameters
- r1 - Operand
- r2 - Operand
Return value
- true, if operands are not equal, otherwise false.
main
range r1 = 0 size 10
range r2 = 5 to 10
bool result = r1 != r2 // result == true
(int) get first
Get the value of the first position.
Return value
main
range r = 0 size 10
int firstElement = r.get first // firstElement == 0
(int) get size
Get the value of the size.
Return value
main
range r = 5 to 10
int size = r.get size // size == 6
set first to <int newFirst>
Assing a new value for the first position.
Parameters
Exceptions
- InvalidArgumentException - If the new value is negative
main
range r = 0 size 10 // Includes 10 elements, starting from the first element
r.set first to 1 // Includes 9 elements, starting from element at index 1
set size to <int newSize>
Assing a new value for the size.
Parameters
Exceptions
- InvalidArgumentException - If the new value is negative
main
range r = 0 size 10 // Includes 10 elements, starting from the first element
r.set size to 5 // Includes 5 elements, starting from the first element