1 // =========================================================================
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // =========================================================================
5 #include <TinyCon/Trie.h>
8 // -------------------------------------------------------------------------
16 // -------------------------------------------------------------------------
20 if( this->m_Extension != NULL )
21 delete this->m_Extension;
22 for( TChildren::value_type& v: this->m_Next )
23 if( v.second != NULL ) delete v.second;
26 // -------------------------------------------------------------------------
30 return( this->m_IsWord );
33 // -------------------------------------------------------------------------
38 if( this->m_Extension != NULL )
39 delete this->m_Extension;
40 this->m_Extension = new Self( );
41 return( this->m_Extension );
44 // -------------------------------------------------------------------------
49 return( this->m_Extension );
52 // -------------------------------------------------------------------------
57 return( this->m_Extension );
60 // -------------------------------------------------------------------------
62 setExtension( Self* extension )
64 if( this->m_Extension != NULL )
65 delete this->m_Extension;
66 this->m_Extension = extension;
69 // -------------------------------------------------------------------------
72 insert( const std::string& w )
75 for( const char& c: w )
77 if( n->m_Next.find( c ) == n->m_Next.end( ) )
78 n->m_Next[ c ] = new Trie( );
85 // -------------------------------------------------------------------------
87 words( std::vector< std::string >& options ) const
89 TChildren::const_iterator mIt = this->m_Next.begin( );
90 for( ; mIt != this->m_Next.end( ); ++mIt )
93 prefix.push_back( mIt->first );
94 if( !( mIt->second->m_IsWord ) )
96 std::vector< std::string > opts;
97 mIt->second->words( opts );
98 for( std::string& o: opts )
99 options.push_back( prefix + o );
102 options.push_back( prefix );
106 // -------------------------------------------------------------------------
107 std::pair< TinyCon::Trie::Self*, std::string >
109 find( const std::string& line, const std::size_t& p )
111 typedef std::pair< TinyCon::Trie::Self*, std::string > _TP;
113 if( p < line.size( ) )
115 TChildren::iterator mIt = this->m_Next.find( line[ p ] );
116 if( mIt != this->m_Next.end( ) )
118 _TP r = mIt->second->find( line, p + 1 );
120 s << mIt->first << r.second;
121 return( _TP( r.first, s.str( ) ) );
124 return( _TP( NULL, "" ) );
127 return( _TP( this, "" ) );
130 // -------------------------------------------------------------------------
131 std::pair< const TinyCon::Trie::Self*, std::string >
133 find( const std::string& line, const std::size_t& p ) const
135 typedef std::pair< const TinyCon::Trie::Self*, std::string > _TP;
137 if( p < line.size( ) )
139 TChildren::const_iterator mIt = this->m_Next.find( line[ p ] );
140 if( mIt != this->m_Next.end( ) )
142 _TP r = mIt->second->find( line, p + 1 );
144 s << mIt->first << r.second;
145 return( _TP( r.first, s.str( ) ) );
148 return( _TP( NULL, "" ) );
151 return( _TP( this, "" ) );