00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef LEMUR_DELETE_RANGE_HPP
00020 #define LEMUR_DELETE_RANGE_HPP
00021
00022 #include <vector>
00023 #include <stack>
00024 #include <map>
00025 namespace indri
00026 {
00027 namespace utility
00028 {
00029 template<typename VectorType>
00030 void delete_vector_contents( std::vector<VectorType>& vector ) {
00031 typedef typename std::vector<VectorType>::iterator vec_iter;
00032
00033 vec_iter begin = vector.begin();
00034 vec_iter end = vector.end();
00035 vec_iter iter;
00036
00037 for( iter = begin; iter != end; iter++ ) {
00038 delete *iter;
00039 }
00040
00041 vector.clear();
00042 }
00043
00044 template<typename MapKey, typename MapValue>
00045 void delete_map_contents( std::map<MapKey, MapValue*>& mp ) {
00046 typedef typename std::map<MapKey, MapValue*>::iterator map_iter;
00047
00048 map_iter begin = mp.begin();
00049 map_iter end = mp.end();
00050 map_iter iter;
00051
00052 for( iter = begin; iter != end; iter++ )
00053 delete iter->second;
00054
00055 mp.clear();
00056 }
00057
00058 template<typename VectorType>
00059 class VectorDeleter {
00060 private:
00061 std::vector<VectorType>* _vec;
00062
00063 public:
00064 VectorDeleter() : _vec(0) {}
00065 VectorDeleter( std::vector<VectorType>& vec ) : _vec(&vec) {}
00066 ~VectorDeleter() {
00067 if( _vec )
00068 delete_vector_contents<VectorType>( *_vec );
00069 }
00070
00071 void setVector( std::vector<VectorType>& vec ) {
00072 _vec = &vec;
00073 }
00074 };
00075
00076 template<typename StackType>
00077 class StackDeleter {
00078 private:
00079 std::stack<StackType*>* _stack;
00080
00081 public:
00082 StackDeleter() : _stack(0) {}
00083 StackDeleter( std::stack<StackType*>& stack ) : _stack(&stack) {}
00084 ~StackDeleter() {
00085 if( _stack ) {
00086 while( _stack->size() ) {
00087 delete _stack->top();
00088 _stack->pop();
00089 }
00090 }
00091 }
00092
00093 void setStack( std::stack<StackType*>& stack ) {
00094 _stack = &stack;
00095 }
00096 };
00097 }
00098 }
00099
00100 #endif // LEMUR_DELETE_RANGE_HPP
00101