/* FILE: hashmap.cpp * DATE MODIFIED: 31-Aug-07 * DESCRIPTION: Helper template for compiling FST data structure * from a GRXML file. * A doubly indexed map class using two maps. * Indices are a user-defined type and an int index. * Both are unique indices. * The int index has automatic non-reusable numbering. * * DESCRIPTION: Part of the SREC graph compiler project source files. * * Copyright 2007, 2008 Nuance Communciations, Inc. * * * * Licensed under the Apache License, Version 2.0 (the 'License'); * * you may not use this file except in compliance with the License. * * * * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an 'AS IS' BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * * * *---------------------------------------------------------------------------*/ #include #include #include #include #include "hashmap.h" #include "sub_grph.h" using namespace std; template HashMap::HashMap(): m_NextAutoIndex(0) { } template void HashMap::setName(std::string s) { m_Name = s; } template bool HashMap::insert( T1 const & index, T2 const & value) { pair::iterator,bool> result = m_Map.insert( make_pair(index, value) ); if (!result.second) { return false; } return true; } template bool HashMap::remove( T1 const & index ) { m_Map.erase( index ); return true; } template bool HashMap::isEmpty() { return m_Map.empty(); } template bool HashMap::clear() { m_Map.clear(); return true; } template bool HashMap::getIndex( T2 const & value, T1 *index ) { //do something with all elements having a certain value typename std::map::iterator pos; for (pos = m_Map.begin(); pos != m_Map.end(); ++pos) { if (pos->second == value) { *index = (pos->first); return true; } } return false; } template bool HashMap::getFirst( T1 *index, T2 *value ) { if (m_Map.empty() ) { return false; } //do something with all elements having a certain value typename std::map::iterator pos; m_pPos= m_Map.begin(); *index = m_pPos->first; *value= m_pPos->second; return true; } template bool HashMap::getNext( T1 *index, T2 *value ) { if ( m_Map.empty() ) { return false; } if ( ++m_pPos == m_Map.end() ) { return false; } *index = m_pPos->first; *value= m_pPos->second; return true; } template bool HashMap::getValue(T1 const & index, T2 *value) { typename std::map::iterator pos; pos = m_Map.find(index); if (m_Map.end() != pos) { *value = pos->second; return true; } return false; } template int HashMap::size() { return m_Map.size(); } template void HashMap::print() { typename std::map::iterator pos; cout << "======= '" << m_Name <<"' =======" << std::endl; for (pos = m_Map.begin(); pos != m_Map.end(); ++pos) { cout << pos->first <<" : " << pos->second << std::endl; } } template void HashMap::writeFile( std::string fileName ) { ofstream outfile; outfile.open ( fileName.c_str() ); typename std::map::iterator pos; for (pos = m_Map.begin(); pos != m_Map.end(); ++pos) { outfile << pos->first << " " << pos->second << std::endl; } outfile.close(); } template typename std::map::iterator HashMap::begin() { m_pPos = m_Map.begin(); return m_pPos; } template typename std::map::iterator HashMap::end() { m_pPos = m_Map.end(); return m_pPos; } // Declare known data types so that we don't need to put this in hashmap.h. // If user needs others the put the declaration in a separate user file. template class HashMap; template class HashMap; template class HashMap; template class HashMap; template class HashMap*>; template class HashMap;