Constructors:
- RNArray
(void); Array Properties:
- const RNBoolean IsEmpty(void) const;
- const int NEntries(void) const;
Entry Properties:
- const int EntryIndex(const RNArrayEntry *entry) const;
- PtrType& EntryContents(RNArrayEntry *entry) const;
Data Access Functions:
- PtrType& Head(void) const;
- PtrType& Tail(void) const;
- PtrType& Kth(int k) const;
- PtrType& operator[](int k) const;
Entry Access Functions:
- RNArrayEntry *HeadEntry(void) const;
- RNArrayEntry *TailEntry(void) const;
- RNArrayEntry *KthEntry(int k) const;
- RNArrayEntry *PrevEntry(const RNArrayEntry *entry) const;
- RNArrayEntry *NextEntry(const RNArrayEntry *entry) const;
- RNArrayEntry *FindEntry(const Type& data) const;
Insertion Functions:
- RNArrayEntry *InsertHead(const PtrType& data);
- RNArrayEntry *InsertTail(const PtrType& data);
- RNArrayEntry *InsertKth(const PtrType& data, int k);
- RNArrayEntry *InsertBefore(const PtrType& data, RNArrayEntry *entry);
- RNArrayEntry *InsertAfter(const PtrType& data, RNArrayEntry *entry);
- RNArrayEntry *Insert(const PtrType& data);
Removal Functions:
- void RemoveHead(void);
- void RemoveTail(void);
- void RemoveKth(int k);
- void RemoveEntry(RNArrayEntry *entry);
- void Remove(const PtrType& data);
Manipulation Functions:
- void Empty(void);
- void Truncate(int length);
- void Shift(int delta);
- void Shift(int start, int length, int delta);
- void Reverse(void);
- void Reverse(int start, int length);
Description:
RNArray<PtrType> is implemented as a templated class that stores ordered entries of a specified pointer data type. The storage allocation for an array is dynamic -- it expands and contracts as entries are inserted and removed. Once an array has been constructed, entries and the data stored in them can be accessed, rearranged, or modified via member functions. For example, an array of three strings could be constructed and manipulated as follows.
// Construct the array RNArray<char *> array; // Creates an empty array array.Insert("String1"); // Insert the 1st string array.Insert("String2"); // Insert the 2nd string array.Insert("String3"); // Insert the 3rd string // Access entries char *data; data = array.Head(); // data == "String1" data = array.Tail(); // data == "String3" data = array.Kth(0); // data == "String1" data = array.Kth(1); // data == "String2" data = array[1]; // data == "String2" // Access all entries for (int i = 0; i < array.NEntries(); i++) { char *data = array[i]; // or array.Kth(i); printf("%s/n", data); } // Manipulate the array array.RemoveKth(1); // Remove the 2nd entry (first entry is at index 0) array.InsertKth("String2", 1); // Re-insert string in 2nd entry
Constructors:
- RNHeap
(PtrType base, RNScalar *value_ptr, PtrType **entry_ptr = NULL, int least_first = TRUE); Heap Properties:
- const RNBoolean IsEmpty(void) const;
- const int NEntries(void) const;
Data Access Functions:
- PtrType& Peak(void) const;
- PtrType& Kth(int k) const;
- PtrType& operator[](int k) const;
Manipulation Functions:
- void Push(PtrType data);
- PtrType Pop(void);
- void Update(PtrType data);
- void Empty(void);
Description:
RNHeap<PtrType> is implemented as a templated class that stores entries of a specified pointer data type in a priority queue that makes access to the entry with least/greatest "value" efficient. The storage allocation for these containers is dynamic -- it expands and contracts as entries are pushed and popped. The heap also allows random access to its entries via the Kth() member function. The RNHeap class must be able to sort its members, and thus it requires a way to access the "value" of any data entry. This is done by providing a pointer to an example data entry (base) and a pointer to that member's value (value_ptr). The difference between these pointers is used to infer the offset in the member struct that contains the value of the entry -- this value must be a RNScalar field of the struct pointed to by PtrType. Similarly, in order to enable efficient updates to the heap when values change, the constructor allows specification of the location of a "backpointer" into the heap for each entry. The mechanism for specifying the location of the backpointer (entry_ptr) is the same as for the value_ptr. The following code provides an example:
// Data type definition struct Data { RNScalar value; struct Data **heapentry; }; // Create heap Data tmp; RNHeap heap(&tmp, &tmp.value, &tmp.heapentry); // Perform a bunch of pushes for (int i = 0; i < 100; i++) { Data *data = new Data(); data->value = RNRandomScalar(); heap.Push(data); } // Perform a bunch of pops while (!heap.IsEmpty()) { Data *data = heap.Pop(); // Do something here }Click this to see a complete sample program.
funk@cs.princeton.edu