• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PeterCDMcLean / BitLib / 14816556113

04 May 2025 01:49AM UTC coverage: 97.086% (-0.5%) from 97.623%
14816556113

Pull #2

github

web-flow
Merge 947e4829a into d0bfc09a2
Pull Request #2: Break up bitlib_utils. Seed tests from the test case name to prevent varying coverage results

1266 of 1304 relevant lines covered (97.09%)

16786579.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.48
/include/bitlib/bit-algorithms/equal.hpp
1
// ================================= EQUAL =================================== //
2
// Project:     The Experimental Bit Algorithms Library
3
// Name:        equal.hpp
4
// Contributor: Bryce Kille [2019]
5
// License:     BSD 3-Clause License
6
// ========================================================================== //
7
#ifndef _EQUAL_HPP_INCLUDED
8
#define _EQUAL_HPP_INCLUDED
9
// ========================================================================== //
10

11

12

13
// ================================ PREAMBLE ================================ //
14
// C++ standard library
15
#include <type_traits>
16
#include <math.h>
17
// Project sources
18
#include "bitlib/bit-iterator/bit.hpp"
19
// Third-party libraries
20
// Miscellaneous
21
namespace bit {
22
// ========================================================================== //
23

24

25

26
// ---------------------------- Equal Algorithms ----------------------------- //
27

28
// Status: Does not work for Input/Output iterators due to distance call
29
template <class RandomAccessIt1, class RandomAccessIt2>
30
constexpr bool equal(
1,024✔
31
        bit_iterator<RandomAccessIt1> first,
32
        bit_iterator<RandomAccessIt1> last,
33
        bit_iterator<RandomAccessIt2> d_first
34
)
35
{
36
    // Types and constants
37
    using dst_word_type = typename bit_iterator<RandomAccessIt2>::word_type;
38
    using src_word_type = typename bit_iterator<RandomAccessIt1>::word_type;
39
    using word_type = dst_word_type;
40
    using size_type = typename bit_iterator<RandomAccessIt2>::size_type;
41
    constexpr size_type digits = binary_digits<word_type>::value;
1,024✔
42

43
    // Assertions
44
    _assert_range_viability(first, last);
1,024✔
45
    static_assert(::std::is_same<dst_word_type, src_word_type>::value, "Underlying word types must be equal");
46
    if (first == last) return true;
1,024✔
47

48
    // Initialization
49
    const bool is_d_first_aligned = d_first.position() == 0;
1,020✔
50
    size_type total_bits_to_check = distance(first, last);
1,020✔
51
    size_type remaining_bits_to_check = total_bits_to_check;
1,020✔
52
    auto it = d_first.base();
1,020✔
53

54
    // d_first is not aligned.
55
    if (!is_d_first_aligned) {
1,020✔
56
        const size_type partial_bits_to_check = ::std::min(
854✔
57
                remaining_bits_to_check,
58
                digits - d_first.position());
1,708✔
59
        const word_type mask = static_cast<word_type>(
922✔
60
                (static_cast<word_type>(1) << partial_bits_to_check) - 1
786✔
61
        ) << d_first.position();
854✔
62
        const word_type comp = static_cast<word_type>(
854✔
63
              get_word<word_type>(first, partial_bits_to_check)
854✔
64
                << d_first.position());
854✔
65
        if ((mask & *it) != (mask & comp)) { return false; }
854✔
66
        remaining_bits_to_check -= partial_bits_to_check;
854✔
67
        advance(first, partial_bits_to_check);
854✔
68
        it++;
854✔
69
    }
70

71
    if (remaining_bits_to_check > 0) {
1,020✔
72
        const bool is_first_aligned = first.position() == 0;
742✔
73
        // d_first will be aligned at this point
74
        if (is_first_aligned && remaining_bits_to_check >= digits) {
742✔
75
            auto N = ::std::distance(first.base(), last.base());
490✔
76
            bool found_mismatch = !::std::equal(first.base(), last.base(), it);
490✔
77
            if (found_mismatch) {return false;}
490✔
78
            it += N;
490✔
79
            first += digits * N;
490✔
80
            remaining_bits_to_check -= digits * N;
490✔
81
        } else {
490✔
82
            // TODO benchmark if its faster to ::std::check the entire range then shift
83
            while (remaining_bits_to_check >= digits) {
252✔
84
                if (*it != get_word<word_type>(first, digits)) {return false;}
×
85
                remaining_bits_to_check -= digits;
×
86
                it++;
×
87
                advance(first, digits);
×
88
            }
89
        }
90
        if (remaining_bits_to_check > 0) {
742✔
91
            const word_type mask = static_cast<word_type>(
1,434✔
92
                    (static_cast<word_type>(1) << remaining_bits_to_check) - 1
717✔
93
            );
94
            const word_type comp = get_word<word_type>(first, remaining_bits_to_check);
717✔
95
            if ((mask & *it) != (mask & comp)) { return false; }
717✔
96
        }
97
    }
98
    return true;
1,019✔
99
}
100
// -------------------------------------------------------------------------- //
101

102

103

104
// ========================================================================== //
105
} // namespace bit
106
#endif // _EQUAL_HPP_INCLUDED
107
// ========================================================================== //
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc