Skip to content

Commit

Permalink
BitSet initializer constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Nov 9, 2023
1 parent 04849da commit dd97add
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ set(HEADERS
include/vclib/concepts/pointers.h
include/vclib/concepts/range.h
include/vclib/concepts/space.h
include/vclib/concepts/types.h
include/vclib/concepts/mesh/components.h
include/vclib/concepts/mesh/containers.h
include/vclib/concepts/mesh/edge_mesh_concept.h
Expand Down
1 change: 1 addition & 0 deletions include/vclib/concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "concepts/pointers.h"
#include "concepts/range.h"
#include "concepts/space.h"
#include "concepts/types.h"

/**
* @defgroup concepts Concepts
Expand Down
46 changes: 46 additions & 0 deletions include/vclib/concepts/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*****************************************************************************
* VCLib *
* Visual Computing Library *
* *
* Copyright(C) 2021-2023 *
* Alessandro Muntoni *
* Visual Computing Lab *
* ISTI - Italian National Research Council *
* *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
* for more details. *
****************************************************************************/

#ifndef VCL_CONCEPTS_TYPES_H
#define VCL_CONCEPTS_TYPES_H

#include <concepts>

namespace vcl {

/**
* @brief Concept for types that can be used as indices.
*/
template<typename T>
concept IntegralOrEnum = std::integral<T> || std::is_enum_v<T>;


/**
* @brief Concept for types that can be used as indices, excluding bool.
*/
template<typename T>
concept NonBoolIntegralOrEnum = IntegralOrEnum<T> && !std::same_as<T, bool>;

} // namespace vcl

#endif // VCL_CONCEPTS_TYPES_H
45 changes: 44 additions & 1 deletion include/vclib/space/bit_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <stdexcept>
#include <string>

#include <vclib/concepts/types.h>

#include "bit_set/bit_proxy.h"

namespace vcl {
Expand Down Expand Up @@ -60,7 +62,46 @@ class BitSet
/**
* @brief Empty constructor. All the Bits of the BitSet are set to false.
*/
BitSet() {}
BitSet() = default;

/**
* @brief Constructor from list of integral (or enum) values that represent
* the indices of the true bits, allowing braces initialization.
*
* Creates a BitSet by setting to true the bits at the indices specified in
* the list. All the other bits are set to false. Each value of the list
* must be less than the number of bits of the BitSet.
*
* @param[in] l: the list of bit positions to set to true.
*/
template<NonBoolIntegralOrEnum I>
BitSet(std::initializer_list<I> l)
{
for (const auto& i : l)
at(i) = true;
}

/**
* @brief Constructor from a list of boolean values, allowing braces
* initialization.
*
* Constructs the bitset from the list of boolean values. The size of the
* list must be less or equal to the number of bits of the BitSet.
*
* @param[in] l: the list of boolean values to assign to the BitSet.
*/
template<typename B>
BitSet(std::initializer_list<B> l) requires std::same_as<bool, B>
{
if (l.size() > SIZE)
throw std::invalid_argument(
"BitSet: list size is greater than the number of bits of the "
"BitSet");

uint i = 0;
for (const auto& b : l)
at(i++) = b;
}

/**
* @brief Returns the number of bits of the BitSet.
Expand Down Expand Up @@ -200,6 +241,8 @@ class BitSet
at(i) = !at(i);
return *this;
}

auto operator<=>(const BitSet<T>&) const = default;
};

/* Specialization Aliases */
Expand Down

0 comments on commit dd97add

Please sign in to comment.