load_binary_tree_from_forward_iterator.h File Reference
This file contains the code for the function template load_binary_tree_from_forward_iterator.
More...
Go to the source code of this file.
Detailed Description
This file contains the code for the function template load_binary_tree_from_forward_iterator.
Definition in file load_binary_tree_from_forward_iterator.h.
Function Documentation
template<class pnt , class mp , typename to_map_element >
| void breadth_first_load_binary_tree_from_forward_iterator |
( |
pnt |
begin, |
|
|
pnt |
end, |
|
|
mp & |
container, |
|
|
to_map_element |
tme | |
|
) |
| | [inline] |
Assumes:
-
class pnt is a forward iterator.
-
the class *pnt, has a less than operator, <.
-
the range from begin to end is sorted according to the < operator for *pnt
-
typename to_map_element, is a function which maps the dereferenced class of pnt, "*pnt", to the appropriate element type class mp.
-
class mp has a functions, empty, clean and insert and that insert is compatible with to_map_element.
-
class mp, uses some sort of tree structure to store data, which uses a < operator which is logically equivalent to the < operator for *pnt.
-
by repeatedly inserting median values, the tree structure of mp, is optimized for average best performance.
Changes: container
Description: This function populates "container" by repeatedly inserting the median values. Specifically, the first insert is for the median between begin and end. Subsequent inserts are for the medians between previous inserts and or begin or end. The medians are chosen in a breadth first search, so that the tree is approximately balanced at all points in time. This should prevent any unnecessary, and possibly non optimal, tree rotations in "container", if class mp automatically rebalances the tree.
Definition at line 44 of file load_binary_tree_from_forward_iterator.h.
00045 {
00046 unsigned long long len = 0;
00047
00048 pnt pdata = begin;
00049 for (; pdata != end; ++len, ++pdata)
00050 {
00051 }
00052
00053 pnt *pointer_array = new pnt[len];
00054 unsigned long long i, j;
00055 for (i=0, pdata=begin; i<len; ++i, ++pdata) {
00056 pointer_array[i] = pdata;
00057 }
00058
00059 if (!container.empty()) {
00060 container.clear();
00061 }
00062
00063 unsigned long long step_size = len;
00064 j = len;
00065 while(step_size > 0) {
00066 for (i = step_size >> 1; i < len; i += step_size) {
00067 if (pointer_array[i] != end) {
00068 container.insert( tme( *pointer_array[i]));
00069 pointer_array[i] = end;
00070 }
00071 }
00072 step_size >>= 1;
00073 }
00074
00075 delete[] pointer_array;
00076 }