https://mooseframework.inl.gov
StaticallyAllocatedSet.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 #include "MooseError.h"
13 
14 #include <array>
15 
16 namespace MooseUtils
17 {
18 
28 template <typename T, std::size_t N>
30 {
31 public:
32  typedef typename std::array<T, N>::iterator iterator;
33  typedef typename std::array<T, N>::const_iterator const_iterator;
34 
39 
43  std::size_t size() const { return _end_pos; }
44 
48  bool empty() const { return !_end_pos; }
49 
53  void insert(const T & value)
54  {
55  if (contains(value))
56  return;
57 
58  if (_end_pos == N)
59  mooseError("Out of space in StaticallyAllocatedSet (size = ", N, ")");
60 
61  _data[_end_pos] = value;
62  ++_end_pos;
63  }
64 
70  void clear() { _end_pos = 0; }
71 
75  iterator begin() { return _data.begin(); }
79  const_iterator begin() const { return _data.begin(); }
80 
84  iterator end() { return _data.begin() + _end_pos; }
88  const_iterator end() const { return _data.begin() + _end_pos; }
89 
93  bool contains(const T & value) const
94  {
95  for (std::size_t i = 0; i < _end_pos; i++)
96  if (_data[i] == value)
97  return true;
98 
99  return false;
100  }
101 
106  {
107  _data.swap(other._data);
108  std::swap(_end_pos, other._end_pos);
109  }
110 
114  std::size_t dataEndPos() const { return _end_pos; }
115 
116 private:
118  std::array<T, N> _data;
119 
121  std::size_t _end_pos;
122 };
123 
124 } // namespace MooseUtils
iterator end()
Iterator for the last entry.
std::array< T, N >::const_iterator const_iterator
void swap(StaticallyAllocatedSet< T, N > &other)
Swap the contents of this set with another.
Optimized set with static allocation.
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:302
void swap(std::vector< T > &data, const std::size_t idx0, const std::size_t idx1, const libMesh::Parallel::Communicator &comm)
Swap function for serial or distributed vector of data.
Definition: Shuffle.h:494
iterator begin()
Iterator for the first entry.
const_iterator end() const
Const iterator for the last entry.
std::array< T, N > _data
The data.
std::size_t _end_pos
Save the ending as positions internally.
Real value(unsigned n, unsigned alpha, unsigned beta, Real x)
std::size_t size() const
Number of entries.
bool contains(const T &value) const
Whether or not the set contains the given item.
bool empty() const
Whether or not the set is empty.
std::array< T, N >::iterator iterator
std::size_t dataEndPos() const
Expert interface: the current ending position.
void insert(const T &value)
Add a new entry to the set.
const_iterator begin() const
Const iterator for the first entry.