CCCC - C and C++ Code Counter  9999-git
CCCC Development version (post-3.1.4)
cccc_tbl.cc
Go to the documentation of this file.
1 /*
2  CCCC - C and C++ Code Counter
3  Copyright (C) 1994-2005 Tim Littlefair (tim_littlefair@hotmail.com)
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 // cccc_tbl.cc
20 #ifndef _CCCC_TBL_BODY
21 #define _CCCC_TBL_BODY
22 
23 #include "cccc_itm.h"
24 #include "cccc_tbl.h"
25 #include <cassert>
26 
27 #define LINE_BUFFER_SIZE 1000
28 
29 
30 template <class T> CCCC_Table<T>::CCCC_Table()
31 : sorted(true)
32 {
33  iter_ = map_t::end();
34 }
35 
36 template <class T> CCCC_Table<T>::~CCCC_Table()
37 {
38  // the container should manage the destruction of its own
39  // nodes correctly, we just need to get rid of the
40  // objects to which we hold pointers.
41  // NB Although CCCC_Table holds pointers, it owns the
42  // objects they point to and is responsible for their disposal.
43  T* itemptr=first_item();
44  while(itemptr!=NULL)
45  {
46  delete itemptr;
47  itemptr=next_item();
48  }
49 }
50 
51 template<class T>
52 int CCCC_Table<T>::get_count(const char* count_tag)
53 {
54  int retval=0;
55  T* itemptr=first_item();
56  while(itemptr!=NULL)
57  {
58  retval+=itemptr->get_count(count_tag);
59  itemptr=next_item();
60  }
61 
62  return retval;
63 }
64 
65 template<class T>
66 T* CCCC_Table<T>::find(string name)
67 {
68  T *retval=NULL;
69  typename map_t::iterator value_iterator=map_t::find(name);
70  if(value_iterator!=map_t::end())
71  {
72  retval=(*value_iterator).second;
73  }
74  return retval;
75 }
76 
77 template<class T>
78 T* CCCC_Table<T>::find_or_insert(T* new_item_ptr)
79 {
80  string new_key=new_item_ptr->key();
81  T *retval=find(new_key);
82  if(retval==NULL)
83  {
84  typename map_t::value_type new_pair(new_key,new_item_ptr);
85  map_t::insert(new_pair);
86  sorted=false;
87  retval=new_item_ptr;
88  }
89  return retval;
90 }
91 
92 template<class T>
93 bool CCCC_Table<T>::remove(T* old_item_ptr)
94 {
95  bool retval=false;
96  typename map_t::iterator value_iterator=map_t::find(old_item_ptr->key());
97  if(value_iterator!=map_t::end())
98  {
99  this->erase(value_iterator);
100  retval=true;
101  }
102  return retval;
103 }
104 
105 template <class T> void CCCC_Table<T>::sort()
106 {
107  if(sorted==false)
108  {
109  sorted=true;
110  }
111 }
112 
113 template <class T> void CCCC_Table<T>::reset_iterator()
114 {
115  iter_=map_t::begin();
116 }
117 
118 template <class T> T* CCCC_Table<T>::first_item()
119 {
120  reset_iterator();
121  return next_item();
122 }
123 
124 template <class T> T* CCCC_Table<T>::next_item()
125 {
126  T* retval=NULL;
127  if(iter_!=map_t::end())
128  {
129  retval=(*iter_).second;
130  iter_++;
131  }
132  return retval;
133 }
134 
135 template <class T> int CCCC_Table<T>::records()
136 {
137  return map_t::size();
138 }
139 
140 #endif // _CCCC_TBL_BODY
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
void sort()
Definition: cccc_tbl.cc:105
CCCC_Table()
Definition: cccc_tbl.cc:30
map_t::iterator iter_
Definition: cccc_tbl.h:41
virtual ~CCCC_Table()
Definition: cccc_tbl.cc:36
void reset_iterator()
Definition: cccc_tbl.cc:113
bool remove(T *old_item_ptr)
Definition: cccc_tbl.cc:93
virtual int get_count(const char *count_tag)
Definition: cccc_tbl.cc:52
T * first_item()
Definition: cccc_tbl.cc:118
T * find_or_insert(T *new_item_ptr)
Definition: cccc_tbl.cc:78
T * next_item()
Definition: cccc_tbl.cc:124
T * find(string name)
Definition: cccc_tbl.cc:66
int records()
Definition: cccc_tbl.cc:135