Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

delete_range.hpp

Go to the documentation of this file.
00001 /*==========================================================================
00002  * Copyright (c) 2004 University of Massachusetts.  All Rights Reserved.
00003  *
00004  * Use of the Lemur Toolkit for Language Modeling and Information Retrieval
00005  * is subject to the terms of the software license set forth in the LICENSE
00006  * file included with this software, and also available at
00007  * http://www.lemurproject.org/license.html
00008  *
00009  *==========================================================================
00010  */
00011 
00012 
00013 //
00014 // delete_range
00015 //
00016 // 5 March 2004 -- tds
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 

Generated on Tue Jun 15 11:02:53 2010 for Lemur by doxygen 1.3.4