In this package, we use the term segment to refer to pointer values which reference a range in memory. We define two particular forms of segments:
- A basic segment is a value which consists of two pointers, namely
begin
and end
, that define the right-open interval, (begin, end]
.
- An enriched segment is a value which consists of a basic segment, along with a pointer, namely
middle
, which points at some location in between begin
and end
, such that begin <= middle < end
.
The following class defines a representation for enriched segments.
template <class Pointer>
class segment {
public:
using pointer_type = Pointer;
pointer_type begin;
pointer_type middle;
pointer_type end;
segment()
: begin(nullptr), middle(nullptr), end(nullptr) { }
segment(pointer_type begin, pointer_type middle, pointer_type end)
: begin(begin), middle(middle), end(end) { }
};
Example
#include <iostream>
#include <string>
#include <assert.h>
int main(
int argc,
const char * argv[]) {
const int chunk_size = 2;
std::cout << "mydeque contains:";
int* p = begin;
while (p != end)
std::cout << " " << *p++;
});
std::cout << std::endl;
iterator it = mydeque.
begin() + 3;
segment_type seg = it.get_segment();
std::cout << "the segment which contains mydeque[3] contains:";
int* p = seg.begin;
while (p != seg.end)
std::cout << " " << *p++;
std::cout << std::endl;
std::cout << "mydeque[3]=" << *seg.middle << std::endl;
return 0;
}
Output
mydeque contains: 0 1 2 3 4 5
the segment which contains mydeque[3] contains: 2 3
mydeque[3]=3