X-Git-Url: https://git.llucax.com/z.facultad/75.68/celdas.git/blobdiff_plain/34ad058e6f661d41b843ab44d1f2dcae8f77a5e1..HEAD:/trunk/src/indicemagico.h diff --git a/trunk/src/indicemagico.h b/trunk/src/indicemagico.h index 7b28472..9ad336e 100644 --- a/trunk/src/indicemagico.h +++ b/trunk/src/indicemagico.h @@ -8,63 +8,48 @@ #include -#include -#include +#include +#include +#include // FIXME template class CIndiceMagico { public: - std::string* m_nombres ; - T* m_datos ; - unsigned m_cant ; + std::vector< std::string > m_nombres ; + std::vector< T > m_datos ; + typedef typename std::vector< T >::iterator iterator; public: - CIndiceMagico() - { - m_cant = 0 ; - m_nombres = new std::string[MAX_ELEMENTOS]; - m_datos = new T[MAX_ELEMENTOS]; + iterator begin() { return m_datos.begin(); } - if (!m_nombres) - std::cerr << "No se pudo allocar el arreglo CIndiceMagico::m_nombres.\n"; + iterator end() { return m_datos.end(); } - if (!m_datos) - std::cerr << "No se pudo allocar el arreglo CIndiceMagico::m_datos.\n"; - - } - - ~CIndiceMagico() - { - delete m_nombres; - delete m_datos; - } - -public: - void add(const char* nombre, const T dato) + void add(const std::string& nombre, const T& dato) { //Si ya existía, lo borro if (exist(nombre)) remove(nombre) ; - - m_nombres[m_cant] = nombre ; - m_datos[m_cant++] = dato ; + m_nombres.push_back(nombre); + m_datos.push_back(dato); } void add(CIndiceMagico& indice) { - for (unsigned i=0; i. //POS: Retorna el elemento. - T& find(const char* nombre) + T& find(const std::string& nombre) { unsigned i; - for (i=0; i +inline +std::ostream& operator<< (std::ostream& os, CIndiceMagico& im) +{ + unsigned c = im.count(); +#ifdef LOG + if (c-- == 0) return os << ""; + for (unsigned i = 0; i < c; ++i) + os << im[i] << ","; + os << im[c]; + return os; +#else // ! LOG + if (c == 0) return os << "\t\tvacío\n"; + for (unsigned i = 0; i < c; ++i) + os << "\t\t" << im.keys(i) << ":\t" << im[i] << "\n"; + return os; +#endif // LOG +} + +template < typename T > +inline +std::ostream& operator<< (std::ostream& os, CIndiceMagico& im) +{ + unsigned c = im.count(); +#ifdef LOG + if (c-- == 0) return os << ""; + for (unsigned i = 0; i < c; ++i) + os << *im[i] << ","; + os << *im[c]; + return os; +#else // ! LOG + if (c == 0) return os << "\t\tvacío\n"; + for (unsigned i = 0; i < c; ++i) + os << "\t\t" << im.keys(i) << ":\t" << *im[i] << "\n"; + return os; +#endif // LOG +} #endif