00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "load_binary_tree_from_forward_iterator.h"
00026
00027 #include <iostream>
00028 #include <string>
00029 #include <map>
00030 #include <iterator>
00031 #include <ext/slist>
00032
00033 using namespace std;
00034 using namespace __gnu_cxx;
00035
00038 struct employee {
00039 int ssn;
00040 string name;
00041
00042 bool operator< ( const employee& rhs) const
00043 {
00044 return ssn < rhs.ssn;
00045 }
00046
00047 friend ostream& operator << ( ostream& os, const employee& e);
00048 };
00049
00050 ostream& operator << ( ostream& os, const employee& e) {
00051 os << "{ " << e.ssn << ", " << e.name << "}";
00052 return os;
00053 }
00054
00055 std::pair<int,employee> create_map_pair( const employee& e)
00056 {
00057 return std::make_pair( e.ssn, e);
00058 }
00059
00060 ostream& operator << ( ostream& os, const std::pair<int,employee>& e) {
00061 os << e.second;
00062 return os;
00063 }
00064
00067 struct Create_Map_Pair {
00068 int id_offset;
00069
00070 Create_Map_Pair( int id_offset) : id_offset( id_offset)
00071 {
00072 }
00073
00074 Create_Map_Pair( const Create_Map_Pair& rhs) : id_offset( rhs.id_offset) {
00075 }
00076
00077 inline std::pair<int,employee> operator()( const employee& e)
00078 {
00079 return std::make_pair( e.ssn + id_offset, e);
00080 }
00081 };
00082
00083 employee workers[] = {
00084 { 1, "Jack"},
00085 { 2, "Harry"},
00086 { 3, "Joe"},
00087 { 4, "Jack"},
00088 { 5, "Harry"},
00089 { 6, "Joe"},
00090 { 7, "Jack"},
00091 { 8, "Harry"},
00092 { 9, "Joe"},
00093 };
00094
00095 int main() {
00096 map<int,employee> a_map;
00097
00098 size_t number_workers = sizeof(workers) / sizeof(employee);
00099 const slist<employee> Workers( &workers[0], &workers[number_workers]);
00100
00101 Create_Map_Pair cmp( 1000);
00102
00103 breadth_first_load_binary_tree_from_forward_iterator( Workers.begin(), Workers.end(), a_map, cmp);
00104
00105 cout << "results from calling functor cmp for a map" << endl;
00106 std::copy(a_map.begin(), a_map.end(), std::ostream_iterator<std::pair<int,employee> >(std::cout, "\n"));
00107
00108 breadth_first_load_binary_tree_from_forward_iterator( Workers.begin(), Workers.end(), a_map, create_map_pair);
00109
00110 cout << "results from calling function create_map_pair a map" << endl;
00111 std::copy(a_map.begin(), a_map.end(), std::ostream_iterator<std::pair<int,employee> >(std::cout, "\n"));
00112
00113 multimap<int,employee> a_multimap;
00114
00115 breadth_first_load_binary_tree_from_forward_iterator( Workers.begin(), Workers.end(), a_multimap, cmp);
00116
00117 cout << "results from calling functor cmp for a multimap" << endl;
00118 std::copy(a_multimap.begin(), a_multimap.end(), std::ostream_iterator<std::pair<int,employee> >(std::cout, "\n"));
00119
00120 cout << "results from calling function create_map_pair a multimap" << endl;
00121 std::copy(a_multimap.begin(), a_multimap.end(), std::ostream_iterator<std::pair<int,employee> >(std::cout, "\n"));
00122
00123
00124 return 0;
00125 }