Flow123d  JS_before_hm-2198-g122e1f2e2
revertable_list.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file revertable_list.hh
15  * @brief
16  */
17 
18 #ifndef REVARTABLE_LIST_HH_
19 #define REVARTABLE_LIST_HH_
20 
21 
22 #include <new>
23 #include "system/asserts.hh"
24 
25 
26 /**
27  * @brief Struct is a container that encapsulates variable size arrays.
28  *
29  * Allows to:
30  * 1. Add new items to container (method push_back, items are stored as temporary.
31  * 2. Mark block of temporary items as final (use method make_permanent)
32  * or cancelled temporary item (method revert_temporary)
33  *
34  * This algorith allows to add blocks of data, evaluates external condition
35  * and possibly reverts unfinished block if condition is not met.
36  *
37  * Reserved (maximal) size is set in constructor. This size can be enlarged manually
38  * through method resize or constructor accepts parameter enlarged_by. If this value
39  * is greater than 0 size of container is automatically enlarged during call push_back
40  * if container is full.
41  */
42 template<class Type>
44 public:
45  /// Constructor, create new instance with reserved size
46  RevertableList(std::size_t reserved_size, std::size_t enlarged_by = 0)
47  : temporary_size_(0), permanent_size_(0), enlardeg_by_(enlarged_by)
48  {
49  data_.reserve(reserved_size);
50  }
51 
52  /// Copy constructor
55  {
56  data_.reserve( other.data_.capacity() );
57  }
58 
59  /**
60  * Resize to new reserved size.
61  *
62  * New size must be higher than actual size!
63  */
64  void resize(std::size_t new_size)
65  {
66  ASSERT_GT(new_size, reserved_size());
67  data_.reserve(new_size);
68  }
69 
70  /// Return permanent size of list.
71  inline std::size_t permanent_size() const
72  {
73  return permanent_size_;
74  }
75 
76  /// Return temporary size of list (full size of stored data).
77  inline std::size_t temporary_size() const
78  {
79  return temporary_size_;
80  }
81 
82  /// Return reserved (maximal) size.
83  inline std::size_t reserved_size() const
84  {
85  return data_.capacity();
86  }
87 
88  /**
89  * Add new item of list.
90  *
91  * New item is added to end of list and temporary size value is incremented.
92  * Method is equivalent with std::vector::push_back().
93  * This method needs to create copy of passed Type and it's beter to use emplace_back method.
94  */
95  inline std::size_t push_back(const Type &t)
96  {
97  ASSERT_DBG((enlardeg_by_ > 0) || (temporary_size_ < reserved_size())).error("Data array overflowed!\n");
98  if (temporary_size_ == reserved_size()) { // enlarge reserved size
99  this->resize( this->reserved_size() + enlardeg_by_ );
100  }
101  data_.push_back(t);
102  temporary_size_++;
103  return temporary_size_;
104  }
105 
106  /**
107  * Create new item in list.
108  *
109  * New item is created at the end of list and temporary size value is incremented.
110  * Method is equivalent with std::vector::emplace_back().
111  * Method passes argumets of Type constructor.
112  */
113  template<class... Args>
114  inline std::size_t emplace_back(Args&&... args)
115  {
116  ASSERT_DBG((enlardeg_by_ > 0) || (temporary_size_ < reserved_size())).error("Data array overflowed!\n");
117  if (temporary_size_ == reserved_size()) { // enlarge reserved size
118  this->resize( this->reserved_size() + enlardeg_by_ );
119  }
120  data_.emplace_back( std::forward<Args>(args)... );
121  temporary_size_++;
122  return temporary_size_;
123  }
124 
125  /// Finalize temporary part of data.
126  inline std::size_t make_permanent()
127  {
129  return temporary_size_;
130  }
131 
132  /// Erase temporary part of data.
133  inline std::size_t revert_temporary()
134  {
136  data_.resize(permanent_size_);
137  return temporary_size_;
138  }
139 
140  /// Clear the list.
141  inline void reset()
142  {
143  temporary_size_ = 0;
144  permanent_size_ = 0;
145  data_.resize(0);
146  }
147 
149  {
150  return data_.begin();
151  }
152 
154  {
155  return data_.begin() + permanent_size_;
156  }
157 
158  /// Return item on given position
159  const Type &operator[](std::size_t pos) const {
160  ASSERT_LT_DBG(pos, temporary_size_).error("Position is out of data size!\n");
161  return data_[pos];
162  }
163 
164 private:
165  std::vector<Type> data_; ///< Vector of items.
166  std::size_t temporary_size_; ///< Temporary size (full size of used data).
167  std::size_t permanent_size_; ///< Final size of data (part of finalize data).
168  std::size_t enlardeg_by_; ///< Allow to enlarge list dynamically during call push_back if reserved size is full
169 };
170 
171 #endif /* REVARTABLE_LIST_HH_ */
RevertableList::temporary_size_
std::size_t temporary_size_
Temporary size (full size of used data).
Definition: revertable_list.hh:166
ASSERT_GT
#define ASSERT_GT(a, b)
Definition of comparative assert macro (Greater Than)
Definition: asserts.hh:312
RevertableList::make_permanent
std::size_t make_permanent()
Finalize temporary part of data.
Definition: revertable_list.hh:126
RevertableList::end
std::vector< Type >::iterator end()
Definition: revertable_list.hh:153
RevertableList::emplace_back
std::size_t emplace_back(Args &&... args)
Definition: revertable_list.hh:114
RevertableList::push_back
std::size_t push_back(const Type &t)
Definition: revertable_list.hh:95
asserts.hh
Definitions of ASSERTS.
RevertableList::permanent_size
std::size_t permanent_size() const
Return permanent size of list.
Definition: revertable_list.hh:71
ASSERT_DBG
#define ASSERT_DBG(expr)
Definition: include_fadbad.hh:28
std::vector
Definition: doxy_dummy_defs.hh:7
RevertableList::reset
void reset()
Clear the list.
Definition: revertable_list.hh:141
RevertableList::reserved_size
std::size_t reserved_size() const
Return reserved (maximal) size.
Definition: revertable_list.hh:83
RevertableList::temporary_size
std::size_t temporary_size() const
Return temporary size of list (full size of stored data).
Definition: revertable_list.hh:77
RevertableList::RevertableList
RevertableList(const RevertableList &other)
Copy constructor.
Definition: revertable_list.hh:53
RevertableList
Struct is a container that encapsulates variable size arrays.
Definition: revertable_list.hh:43
RevertableList::resize
void resize(std::size_t new_size)
Definition: revertable_list.hh:64
RevertableList::operator[]
const Type & operator[](std::size_t pos) const
Return item on given position.
Definition: revertable_list.hh:159
RevertableList::begin
std::vector< Type >::iterator begin()
Definition: revertable_list.hh:148
RevertableList::revert_temporary
std::size_t revert_temporary()
Erase temporary part of data.
Definition: revertable_list.hh:133
ASSERT_LT_DBG
#define ASSERT_LT_DBG(a, b)
Definition of comparative assert macro (Less Than) only for debug mode.
Definition: asserts.hh:300
RevertableList::enlardeg_by_
std::size_t enlardeg_by_
Allow to enlarge list dynamically during call push_back if reserved size is full.
Definition: revertable_list.hh:168
RevertableList::permanent_size_
std::size_t permanent_size_
Final size of data (part of finalize data).
Definition: revertable_list.hh:167
RevertableList::RevertableList
RevertableList(std::size_t reserved_size, std::size_t enlarged_by=0)
Constructor, create new instance with reserved size.
Definition: revertable_list.hh:46
RevertableList::data_
std::vector< Type > data_
Vector of items.
Definition: revertable_list.hh:165