summaryrefslogtreecommitdiff
path: root/SA_POP_Utils.cpp
blob: e018ccc3109466b31df131c5140fc3ec48a20eca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//
// $Id$
//

//=============================================================================
/**
 * @file  SA_POP_Types.cpp
 *
 * This file contains the implementation of class types used throughout SA-POP.
 *
 * @author  John S. Kinnebrew <john.s.kinnebrew@vanderbilt.edu>
 */
//=============================================================================

#ifndef SA_POP_UTILS_CPP_
#define SA_POP_UTILS_CPP_

#include "SA_POP_Utils.h"

//using namespace SA_POP;

template<typename L, typename T>
SA_POP::ListMultiMap<L,T>::ListMultiMap (SA_POP::FrontBack append_direction, SA_POP::FrontBack remove_direction)
: append_dir_ (append_direction),
remove_dir_(remove_direction)
{
    multimap_.clear();
    list_.clear ();
};


// Map Insert
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::insert(const typename SA_POP::ListMultiMap<L,T>::value_type val)
{
  
  if(append_dir_ == SA_POP::BACK)
  {
    this->list_.push_back(val);
  }
  else if(append_dir_ == SA_POP::FRONT)
  {
    this->list_.push_front(val);
  }
  else 
  {
    throw "ListMultiMap<L,T>::iterator ListMultiMap<L,T>::insert(const typename ListMultiMap<L,T>::value_type val): Unknown direction to append.";
  }

  return this->multimap_.insert (val);
  

};

    
// Map Erase
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::size_type SA_POP::ListMultiMap<L,T>::erase(const typename SA_POP::ListMultiMap<L,T>::key_type& key)
{

  //Removal all list items with matching key in the pair

  for(SA_POP::ListMultiMap<L,T>::list_iterator l_iter = list_.begin(); l_iter != list_.end();)
  {
    SA_POP::ListMultiMap<L,T>::list_iterator prev_iter = l_iter;
    l_iter++;
    if((*prev_iter).first == key)
    {
      this->list_.erase(prev_iter);
    }
  }


  return this->multimap_.erase (key);
};

template<typename L, typename T>
void SA_POP::ListMultiMap<L,T>::erase(typename SA_POP::ListMultiMap<L,T>::iterator _Where)
{
  if(_Where != this->end())
  {
      //Removal first or last list items with matching key in the pair
      if(remove_dir_ == SA_POP::FRONT)
      {
        for(SA_POP::ListMultiMap<L,T>::list_iterator l_iter = list_.begin(); l_iter != list_.end();)
        {
          SA_POP::ListMultiMap<L,T>::list_iterator prev_iter = l_iter;
          l_iter++;
          if((*prev_iter).first == _Where->first && (*prev_iter).second == _Where->second )
          {
            this->list_.erase(prev_iter);
            break;
          }
        }
      }
      else if(remove_dir_ == SA_POP::BACK)
      {
        SA_POP::ListMultiMap<L,T>::list_iterator last_iter = list_.end();
        for(SA_POP::ListMultiMap<L,T>::list_iterator l_iter = list_.begin(); l_iter != list_.end();l_iter++)
        {
          if((*l_iter).first == _Where->first && (*l_iter).second == _Where->second)
          {
            last_iter = l_iter;
          }
        }
        if(last_iter != list_.end())
        {
          this->list_.erase(last_iter);
        }
        else
        {
          throw "ListMultiMap<L,T>::erase(typename ListMultiMap<L,T>::iterator _Where) : Could not find value in the list";
        }
      }
      else 
      {
        throw "ListMultiMap<L,T>::erase(typename ListMultiMap<L,T>::iterator _Where): Unknown direction to remove.";
      }
  }
  this->multimap_.erase (_Where);
};

template<typename L, typename T>
void SA_POP::ListMultiMap<L,T>::erase(typename SA_POP::ListMultiMap<L,T>::iterator _First, typename SA_POP::ListMultiMap<L,T>::iterator _Last)
{
  
  //iterate through the range [_First, _Last) and erasing each iterator in turn
  if(_First != this->end())
  {
    for(SA_POP::ListMultiMap<L,T>::iterator m_iter = _First; m_iter != _Last;)
    {
      SA_POP::ListMultiMap<L,T>::iterator prev_iter = m_iter;
      m_iter++;
      this->erase(prev_iter);
    }
  }

};

