|
Space-efficient geometric algorithms and data structuresBy Ilya Katz and Hervé Brönnimann |
00001 /* 00002 * Copyright © Ilya Katz and Herve Bronnimann, 2005. 00003 * 00004 * A beap_set is an adaptor that encapsulates functionality of the \c beap functions: 00005 * it povides the ability to insert and remove elements, inspection of the top and bottom elements. 00006 * Top element is the smallest element if default comparison is used. 00007 * 00008 * Beap_set is a container adaptor, meaning that it is implemented on top of some 00009 * underlying container type. By default that underlying type is vector, but a different 00010 * type may be selected explicitly. 00011 */ 00012 00013 #ifndef BEAP_SET_HPP 00014 #define BEAP_SET_HPP 00015 00016 //testing is not complete 00017 #include <vector> 00018 00019 namespace inplaceds 00020 { 00021 template <class _Tp, 00022 class _Sequence = std::vector<_Tp>, 00023 class _Compare = std::less<typename _Sequence::value_type> > 00024 class beap_set 00025 { 00026 00027 public: 00028 typedef typename _Sequence::value_type value_type; 00029 typedef typename _Sequence::size_type size_type; 00030 typedef _Sequence container_type; 00031 00032 typedef typename _Sequence::iterator iterator; 00033 typedef typename _Sequence::const_iterator const_iterator; 00034 00035 protected: 00036 _Sequence c; 00037 _Compare comp; 00038 00039 public: 00040 00046 beap_set(const value_type* first, const value_type* last) 00047 : c(first, last) 00048 { 00049 make_beap(c.begin(), c.end(), comp); 00050 } 00051 00056 beap_set(const value_type* first, const value_type* last, const Comp& compare) 00057 : c(first, last), comp(compare) 00058 { 00059 make_beap(c.begin(), c.end(), comp); 00060 } 00061 00065 reference find(_Tp x) 00066 { 00067 return find_beap(c.begin(), c.end(), x, comp); 00068 } 00069 00073 bool is_empty() 00074 { 00075 return empty(); 00076 } 00077 00080 const_iterator top() const 00081 { 00082 return min_beap(c.begin(), c.end(), comp); 00083 } 00084 00087 const_iterator bottom() const 00088 { 00089 return max_beap(c.begin(), c.end(), comp); 00090 } 00091 00094 void insert(_Tp x) 00095 { 00096 insert_beap(c.begin(), c.end(), comp); 00097 } 00098 00103 bool remove(_Tp x) 00104 { 00105 reference pos = find(x); 00106 if(pos!=c.end()) 00107 { 00108 remove_beap(c.begin(), pos ,c.end() ); 00109 return true; 00110 } 00111 00112 else 00113 { 00114 return false; 00115 } 00116 } 00117 00121 void sort(reference first) 00122 { 00123 typedef typename std::iterator_traits<RandomAccessIterator>::difference_type Distance; 00124 copy(c.begin(), c.end(), first); 00125 sort_beap(first, first+Distance(c.begin(), c.end()), comp); 00126 } 00127 00130 const_iterator end() 00131 { 00132 return c.end(); 00133 } 00134 }; 00135 }//namespace 00136 00137 #endif
Code Documentation generated Using Doxygen
Copyright © Ilya Katz and Hervé Brönnimann, 2005, 2006.