Overview

The RoseReal2DArray class is a lightweight, resizable array of floating point pairs, such as lists of UV points and 2D directions in the XY plane. It contains a simple array, but access and update methods work on pairs of numbers at a time. It also has overloads that work with RosePoint2D and RoseDirection2D. There is also a 3D version.

append()

void append(const double vec[2]);
void append(double x, double y);
void append(const RosePoint2D& pt);
void append(const RoseDirection2D& dir);

The append() function increments the size of the array and adds the given value pair as the last element. The function has overloads that take an array of two values, two separate values, as well as references to point and direction objects.

RoseReal2DArray o1;

double d1[] = { 1,2 };
double d2[] = { 4,5 };
RosePoint2D p1 (10, 20);

o1.append (d1);    
o1.append (d2);    
o1.append (p1);    
o1.append (100,101);

as_dir()

RoseDirection2D& as_dir (unsigned i);
const RoseDirection2D& as_dir (unsigned i) const;

The as_dir() function returns the value pair at a given index as a reference to a direction object. Constant and non-constant references are available. No copying is done, so the non-constant reference can be changed to update the value of the array. See as_point() for an example.

Because this is intended for use in tight graphics loops, no range checking is done on the index. Make sure that i is less than size() or you will get undefined behavior.

as_point()

RosePoint2D& as_point (unsigned i);
const RosePoint2D& as_point (unsigned i) const;

The as_point() function returns the value pair at a given index as a reference to a point object. Constant and non-constant references are available. No copying is done, so the non-constant reference can be changed to update the value of the array.

Because this is intended for use in tight graphics loops, no range checking is done on the index. Make sure that i is less than size() or you will get undefined behavior.

RoseReal2DArray o1;

o1.append (10, 20);    
o1.append (30, 40);    
o1.append (50, 60);    
// array contains (10, 20), (30, 40), (50, 60)

o1.as_point(0).x(98);
o1.as_point(0).y(99);
// array contains (98, 99), (30, 40), (50, 60)


// This copies the (98, 99)
RosePoint2D a_copy = o1.as_point(0);
a_copy.xy(10,20);

// only the copy was changed
// array contains (98, 99), (30, 40), (50, 60)

capacity()

unsigned capacity() const;
void capacity (unsigned newcap);

The capacity() function returns the number of pairs that the memory buffer can hold without resizing. Functions like append() expand the capacity as needed. You can manually set the capacity to pre-allocate a buffer of a certain size. Set capacity to zero to deallocate buffer, otherwise this only expands capacity, never decreases it.

empty()

void empty();

The empty() function removes all elements by setting the size of the array to zero. The capacity of the array is not changed.

expand()

void expand (unsigned min_extra = 1);

The expand() function forces the capacity to increase by a certain number of pairs.

get() / operator[]

double * operator[] (unsigned i);
const double * get (unsigned i) const;

The get() function and the index operator[] get the value pair at a given index. Both return pointer to double, which is the start of a two element array. No copying is done, the return value is a pointer into the raw data buffer. Use the get() function for read-only access because it returns a constant pointer. The index operator is not constant, so you can change the values in place.

Because this is intended for use in tight graphics loops, no range checking is done on the index. Make sure that i is less than size() or you will get undefined behavior.

The as_point() and as_dir() functions also get the value pair at a given index, but as a reference to a point or direction object.

RoseReal2DArray o1;

o1.append (10, 20);    
o1.append (30, 40);    
o1.append (50, 60);    
// array contains (10, 20), (30, 40), (50, 60)

// read-only access
double x, y;
x = o1.get(0)[0]; // is 10
y = o1.get(0)[1]; // is 20

const double * pt = o1.get(0);
x = pt[0]; // is 10
y = pt[1]; // is 20


// read-write access using index operator
x = o1[0][0]; // is 10
y = o1[0][1]; // is 20

// update values in place 
o1[0][0] = 97;
o1[0][1] = 98;
// array now contains (97, 98), (30, 40), (50, 60)

insert()

void insert(const double vec[2], unsigned i);
void insert(double x, double y, unsigned i);
void insert(const RosePoint2D& pt, unsigned i);
void insert(const RoseDirection2D& dir, unsigned i);

The insert() function increments the size of the array and inserts the value pair at the given position. All values previously at that position and beyond are moved one index position higher. Inserting at position size() or higher is treated as an append().

The function has overloads that take an array of two values, two separate values, as well as references to point and direction objects.

put()

void put(const double vec[2], unsigned i);
void put(double x, double y, unsigned i);
void put(const RosePoint2D& v, unsigned i);
void put(const RoseDirection2D& v, unsigned i);

The put() function changes a value pair at a given position. You can provide the replacement pair as an array of two values, two separate values, or as a reference to point or direction object. You can also change the value by updating through the index operator or the non-const references returned by the as_point() and as_dir() functions.

Because this is intended for use in tight graphics loops, no range checking is done on the index. Make sure that i is less than size() or you will get undefined behavior.

RoseReal2DArray o1;

o1.append (10, 20);    
o1.append (30, 40);    
o1.append (50, 60);    
// array contains (10, 20), (30, 40), (50, 60)


double foo[2] = { 200, 300 };

o1.put(foo, 1);
// array contains (10, 20), (200, 300), (50, 60)

o1.as_point(1).xy(111, 222);
// array contains (10, 20), (111, 222), (50, 60)

o1[1][0] = 95;
o1[1][1] = 96;
// array contains (10, 20), (95, 96), (50, 60)

remove()

void remove(unsigned i);

The remove() function removes the value pair at a given index, shifting all subsequent values pairs and decrementing the size of the array.

size()

unsigned size() const;
void size(unsigned sz);

The size() function returns the number of value pairs held by the array. Changing the size of the array will increase the capacity() if needed and set the size of the array to the new number of pairs. You must initialize the values manually if you increase the size of the array.