// Begin
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::begin()
{
  return this->multimap_.begin ();
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::const_iterator SA_POP::ListMultiMap<L,T>::begin() const
{
  return this->multimap_.begin ();
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::reverse_iterator SA_POP::ListMultiMap<L,T>::rbegin()
{
  return this->multimap_.rbegin ();
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::const_reverse_iterator SA_POP::ListMultiMap<L,T>::rbegin() const
{
  return this->multimap_.rbegin ();
};

// End
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::end()
{
  return this->multimap_.end ();
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::const_iterator SA_POP::ListMultiMap<L,T>::end() const
{
  return this->multimap_.end ();
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::reverse_iterator SA_POP::ListMultiMap<L,T>::rend()
{
  return this->multimap_.rend ();
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::const_reverse_iterator SA_POP::ListMultiMap<L,T>::rend() const
{
  return this->multimap_.rend ();
};

// Lower Bound
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::lower_bound(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval)
{
  return this->multimap_.lower_bound (_Keyval);
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::const_iterator SA_POP::ListMultiMap<L,T>::lower_bound(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) const
{
  return this->multimap_.lower_bound (_Keyval);
};

// Upper Bound
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::iterator SA_POP::ListMultiMap<L,T>::upper_bound(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval)
{
  return this->multimap_.upper_bound (_Keyval);
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::const_iterator SA_POP::ListMultiMap<L,T>::upper_bound(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) const
{
  return this->multimap_.upper_bound (_Keyval);

};

// Size
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::size_type SA_POP::ListMultiMap<L,T>::size() const
{
  return this->multimap_.size ();
};

// Empty?
template<typename L, typename T>
bool SA_POP::ListMultiMap<L,T>::empty() const
{
  return this->multimap_.empty ();
};

///Count
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::size_type SA_POP::ListMultiMap<L,T>::count(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) const
{
  return this->multimap_.count (_Keyval);
};

///Clear
template<typename L, typename T>
void SA_POP::ListMultiMap<L,T>::clear()
{
  this->multimap_.clear ();
  this->list_.clear ();
};

///Equal Range
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::_Pairii SA_POP::ListMultiMap<L,T>::equal_range(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval)
{
  return this->multimap_.equal_range (_Keyval);
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::_Paircc SA_POP::ListMultiMap<L,T>::equal_range(const typename SA_POP::ListMultiMap<L,T>::key_type& _Keyval) const
{
  return this->multimap_.equal_range (_Keyval);
};

//List Functions

///Erase  
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::list_iterator SA_POP::ListMultiMap<L,T>::erase(typename SA_POP::ListMultiMap<L,T>::list_iterator _Where)
{
  // Erase from the multimap
  if(_Where != list_.end())
  {

    for (std::multimap<L, T>::iterator m_iter = multimap_.lower_bound ((*_Where).first);m_iter != multimap_.upper_bound ((*_Where).first);)
    {
      std::multimap<L, T>::iterator prev_iter = m_iter;
      m_iter++;
      
      if(prev_iter->second == (*_Where).second)
      {
        this->multimap_.erase(prev_iter);
        break;
      }
    }

  }
  
  return this->list_.erase(_Where);
 
};

///Front
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::list_reference SA_POP::ListMultiMap<L,T>::front()
{
  return this->list_.front ();
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::const_list_reference SA_POP::ListMultiMap<L,T>::front() const
{
  return this->list_.front ();
};

///Back
template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::list_reference SA_POP::ListMultiMap<L,T>::back()
{
  return this->list_.back ();
};

template<typename L, typename T>
typename SA_POP::ListMultiMap<L,T>::const_list_reference SA_POP::ListMultiMap<L,T>::back() const
{
  return this->list_.back ();
};

///Push Front
template<typename L, typename T>
void SA_POP::ListMultiMap<L,T>::push_front(const typename SA_POP::ListMultiMap<L,T>::list_Ty& _Val)
{
  
  this->multimap_.insert(_Val);
  this->list_.push_front (_Val);
};

///Pop Front
template<typename L, typename T>
void SA_POP::ListMultiMap<L,T>::pop_front()
{

  //This will erase the front item in both list and multimap.
  this->erase(this->list_.begin());

};

///Push Back
template<typename L, typename T>
void SA_POP::ListMultiMap<L,T>::push_back(const typename SA_POP::ListMultiMap<L,T>::list_Ty& _Val)
{
  
  this->multimap_.insert(_Val);

  this->list_.push_back (_Val);
};

///Pop Back
template<typename L, typename T>
void SA_POP::ListMultiMap<L,T>::pop_back()
{
  //This will erase the back item in both list and multimap.
  SA_POP::ListMultiMap<L,T>::list_iterator l_iter= -- this->list_.end();
  this->erase(l_iter);
};


#endif /* SA_POP_UTILS_CPP_ */