<commit_before>
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2016 <NAME>. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////


#ifndef GCPP_BITFLAGS
#define GCPP_BITFLAGS

#include "util.h"

#include <climits>
#include <memory>
#include <algorithm>
#include <type_traits>

namespace gcpp {

	//----------------------------------------------------------------------------
	//
	//	vector<bool> operations aren't always optimized, so here's a custom class.
	//
	//----------------------------------------------------------------------------

	class bitflags {
		using unit = unsigned int;
		static_assert(std::is_unsigned<unit>::value, "unit must be an unsigned integral type.");

		std::unique_ptr<unit[]> bits;
		const int size;

		static constexpr auto bits_per_unit = static_cast<int>(sizeof(unit) * CHAR_BIT);

		//  Return a unit with all bits set if "set" is true, or all bits cleared otherwise.
		//
		static constexpr unit all_bits(bool set) noexcept {
			return set ? ~unit(0) : unit(0);
		}

		//  Return a mask that will select the bit at position from its unit
		//
		static unit bit_mask(int at) noexcept {
			Expects(0 <= at && "position must be non-negative");
			return unit(1) << (at % bits_per_unit);
		}

		//  Return the number of units needed to represent a number of bits
		//
		static int unit_count(int bit_count) noexcept {
			Expects(0 <= bit_count && "bit_count must be non-negative");
			return (bit_count + bits_per_unit - 1) / bits_per_unit;
		}

		//  Get the unit that contains the bit at position
		//
		unit& bit_unit(int at) noexcept {
			Expects(0 <= at && "position must be non-negative");
			return bits[at / bits_per_unit];
		}
		const unit& bit_unit(int at) const noexcept {
			Expects(0 <= at && "position must be non-negative");
			return bits[at / bits_per_unit];
		}

	public:
		bitflags(int nbits, bool value)
			: size{ nbits }
		{
			Expects(nbits > 0 && "#bits must be positive");
			bits = std::make_unique<unit[]>(unit_count(nbits));
			if (value) {
				set_all(true);
			}
		}

		//	Get flag value at position
		//
		bool get(int at) const noexcept {
			Expects(0 <= at && at < size && "bitflags get() out of range");
			return (bit_unit(at) & bit_mask(at)) != unit(0);
		}

		//	Test whether all bits are false
		//
		bool all_false() const noexcept {
			auto test = [](unit u) { return u > unit(0); };
			return std::none_of(bits.get(), bits.get() + unit_count(size) - 1, test)
				&& !test(*(bits.get() + unit_count(size) - 1) & (bit_mask(size) - 1));
		}

		//	Set flag value at position
		//
		void set(int at, bool value) noexcept {
			Expects(0 <= at && at < size && "bitflags set() out of range");
			if (value) {
				bit_unit(at) |= bit_mask(at);
			}
			else {
				bit_unit(at) &= ~bit_mask(at);
			}
		}

		//	Set all flags to value
		//
		void set_all(bool value) noexcept {<commit_msg>Give a more readable name to "test" and avoid double negatives<commit_after>
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2016 <NAME>. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////


#ifndef GCPP_BITFLAGS
#define GCPP_BITFLAGS

#include "util.h"

#include <climits>
#include <memory>
#include <algorithm>
#include <type_traits>

namespace gcpp {

	//----------------------------------------------------------------------------
	//
	//	vector<bool> operations aren't always optimized, so here's a custom class.
	//
	//----------------------------------------------------------------------------

	class bitflags {
		using unit = unsigned int;
		static_assert(std::is_unsigned<unit>::value, "unit must be an unsigned integral type.");

		std::unique_ptr<unit[]> bits;
		const int size;

		static constexpr auto bits_per_unit = static_cast<int>(sizeof(unit) * CHAR_BIT);

		//  Return a unit with all bits set if "set" is true, or all bits cleared otherwise.
		//
		static constexpr unit all_bits(bool set) noexcept {
			return set ? ~unit(0) : unit(0);
		}

		//  Return a mask that will select the bit at position from its unit
		//
		static unit bit_mask(int at) noexcept {
			Expects(0 <= at && "position must be non-negative");
			return unit(1) << (at % bits_per_unit);
		}

		//  Return the number of units needed to represent a number of bits
		//
		static int unit_count(int bit_count) noexcept {
			Expects(0 <= bit_count && "bit_count must be non-negative");
			return (bit_count + bits_per_unit - 1) / bits_per_unit;
		}

		//  Get the unit that contains the bit at position
		//
		unit& bit_unit(int at) noexcept {
			Expects(0 <= at && "position must be non-negative");
			return bits[at / bits_per_unit];
		}
		const unit& bit_unit(int at) const noexcept {
			Expects(0 <= at && "position must be non-negative");
			return bits[at / bits_per_unit];
		}

	public:
		bitflags(int nbits, bool value)
			: size{ nbits }
		{
			Expects(nbits > 0 && "#bits must be positive");
			bits = std::make_unique<unit[]>(unit_count(nbits));
			if (value) {
				set_all(true);
			}
		}

		//	Get flag value at position
		//
		bool get(int at) const noexcept {
			Expects(0 <= at && at < size && "bitflags get() out of range");
			return (bit_unit(at) & bit_mask(at)) != unit(0);
		}

		//	Test whether all bits are false
		//
		bool all_false() const noexcept {
			auto all_false = [](unit u) { return u == unit(0); };
			return std::all_of(bits.get(), bits.get() + unit_count(size) - 1, all_false)
				&& all_false(*(bits.get() + unit_count(size) - 1) & (bit_mask(size) - 1));
		}

		//	Set flag value at position
		//
		void set(int at, bool value) noexcept {
			Expects(0 <= at && at < size && "bitflags set() out of range");
			if (value) {
				bit_unit(at) |= bit_mask(at);
			}
			else {
				bit_unit(at) &= ~bit_mask(at);
			}
		}

		//	Set all flags to value
		//
		void set_all(bool value) noexcept {
{'fraction_non_alphanumeric': 0.20461952671106703, 'fraction_numerical': 0.005668130933824571, 'mean_word_length': 2.266080518278575, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7163438', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2110, 'n_words': 746}