• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/StringTable.cpp

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/math/Math.h>
00018 #include <spl/collection/StringTable.h>
00019 
00020 StringTable::StringTable()
00021 : m_stringIdx(), m_strings(), m_lists()
00022 {
00023 }
00024 
00025 StringTable::StringTable(const StringTable& st)
00026 : m_stringIdx(), m_strings(), m_lists()
00027 {
00028         *this = st;
00029 }
00030 
00031 
00032 StringTable::~StringTable()
00033 {
00034         Clear();
00035 }
00036 
00037 StringTable& StringTable::operator =(const StringTable& st)
00038 {
00039         Clear();
00040         for ( int x = 0; x < st.m_strings.Count(); x++ )
00041         {
00042                 GetPtr(st.m_strings.ElementAt(x));
00043         }
00044 
00045         return *this;
00046 }
00047 
00048 void StringTable::Clear()
00049 {
00050         int x;
00051 
00052         m_stringIdx.Clear();
00053 
00054         for ( x = 0; x < m_strings.Count(); x++ )
00055         {
00056                 delete m_strings.ElementAt(x);
00057         }
00058         for ( x = 0; x < m_lists.Count(); x++ )
00059         {
00060                 delete m_lists.ElementAt(x);
00061         }
00062         m_strings.Clear();
00063         m_lists.Clear();
00064 }
00065 
00066 int StringTable::PositionOf(const String& str)
00067 {
00068         GetPtr(str);
00069 
00070         for ( int x = 0; x < m_strings.Count(); x++ )
00071         {
00072                 if (m_strings.ElementAt(x)->Equals(str))
00073                 {
00074                         return x;
00075                 }
00076         }
00077         throw new Exception("Internal error at StringTable::PositionOf");
00078 }
00079 
00080 String *StringTable::GetPtr(const char *cp)
00081 {
00082         Vector<String *> *lst;
00083         int hash = Math::Hash(cp);
00084         if ( !m_stringIdx.ContainsKey(hash) )
00085         {
00086                 lst = new Vector<String *>();
00087                 m_lists.Add(lst);
00088                 m_stringIdx.Set(hash, lst);
00089         }
00090         else
00091         {
00092                 lst = m_stringIdx.Get(hash);
00093         }
00094         ASSERT_MEM(lst, sizeof(Vector<String *>));
00095 
00096         for (int x = 0; x < lst->Count(); x++)
00097         {
00098                 if (lst->ElementAt(x)->Equals(cp))
00099                 {
00100                         return lst->ElementAt(x);
00101                 }
00102         }
00103         String *str = new String(cp);
00104         m_strings.Add(str);
00105         lst->Add(str);
00106         return str;
00107 }
00108 
00109 #ifdef DEBUG
00110 void StringTable::ValidateMem() const
00111 {
00112         m_stringIdx.ValidateMem();
00113         m_strings.ValidateMem();
00114         m_lists.ValidateMem();
00115 }
00116 
00117 void StringTable::CheckMem() const
00118 {
00119         m_stringIdx.CheckMem();
00120         m_strings.CheckMem();
00121         m_lists.CheckMem();
00122 }
00123 #endif
00124