SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CMappedMultiLevelList.hpp
1 /* This file is part of sUtil, a random collection of utilities.
2 
3 sUtil is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 
8 Alternatively, you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 sUtil is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License and a copy of the GNU General Public License along with
20 sUtil. If not, see <http://www.gnu.org/licenses/>.
21  */
22 /* \file CMappedMultiLevelList.hpp
23  *
24  * Created on: Jun 28, 2010
25  *
26  * Copyright (C) 2010, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef CMAPPEDMULTILEVELLIST_HPP_
30 #define CMAPPEDMULTILEVELLIST_HPP_
31 
32 #include <sutil/CMappedList.hpp>
33 
34 #include <vector>
35 
36 #ifdef DEBUG
37 #include <iostream>
38 #endif
39 
40 namespace sutil
41 {
47  template <typename Idx, typename T>
48  class CMappedMultiLevelList : public CMappedList<Idx,T>
49  {
50  public:
53 
56  virtual ~CMappedMultiLevelList();
57 
60  virtual T* create(const Idx& arg_idx, const T &arg_node2add,
61  const std::size_t arg_priority);
62 
65  virtual T* create(const Idx& arg_idx, const std::size_t arg_priority);
66 
69  virtual T* insert(const Idx& arg_idx, T *arg_node2add,
70  const std::size_t arg_priority);
71 
72 
76  {
77  deepCopy(&arg_rhs);
78  return *this;
79  }
80 
83  virtual bool erase(T* arg_t);
84 
87  virtual bool erase(const Idx& arg_idx);
88 
90  virtual bool clear();
91 
93  std::vector<T*>* getSinglePriorityLevel(std::size_t arg_pri);
94 
95  std::size_t getNumPriorityLevels() const
96  { return pri_levels_; }
97 
101  int getPriorityLevel(T* arg_t);
102 
107  int getPriorityLevel(const Idx & arg_idx);
108 
115  std::vector<std::vector<T*> > mlvec_;
116 
117  protected:
122  std::map<const T*, std::size_t> map_nodeptr2pri_;
123 
125  std::size_t pri_levels_;
126 
127  protected:
141  virtual bool deepCopy(const CMappedMultiLevelList<Idx,T>* arg_br);
142 
143  }; //End of template class
144 
145  /***************************************************************
146  *******************************Function Definitions*************
147  ****************************************************************
148  */
149 
151  template <typename Idx, typename T>
153  CMappedList<Idx,T>::CMappedList()
154  { mlvec_.clear(); pri_levels_ = 0; }
155 
160  template <typename Idx, typename T>
162  { mlvec_.clear(); map_nodeptr2pri_.clear(); pri_levels_=0; }
163 
164 
166  template <typename Idx, typename T>
168  const Idx& arg_idx, const T & arg_node2add,
169  const std::size_t arg_priority)
170  {
171  //Add the node.
172  T* tLnk = CMappedList<Idx,T>::create(arg_idx,arg_node2add);
173 
174  if(NULL!=tLnk)
175  {
176  for(std::size_t i=mlvec_.size(); i <= arg_priority; i++)
177  {
178  std::vector<T*> tmp;
179  mlvec_.push_back(tmp);
180  pri_levels_++;//Every push back increases pri levels.
181  }
182  mlvec_[arg_priority].push_back(tLnk);
183  map_nodeptr2pri_.insert(std::pair<T*,std::size_t>(tLnk,arg_priority));
184  }
185  return tLnk;
186  }
187 
188  template <typename Idx, typename T>
190  const Idx& arg_idx, const std::size_t arg_priority)
191  {
192  //Add the node.
193  T* tLnk = CMappedList<Idx,T>::create(arg_idx);
194 
195  if(NULL!=tLnk)
196  {
197  for(std::size_t i=mlvec_.size(); i <= arg_priority; i++)
198  {
199  std::vector<T*> tmp;
200  mlvec_.push_back(tmp);
201  pri_levels_++;//Every push back increases pri levels.
202  }
203  mlvec_[arg_priority].push_back(tLnk);
204  map_nodeptr2pri_.insert(std::pair<T*,std::size_t>(tLnk,arg_priority));
205  }
206 
207  return tLnk;
208  }
209 
210 
212  template <typename Idx, typename T>
214  const Idx& arg_idx, T *arg_node2add,
215  const std::size_t arg_priority)
216  {
217  //Add the node.
218  T* tLnk = CMappedList<Idx,T>::create(arg_idx,arg_node2add);
219 
220  if(NULL!=tLnk)
221  {
222  for(std::size_t i=mlvec_.size(); i <= arg_priority; i++)
223  {
224  std::vector<T*> tmp;
225  mlvec_.push_back(tmp);
226  pri_levels_++;//Every push back increases pri levels.
227  }
228  mlvec_[arg_priority].push_back(tLnk);
229  map_nodeptr2pri_.insert(std::pair<T*,std::size_t>(tLnk,arg_priority));
230  }
231  return tLnk;
232  }
233 
234 
235  template <typename Idx, typename T>
238  {//Deep copy.
239  this->~CMappedMultiLevelList(); //Delete everything in the mapped list
240 
242  if(0 == arg_br->size())
243  {
247  mlvec_.clear(); map_nodeptr2pri_.clear();
248  }
249  else
250  {
251  //Copy over each element.
252  typename CMappedList<Idx,T>::const_iterator it,ite;
253  for(it = arg_br->CMappedList<Idx,T>::begin(), ite = arg_br->CMappedList<Idx,T>::end();
254  it!=ite; ++it)
255  {
256  T* tmp = CMappedList<Idx,T>::create(!it,*it);
257  if(NULL == tmp)
258  {
259 #ifdef DEBUG
260  std::cerr<<"\nCMultiLevelPileMap<Idx,T>::deepCopy() Error :Deep copy failed. Resetting multi-level mapped list.";
261 #endif
262  clear(); return false;
263  }
264 
265  //Now recreate the priority map
266  const T* tmpptr = &(*it); //Deref iterator to get ref. Get ref's addr to access priority in map.
267  if(arg_br->map_nodeptr2pri_.find(tmpptr) ==
268  arg_br->map_nodeptr2pri_.end())
269  {
270 #ifdef DEBUG
271  std::cerr<<"\nCMultiLevelPileMap<Idx,T>::deepCopy() Error :Did not find a node in the priority map.";
272 #endif
273  clear(); return false;
274  }
275 
276  std::size_t pri = arg_br->map_nodeptr2pri_.at(tmpptr);
277  map_nodeptr2pri_.insert(std::pair<T*,std::size_t>(tmp,pri));
278 
279  //Now add the entry to the vector
280  for(std::size_t i=mlvec_.size(); i <= pri; i++)
281  {
282  std::vector<T*> tmp;
283  mlvec_.push_back(tmp);
284  pri_levels_++;//Every push back increases pri levels.
285  }
286  mlvec_[pri].push_back(tmp);
287  }
288  }
289  return true;
290  }
291 
292  template <typename Idx, typename T>
294  {
295  bool flag;
296  if((NULL==CMappedList<Idx,T>::front_) || (NULL==arg_t))
297  { return false; }
298  else
299  {
300  if(map_nodeptr2pri_.find(arg_t) ==
301  map_nodeptr2pri_.end())
302  { return false; }
303 
304  std::size_t pri = map_nodeptr2pri_[arg_t];
305 
306  //Remove it from the priority level vectors
307  typename std::vector<T*>::iterator it,ite;
308  for(it = mlvec_[pri].begin(),ite = mlvec_[pri].end();
309  it!=ite; ++it)
310  {
311  if(*it == arg_t)
312  { mlvec_[pri].erase(it); break; }
313  }
314 
315  if(pri == pri_levels_ -1)//Deleted from lowest level.
316  {
317  if(mlvec_[pri].size()==0)
318  {//If last one is now empty, remove it (and others if possible)
319  typename std::vector<std::vector<T*> >::reverse_iterator itr,itre;
320  for(itr = mlvec_.rbegin(),itre = mlvec_.rend();
321  itr!=itre; ++itr)
322  {
323  if((*itr).size() == 0)
324  //Use rev_iter.base() func to convert it to normal iter
325  //Can only erase a normal iter
326  //reverse iterator points to one element closer to begin
327  //than the base() function
328  { mlvec_.erase((itr+1).base()); }
329  else
330  { break; }
331  }
332  }
333  }
334 
335  //Remove it from the nodeptr2pri map
336  map_nodeptr2pri_.erase(arg_t);
337 
338  //Remove it from the mapped list (deallocate memory)
339  flag = CMappedList<Idx,T>::erase(arg_t);
340  if(false == flag)
341  { return false; }
342  }
343  pri_levels_ = mlvec_.size();
344  return true;
345  }
346 
347  template <typename Idx, typename T>
348  bool CMappedMultiLevelList<Idx,T>::erase(const Idx& arg_idx)
349  {
350  bool flag;
352  { return false; }
353  else
354  {
355  T* t_ptr = CMappedList<Idx,T>::at(arg_idx);
356  if(NULL == t_ptr)
357  { return false; }
358 
359  if(map_nodeptr2pri_.find(t_ptr) ==
360  map_nodeptr2pri_.end())
361  { return false; }
362 
363  std::size_t pri = map_nodeptr2pri_[t_ptr];
364 
365  //Remove it from the priority level vectors
366  typename std::vector<T*>::iterator it,ite;
367  for(it = mlvec_[pri].begin(),ite = mlvec_[pri].end();
368  it!=ite; ++it)
369  {
370  if(*it == t_ptr)
371  { mlvec_[pri].erase(it); break; }
372  }
373 
374  if(pri == pri_levels_ -1)//Deleted from lowest level.
375  {
376  if(mlvec_[pri].size()==0)
377  {//If last one is now empty, remove it (and others if possible)
378  typename std::vector<std::vector<T*> >::reverse_iterator itr,itre;
379  for(itr = mlvec_.rbegin(),itre = mlvec_.rend();
380  itr!=itre; ++itr)
381  {
382  if((*itr).size() == 0)
383  //Use rev_iter.base() func to convert it to normal iter
384  //Can only erase a normal iter
385  //reverse iterator points to one element closer to begin
386  //than the base() function
387  { mlvec_.erase((itr+1).base()); }
388  else
389  { break; }
390  }
391  }
392  }
393 
394  //Remove it from the nodeptr2pri map
395  map_nodeptr2pri_.erase(t_ptr);
396 
397  //Remove it from the mapped list (deallocate memory)
398  flag = CMappedList<Idx,T>::erase(arg_idx);
399  if(false == flag)
400  { return false; }
401  }
402  pri_levels_ = mlvec_.size();
403  return true;
404  }
405  template <typename Idx, typename T>
407  {
409  mlvec_.clear();
410  map_nodeptr2pri_.clear();
411  pri_levels_=0;
412  return true;
413  }
414 
415  template <typename Idx, typename T>
416  std::vector<T*>* CMappedMultiLevelList<Idx,T>::
417  getSinglePriorityLevel(std::size_t arg_pri)
418  {
419  if(arg_pri > mlvec_.size())
420  { return NULL; }
421  else
422  {
423  std::vector<T*>* ret = &(mlvec_.at(arg_pri));
424  return ret;
425  }
426  }
427 
428  template <typename Idx, typename T>
431  {
432  int ret;
433  if(map_nodeptr2pri_.find(arg_t) ==
434  map_nodeptr2pri_.end())
435  { ret = -1; }
436  else
437  { ret = static_cast<int>(map_nodeptr2pri_[arg_t]); }
438 
439  return ret;
440  }
441 
442  template <typename Idx, typename T>
444  getPriorityLevel(const Idx & arg_idx)
445  {
446  T* t_ptr = CMappedList<Idx,T>::at(arg_idx);
447  if(NULL == t_ptr)
448  { return -1; }
449 
450  int ret;
451  if(map_nodeptr2pri_.find(t_ptr) ==
452  map_nodeptr2pri_.end())
453  { ret = -1; }
454  else
455  { ret = static_cast<int>(map_nodeptr2pri_[t_ptr]); }
456 
457  return ret;
458  }
459 
460 }//End of namespace sutil
461 
462 #endif /*CMAPPEDMULTILEVELLIST_HPP_*/
int getPriorityLevel(T *arg_t)
Definition: CMappedMultiLevelList.hpp:430
std::vector< std::vector< T * > > mlvec_
Definition: CMappedMultiLevelList.hpp:115
virtual bool clear()
Definition: CMappedList.hpp:1127
CMappedMultiLevelList()
Definition: CMappedMultiLevelList.hpp:152
Definition: CMappedList.hpp:85
virtual T * create(const Idx &arg_idx, const T &arg_node2add, const std::size_t arg_priority)
Definition: CMappedMultiLevelList.hpp:167
virtual CMappedMultiLevelList< Idx, T > & operator=(const CMappedMultiLevelList< Idx, T > &arg_rhs)
Definition: CMappedMultiLevelList.hpp:75
virtual bool clear()
Definition: CMappedMultiLevelList.hpp:406
virtual bool deepCopy(const CMappedMultiLevelList< Idx, T > *arg_br)
Definition: CMappedMultiLevelList.hpp:237
virtual ~CMappedMultiLevelList()
Definition: CMappedMultiLevelList.hpp:161
std::vector< T * > * getSinglePriorityLevel(std::size_t arg_pri)
Definition: CMappedMultiLevelList.hpp:417
virtual T * at(const std::size_t arg_idx)
Definition: CMappedList.hpp:832
virtual T * insert(const Idx &arg_idx, T *arg_node2add, const std::size_t arg_priority)
Definition: CMappedMultiLevelList.hpp:213
virtual bool erase(T *arg_t)
Definition: CMappedList.hpp:985
Definition: CMappedMultiLevelList.hpp:48
std::map< const T *, std::size_t > map_nodeptr2pri_
Definition: CMappedMultiLevelList.hpp:122
virtual bool erase(T *arg_t)
Definition: CMappedMultiLevelList.hpp:293
virtual T * create(const Idx &arg_idx, const bool insert_at_start=true)
Definition: CMappedList.hpp:714
Definition: CMappedList.hpp:381
virtual std::size_t size() const
Definition: CMappedList.hpp:240
std::size_t pri_levels_
Definition: CMappedMultiLevelList.hpp:125