Main Page   Class Hierarchy   Compound List   File List   Compound Members  

C_IdGenerator.cpp

00001 /*
00002  *  This program is free software; you can redistribute it and/or modify
00003  *  it under the terms of the GNU General Public License as published by
00004  *  the Free Software Foundation; either version 2 of the License, or
00005  *  (at your option) any later version.
00006  *
00007  *  This program is distributed in the hope that it will be useful,
00008  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00009  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010  *  GNU General Public License for more details.
00011  *
00012  *  You should have received a copy of the GNU General Public License
00013  *  along with this program; if not, write to the Free Software
00014  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00015  *
00016  * (c)Copyright 2006 Hewlett-Packard Development Company, LP.
00017  *
00018  */
00019 
00020 #include "C_IdGenerator.hpp"
00021 #include "Utils.hpp"
00022 
00023 #include "GeneratorTrace.hpp"
00024 
00025 #include "list_t.hpp"
00026 #include "set_t.hpp"
00027 
00028 typedef list_t<int> T_IdList ;
00029 typedef set_t<int>  T_IdSet  ;
00030 
00031 typedef struct _struct_id_gen {
00032   int      m_nb_id_used ;
00033   T_IdList *m_available_ids ;
00034   T_IdSet  *m_used_ids ;
00035 } T_IdGen, *T_pIdGen ;
00036 
00037 
00038 C_IdGenerator::C_IdGenerator() {
00039   T_pIdGen L_gen ;
00040   ALLOC_VAR(L_gen, T_pIdGen, sizeof(T_IdGen));
00041   m_impl = L_gen ;
00042   NEW_VAR(L_gen->m_available_ids, T_IdList());
00043   NEW_VAR(L_gen->m_used_ids, T_IdSet());
00044   L_gen->m_used_ids->clear();
00045   L_gen->m_available_ids->clear();
00046   L_gen->m_nb_id_used = 0 ;
00047 }
00048 
00049 C_IdGenerator::~C_IdGenerator() {
00050   T_pIdGen L_gen = (T_pIdGen) m_impl;
00051   
00052   if (!L_gen->m_used_ids->empty()) {
00053     L_gen->m_used_ids->erase(L_gen->m_used_ids->begin(),
00054                             L_gen->m_used_ids->end());
00055   }
00056   if (!L_gen->m_available_ids->empty()) {
00057     L_gen->m_available_ids->erase(L_gen->m_available_ids->begin(),
00058                                  L_gen->m_available_ids->end());
00059   }
00060   L_gen->m_nb_id_used = 0;
00061 
00062   DELETE_VAR(L_gen->m_available_ids);
00063   DELETE_VAR(L_gen->m_used_ids);
00064 
00065   FREE_VAR(L_gen);
00066   m_impl = NULL ;
00067 }
00068 
00069 int  C_IdGenerator::new_id () {
00070 
00071   T_pIdGen           L_gen = (T_pIdGen) m_impl;
00072   T_IdList::iterator L_it ;
00073   int                L_id ;
00074 
00075   if (!L_gen->m_available_ids->empty()) {
00076     L_it = L_gen->m_available_ids->begin() ;
00077     L_id = *L_it ;
00078     L_gen->m_available_ids->erase(L_it);
00079   } else {
00080     L_id = L_gen->m_nb_id_used ;
00081     (L_gen->m_nb_id_used)++ ;
00082   }
00083   L_gen->m_used_ids->insert(L_id);
00084 
00085   return (L_id);
00086 } 
00087 
00088 int  C_IdGenerator::release_id (int P_id) {
00089 
00090   T_pIdGen          L_gen = (T_pIdGen) m_impl;
00091   int               L_ret = -1 ;
00092   T_IdSet::iterator L_it ;
00093 
00094   L_it = L_gen->m_used_ids->find(P_id) ;
00095   if (L_it != L_gen->m_used_ids->end()) {
00096     L_gen->m_used_ids->erase(L_it);
00097     L_gen->m_available_ids->push_back(P_id);
00098     L_ret = 0 ;
00099   }
00100   return (L_ret);
00101 }

Generated on Wed Mar 7 14:44:50 2007 for Seagull by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002