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

xst_dictionary_translator.h

Go to the documentation of this file.
00001 
00002 // Common Text Transformation Library
00003 // Copyright (C) 1997-2006 by Igor Kholodov. 
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the
00017 // Free Software Foundation, Inc.,
00018 // 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 // mailto:cttl@users.sourceforge.net
00021 // http://sourceforge.net/projects/cttl/
00023 
00033 // xst_translator.h
00034 
00035 #ifndef _XST_DICTIONARY_TRANSLATOR_H_INCLUDED_
00036 #define _XST_DICTIONARY_TRANSLATOR_H_INCLUDED_
00037 
00038 namespace cttl_impl {
00039 
00062 template< typename SequenceT, typename MapT >
00063 class xst_dictionary_translator
00064 {
00065 public:
00067     typedef typename SequenceT::value_type value_T;
00068 
00069 private:
00071     SequenceT& m_sequence;
00072 
00074     MapT& m_map;
00075 
00076 public:
00078     xst_dictionary_translator( SequenceT& vector_, MapT& map_ )
00079         :
00080     m_sequence( vector_ ),
00081     m_map( map_ )
00082     {
00083     }
00084 
00086     xst_dictionary_translator( xst_dictionary_translator< SequenceT, MapT > const& other_ )
00087         :
00088     m_sequence( other_.m_sequence ),
00089     m_map( other_.m_map )
00090     {
00091     }
00092 
00094     void operator=( xst_dictionary_translator< SequenceT, MapT > const& ) const
00095     {
00096     }
00097 
00107     template< typename InputValueT >
00108     int operator() ( InputValueT const& value_ )
00109     {
00110         int id = m_sequence.size();
00111         // is this value already in the dictionary ?
00112         typename MapT::const_iterator it = m_map.find( value_T( value_ ) );
00113         if ( it == m_map.end() ) {
00114             // no, this value is new,
00115             // generate new identity
00116             m_map.insert(
00117                 std::make_pair( value_, id )
00118                 );
00119             m_sequence.push_back( value_T( value_ ) );
00120             //assert( m_sequence.size() == m_map.size() );
00121 
00122         } else
00123             // yes, value already exists, return its identity:
00124             id = it->second;
00125 
00126         trace_deposit( id );
00127         return id;
00128     }
00129 
00130 private:
00132     void trace_deposit( int id_ )
00133     {
00134         CTTL_TRACE_DEPOSIT_TEXT( "dictionary[" );
00135         CTTL_TRACE_DEPOSIT_TEXT( id_ );
00136         CTTL_TRACE_DEPOSIT_TEXT( "]=\"" );
00137         CTTL_TRACE_DEPOSIT_TEXT( m_sequence[ id_ ] );
00138         CTTL_TRACE_DEPOSIT_TEXT( "\" " );
00139     }
00140 
00141 }; // class xst_dictionary_translator
00142 
00143 
00144 }   // namespace cttl_impl
00145 
00146 
00147 #endif //_XST_DICTIONARY_TRANSLATOR_H_INCLUDED_

Generated on Thu Nov 2 17:48:22 2006 for CTTL Lambda Expression by  doxygen 1.3.9.1