LCOV - code coverage report
Current view: top level - include/utils - StaticallyAllocatedSet.h (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 2bf808 Lines: 18 20 90.0 %
Date: 2025-07-17 01:28:37 Functions: 7 7 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : 
      19             : /**
      20             :  * Optimized set with static allocation.
      21             :  *
      22             :  * Useful when you a set that is small and doesn't do any allocation.
      23             :  * Small in this context is the size at which you are not concerned with
      24             :  * the linear search that involves a comparison of T.
      25             :  *
      26             :  * It is templated on the type the set will hold and the maximum size of the set (N)
      27             :  */
      28             : template <typename T, std::size_t N>
      29             : class StaticallyAllocatedSet
      30             : {
      31             : public:
      32             :   typedef typename std::array<T, N>::iterator iterator;
      33             :   typedef typename std::array<T, N>::const_iterator const_iterator;
      34             : 
      35             :   /**
      36             :    * Create a set
      37             :    */
      38           2 :   StaticallyAllocatedSet() : _end_pos(0) {}
      39             : 
      40             :   /**
      41             :    * Number of entries
      42             :    */
      43           3 :   std::size_t size() const { return _end_pos; }
      44             : 
      45             :   /**
      46             :    * Whether or not the set is empty
      47             :    */
      48             :   bool empty() const { return !_end_pos; }
      49             : 
      50             :   /**
      51             :    * Add a new entry to the set
      52             :    */
      53           5 :   void insert(const T & value)
      54             :   {
      55           5 :     if (contains(value))
      56           0 :       return;
      57             : 
      58           5 :     if (_end_pos == N)
      59           0 :       mooseError("Out of space in StaticallyAllocatedSet (size = ", N, ")");
      60             : 
      61           5 :     _data[_end_pos] = value;
      62           5 :     ++_end_pos;
      63             :   }
      64             : 
      65             :   /**
      66             :    * Remove all entries
      67             :    *
      68             :    * Note: this does NOT at all free any entries
      69             :    */
      70           1 :   void clear() { _end_pos = 0; }
      71             : 
      72             :   /**
      73             :    * Iterator for the first entry
      74             :    */
      75             :   iterator begin() { return _data.begin(); }
      76             :   /**
      77             :    * Const iterator for the first entry
      78             :    */
      79             :   const_iterator begin() const { return _data.begin(); }
      80             : 
      81             :   /**
      82             :    * Iterator for the last entry
      83             :    */
      84             :   iterator end() { return _data.begin() + _end_pos; }
      85             :   /**
      86             :    * Const iterator for the last entry
      87             :    */
      88             :   const_iterator end() const { return _data.begin() + _end_pos; }
      89             : 
      90             :   /**
      91             :    * Whether or not the set contains the given item
      92             :    */
      93          15 :   bool contains(const T & value) const
      94             :   {
      95          23 :     for (std::size_t i = 0; i < _end_pos; i++)
      96          13 :       if (_data[i] == value)
      97           5 :         return true;
      98             : 
      99          10 :     return false;
     100             :   }
     101             : 
     102             :   /**
     103             :    * Swap the contents of this set with another
     104             :    */
     105           1 :   void swap(StaticallyAllocatedSet<T, N> & other)
     106             :   {
     107           1 :     _data.swap(other._data);
     108           1 :     std::swap(_end_pos, other._end_pos);
     109           1 :   }
     110             : 
     111             :   /**
     112             :    * Expert interface: the current ending position
     113             :    */
     114           4 :   std::size_t dataEndPos() const { return _end_pos; }
     115             : 
     116             : private:
     117             :   /// The data
     118             :   std::array<T, N> _data;
     119             : 
     120             :   /// Save the ending as positions internally
     121             :   std::size_t _end_pos;
     122             : };
     123             : 
     124             : } // namespace MooseUtils

Generated by: LCOV version 1.14