| Fast DDS Version 3.3.0 Fast DDS

Loading...
Searching...
No Matches
ResourceLimitedVector.hpp
1// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
19
20#ifndef FASTDDS_UTILS_COLLECTIONS__RESOURCELIMITEDVECTOR_HPP
21#define FASTDDS_UTILS_COLLECTIONS__RESOURCELIMITEDVECTOR_HPP
22
23#include "ResourceLimitedContainerConfig.hpp"
24
25#include <assert.h>
26#include <algorithm>
27#include <type_traits>
28#include <vector>
29
30 namespace eprosima {
31 namespace fastdds {
32
52 template <
53 typename _Ty,
54 typename _KeepOrderEnabler = std::false_type,
55 typename _LimitsConfig = ResourceLimitedContainerConfig,
56 typename _Alloc = std::allocator<_Ty>,
57 typename _Collection = std::vector<_Ty, _Alloc>>
58 class ResourceLimitedVector
59{
60 public:
61
62 using configuration_type = _LimitsConfig;
63 using collection_type = _Collection;
64 using value_type = _Ty;
65 using allocator_type = _Alloc;
66 using pointer = typename collection_type::pointer;
67 using const_pointer = typename collection_type::const_pointer;
68 using reference = typename collection_type::reference;
69 using const_reference = typename collection_type::const_reference;
70 using size_type = typename collection_type::size_type;
71 using difference_type = typename collection_type::difference_type;
72 using iterator = typename collection_type::iterator;
73 using const_iterator = typename collection_type::const_iterator;
74 using reverse_iterator = typename collection_type::reverse_iterator;
75 using const_reverse_iterator = typename collection_type::const_reverse_iterator;
76
89 ResourceLimitedVector(
90 configuration_type cfg = configuration_type(),
91 const allocator_type& alloc = allocator_type())
92 : configuration_(cfg)
93 , collection_(alloc)
94 {
95 collection_.reserve(cfg.initial);
96 }
97
98 ResourceLimitedVector(
99 const ResourceLimitedVector& other)
100 : configuration_(other.configuration_)
101 , collection_(other.collection_.get_allocator())
102 {
103 collection_.reserve(other.collection_.capacity());
104 collection_.assign(other.collection_.begin(), other.collection_.end());
105 }
106
107 virtual ~ResourceLimitedVector () = default;
108
109 ResourceLimitedVector& operator = (
110 const ResourceLimitedVector& other)
111 {
112 clear();
113 for (const_reference item : other)
114 {
115 push_back(item);
116 }
117
118 assert(size() == other.size());
119 return *this;
120 }
121
130 iterator insert(
131 const_iterator pos,
132 const value_type& value)
133 {
134 auto dist = std::distance(collection_.cbegin(), pos);
135 if (!ensure_capacity())
136 {
137 return end();
138 }
139
140 return collection_.insert(collection_.cbegin() + dist, value);
141 }
142
151 iterator insert(
152 const_iterator pos,
153 value_type&& value)
154 {
155 auto dist = std::distance(collection_.cbegin(), pos);
156 if (!ensure_capacity())
157 {
158 return end();
159 }
160
161 return collection_.insert(collection_.cbegin() + dist, std::move(value));
162 }
163
174 pointer push_back(
175 const value_type& val)
176 {
177 return emplace_back(val);
178 }
179
190 pointer push_back(
191 value_type&& val)
192 {
193 if (!ensure_capacity())
194 {
195 // Indicate error by returning null pointer
196 return nullptr;
197 }
198
199 // Move the element at the end of the collection
200 collection_.push_back(std::move(val));
201
202 // Return pointer to newly created element
203 return &collection_.back();
204 }
205
216 template<typename ... Args>
217 pointer emplace_back(
218 Args&& ... args)
219 {
220 if (!ensure_capacity())
221 {
222 // Indicate error by returning null pointer
223 return nullptr;
224 }
225
226 // Construct new element at the end of the collection
227 collection_.emplace_back(args ...);
228
229 // Return pointer to newly created element
230 return &collection_.back();
231 }
232
243 bool remove(
244 const value_type& val)
245 {
246 iterator it = std::find(collection_.begin(), collection_.end(), val);
247 if (it != collection_.end())
248 {
249 do_remove(it);
250 return true;
251 }
252 return false;
253 }
254
270 template<class UnaryPredicate>
271 bool remove_if(
272 UnaryPredicate pred)
273 {
274 iterator it = std::find_if(collection_.begin(), collection_.end(), pred);
275 if (it != collection_.end())
276 {
277 do_remove(it);
278 return true;
279 }
280 return false;
281 }
282
292 bool contains(
293 const value_type& val)
294 {
295 iterator it = std::find(collection_.begin(), collection_.end(), val);
296 if (it != collection_.end())
297 {
298 return true;
299 }
300 return false;
301 }
302
317 template <class InputIterator>
318 void assign(
319 InputIterator first,
320 InputIterator last)
321 {
322 size_type n = static_cast<size_type>(std::distance(first, last));
323 n = (std::min)(n, configuration_.maximum);
324 InputIterator value = first;
325 std::advance(value, n);
326 collection_.assign(first, value);
327 }
328
339 void assign(
340 size_type n,
341 const value_type& val)
342 {
343 n = (std::min)(n, configuration_.maximum);
344 collection_.assign(n, val);
345 }
346
358 void assign(
359 std::initializer_list<value_type> il)
360 {
361 size_type n = (std::min)(il.size(), configuration_.maximum);
362 collection_.assign(il.begin(), il.begin() + n);
363 }
364
370 reference at(
371 size_type pos)
372 {
373 return collection_.at(pos);
374 }
375
376 const_reference at(
377 size_type pos) const
378 {
379 return collection_.at(pos);
380 }
381
382 reference operator [](
383 size_type pos)
384 {
385 return collection_[pos];
386 }
387
388 const_reference operator [](
389 size_type pos) const
390 {
391 return collection_[pos];
392 }
393
394 reference front()
395 {
396 return collection_.front();
397 }
398
399 const_reference front() const
400 {
401 return collection_.front();
402 }
403
404 reference back()
405 {
406 return collection_.back();
407 }
408
409 const_reference back() const
410 {
411 return collection_.back();
412 }
413
414 iterator begin() noexcept
415 {
416 return collection_.begin();
417 }
418
419 const_iterator begin() const noexcept
420 {
421 return collection_.begin();
422 }
423
424 const_iterator cbegin() const noexcept
425 {
426 return collection_.cbegin();
427 }
428
429 iterator end() noexcept
430 {
431 return collection_.end();
432 }
433
434 const_iterator end() const noexcept
435 {
436 return collection_.end();
437 }
438
439 const_iterator cend() const noexcept
440 {
441 return collection_.cend();
442 }
443
444 reverse_iterator rbegin() noexcept
445 {
446 return collection_.rbegin();
447 }
448
449 const_reverse_iterator rbegin() const noexcept
450 {
451 return collection_.rbegin();
452 }
453
454 const_reverse_iterator crbegin() const noexcept
455 {
456 return collection_.crbegin();
457 }
458
459 reverse_iterator rend() noexcept
460 {
461 return collection_.rend();
462 }
463
464 const_reverse_iterator rend() const noexcept
465 {
466 return collection_.rend();
467 }
468
469 const_reverse_iterator crend() const noexcept
470 {
471 return collection_.crend();
472 }
473
474 bool empty() const noexcept
475 {
476 return collection_.empty();
477 }
478
479 size_type size() const noexcept
480 {
481 return collection_.size();
482 }
483
484 size_type capacity() const noexcept
485 {
486 return collection_.capacity();
487 }
488
489 size_type max_size() const noexcept
490 {
491 return (std::min)(configuration_.maximum, collection_.max_size());
492 }
493
494 void clear()
495 {
496 collection_.clear();
497 }
498
499 iterator erase(
500 const_iterator pos)
501 {
502 return collection_.erase(pos);
503 }
504
505 iterator erase(
506 const_iterator first,
507 const_iterator last)
508 {
509 return collection_.erase(first, last);
510 }
511
512 void pop_back()
513 {
514 collection_.pop_back();
515 }
516
517 value_type* data()
518 {
519 return collection_.data();
520 }
521
522 const value_type* data() const
523 {
524 return collection_.data();
525 }
526
528
536 operator const collection_type& () const noexcept
537 {
538 return collection_;
539 }
540
541 protected:
542
543 configuration_type configuration_;
544 collection_type collection_;
545
553 bool ensure_capacity()
554 {
555 size_type size = collection_.size();
556 size_type cap = collection_.capacity();
557 if (size == cap)
558 {
559 // collection is full, check resource limit
560 if (cap < configuration_.maximum)
561 {
562 // increase collection capacity
563 assert(configuration_.increment > 0);
564 cap += configuration_.increment;
565 cap = (std::min)(cap, configuration_.maximum);
566 collection_.reserve(cap);
567 }
568 else
569 {
570 return false;
571 }
572 }
573
574 return true;
575 }
576
586 template <typename Enabler = _KeepOrderEnabler>
587 typename std::enable_if<!Enabler::value, void>::type do_remove(
588 iterator it)
589 {
590 // Copy last element into the element being removed
591 if (it != --collection_.end())
592 {
593 *it = std::move(collection_.back());
594 }
595
596 // Then drop last element
597 collection_.pop_back();
598 }
599
610 template <typename Enabler = _KeepOrderEnabler>
611 typename std::enable_if<Enabler::value, void>::type do_remove(
612 iterator it)
613 {
614 collection_.erase(it);
615 }
616
617};
618
619} // namespace fastdds
620} // namespace eprosima
621
622#endif // FASTDDS_UTILS_COLLECTIONS__RESOURCELIMITEDVECTOR_HPP
eprosima::fastdds::ResourceLimitedVector::pop_back
void pop_back()
Definition ResourceLimitedVector.hpp:512
eprosima::fastdds::ResourceLimitedVector::size
size_type size() const noexcept
Definition ResourceLimitedVector.hpp:479
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::configuration_
configuration_type configuration_
Definition ResourceLimitedVector.hpp:543
eprosima::fastdds::ResourceLimitedVector::front
const_reference front() const
Definition ResourceLimitedVector.hpp:399
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::iterator
typename collection_type::iterator iterator
Definition ResourceLimitedVector.hpp:72
eprosima::fastdds::ResourceLimitedVector::push_back
pointer push_back(value_type &&val)
Add element at the end.
Definition ResourceLimitedVector.hpp:190
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::difference_type
typename collection_type::difference_type difference_type
Definition ResourceLimitedVector.hpp:71
eprosima::fastdds::ResourceLimitedVector::assign
void assign(std::initializer_list< value_type > il)
Assign vector content.
Definition ResourceLimitedVector.hpp:358
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::reverse_iterator
typename collection_type::reverse_iterator reverse_iterator
Definition ResourceLimitedVector.hpp:74
eprosima::fastdds::ResourceLimitedVector::do_remove
std::enable_if< Enabler::value, void >::type do_remove(iterator it)
Remove element.
Definition ResourceLimitedVector.hpp:611
eprosima::fastdds::ResourceLimitedVector::assign
void assign(size_type n, const value_type &val)
Assign vector content.
Definition ResourceLimitedVector.hpp:339
eprosima::fastdds::ResourceLimitedVector::begin
const_iterator begin() const noexcept
Definition ResourceLimitedVector.hpp:419
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::const_pointer
typename collection_type::const_pointer const_pointer
Definition ResourceLimitedVector.hpp:67
eprosima::fastdds::ResourceLimitedVector::do_remove
std::enable_if<!Enabler::value, void >::type do_remove(iterator it)
Remove element.
Definition ResourceLimitedVector.hpp:587
eprosima::fastdds::ResourceLimitedVector::erase
iterator erase(const_iterator pos)
Definition ResourceLimitedVector.hpp:499
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::configuration_type
ResourceLimitedContainerConfig configuration_type
Definition ResourceLimitedVector.hpp:62
eprosima::fastdds::ResourceLimitedVector::insert
iterator insert(const_iterator pos, value_type &&value)
Insert value before pos.
Definition ResourceLimitedVector.hpp:151
eprosima::fastdds::ResourceLimitedVector::empty
bool empty() const noexcept
Definition ResourceLimitedVector.hpp:474
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::const_reverse_iterator
typename collection_type::const_reverse_iterator const_reverse_iterator
Definition ResourceLimitedVector.hpp:75
eprosima::fastdds::ResourceLimitedVector::ResourceLimitedVector
ResourceLimitedVector(const ResourceLimitedVector &other)
Definition ResourceLimitedVector.hpp:98
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::pointer
typename collection_type::pointer pointer
Definition ResourceLimitedVector.hpp:66
eprosima::fastdds::ResourceLimitedVector::at
const_reference at(size_type pos) const
Definition ResourceLimitedVector.hpp:376
eprosima::fastdds::ResourceLimitedVector::at
reference at(size_type pos)
Wrappers to other basic vector methods.
Definition ResourceLimitedVector.hpp:370
eprosima::fastdds::ResourceLimitedVector::end
const_iterator end() const noexcept
Definition ResourceLimitedVector.hpp:434
eprosima::fastdds::ResourceLimitedVector::back
const_reference back() const
Definition ResourceLimitedVector.hpp:409
eprosima::fastdds::ResourceLimitedVector::remove_if
bool remove_if(UnaryPredicate pred)
Remove element.
Definition ResourceLimitedVector.hpp:271
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::const_iterator
typename collection_type::const_iterator const_iterator
Definition ResourceLimitedVector.hpp:73
eprosima::fastdds::ResourceLimitedVector::front
reference front()
Definition ResourceLimitedVector.hpp:394
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::collection_type
std::vector< fastdds::rtps::octet, std::allocator< fastdds::rtps::octet > > collection_type
Definition ResourceLimitedVector.hpp:63
eprosima::fastdds::ResourceLimitedVector::ensure_capacity
bool ensure_capacity()
Make room for one item.
Definition ResourceLimitedVector.hpp:553
eprosima::fastdds::ResourceLimitedVector::begin
iterator begin() noexcept
Definition ResourceLimitedVector.hpp:414
eprosima::fastdds::ResourceLimitedVector::cend
const_iterator cend() const noexcept
Definition ResourceLimitedVector.hpp:439
eprosima::fastdds::ResourceLimitedVector::data
const value_type * data() const
Definition ResourceLimitedVector.hpp:522
eprosima::fastdds::ResourceLimitedVector::erase
iterator erase(const_iterator first, const_iterator last)
Definition ResourceLimitedVector.hpp:505
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::value_type
fastdds::rtps::octet value_type
Definition ResourceLimitedVector.hpp:64
eprosima::fastdds::ResourceLimitedVector::operator[]
reference operator[](size_type pos)
Definition ResourceLimitedVector.hpp:382
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::allocator_type
std::allocator< fastdds::rtps::octet > allocator_type
Definition ResourceLimitedVector.hpp:65
eprosima::fastdds::ResourceLimitedVector::push_back
pointer push_back(const value_type &val)
Add element at the end.
Definition ResourceLimitedVector.hpp:174
eprosima::fastdds::ResourceLimitedVector::crbegin
const_reverse_iterator crbegin() const noexcept
Definition ResourceLimitedVector.hpp:454
eprosima::fastdds::ResourceLimitedVector::cbegin
const_iterator cbegin() const noexcept
Definition ResourceLimitedVector.hpp:424
eprosima::fastdds::ResourceLimitedVector::ResourceLimitedVector
ResourceLimitedVector(configuration_type cfg=configuration_type(), const allocator_type &alloc=allocator_type())
Construct a ResourceLimitedVector.
Definition ResourceLimitedVector.hpp:89
eprosima::fastdds::ResourceLimitedVector::max_size
size_type max_size() const noexcept
Definition ResourceLimitedVector.hpp:489
eprosima::fastdds::ResourceLimitedVector::~ResourceLimitedVector
virtual ~ResourceLimitedVector()=default
eprosima::fastdds::ResourceLimitedVector::rend
const_reverse_iterator rend() const noexcept
Definition ResourceLimitedVector.hpp:464
eprosima::fastdds::ResourceLimitedVector::rbegin
reverse_iterator rbegin() noexcept
Definition ResourceLimitedVector.hpp:444
eprosima::fastdds::ResourceLimitedVector::clear
void clear()
Definition ResourceLimitedVector.hpp:494
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::size_type
typename collection_type::size_type size_type
Definition ResourceLimitedVector.hpp:70
eprosima::fastdds::ResourceLimitedVector::insert
iterator insert(const_iterator pos, const value_type &value)
Insert value before pos.
Definition ResourceLimitedVector.hpp:130
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::reference
typename collection_type::reference reference
Definition ResourceLimitedVector.hpp:68
eprosima::fastdds::ResourceLimitedVector::crend
const_reverse_iterator crend() const noexcept
Definition ResourceLimitedVector.hpp:469
eprosima::fastdds::ResourceLimitedVector::rend
reverse_iterator rend() noexcept
Definition ResourceLimitedVector.hpp:459
eprosima::fastdds::ResourceLimitedVector::assign
void assign(InputIterator first, InputIterator last)
Assign vector content.
Definition ResourceLimitedVector.hpp:318
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::collection_
collection_type collection_
Definition ResourceLimitedVector.hpp:544
eprosima::fastdds::ResourceLimitedVector::capacity
size_type capacity() const noexcept
Definition ResourceLimitedVector.hpp:484
eprosima::fastdds::ResourceLimitedVector::emplace_back
pointer emplace_back(Args &&... args)
Construct and insert element at the end.
Definition ResourceLimitedVector.hpp:217
eprosima::fastdds::ResourceLimitedVector::data
value_type * data()
Definition ResourceLimitedVector.hpp:517
eprosima::fastdds::ResourceLimitedVector::contains
bool contains(const value_type &val)
Contains element.
Definition ResourceLimitedVector.hpp:292
eprosima::fastdds::ResourceLimitedVector::remove
bool remove(const value_type &val)
Remove element.
Definition ResourceLimitedVector.hpp:243
eprosima::fastdds::ResourceLimitedVector< fastdds::rtps::octet >::const_reference
typename collection_type::const_reference const_reference
Definition ResourceLimitedVector.hpp:69
eprosima::fastdds::ResourceLimitedVector::back
reference back()
Definition ResourceLimitedVector.hpp:404
eprosima::fastdds::ResourceLimitedVector::end
iterator end() noexcept
Definition ResourceLimitedVector.hpp:429
eprosima::fastdds::ResourceLimitedVector::rbegin
const_reverse_iterator rbegin() const noexcept
Definition ResourceLimitedVector.hpp:449
eprosima::fastdds::ResourceLimitedVector::operator=
ResourceLimitedVector & operator=(const ResourceLimitedVector &other)
Definition ResourceLimitedVector.hpp:109
eprosima::fastdds
eprosima
eProsima namespace.
eprosima::fastdds::ResourceLimitedContainerConfig
Specifies the configuration of a resource limited collection.
Definition ResourceLimitedContainerConfig.hpp:36