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

PeterCDMcLean / BitLib / 15380630124

01 Jun 2025 11:33PM UTC coverage: 52.575% (-0.2%) from 52.785%
15380630124

Pull #16

github

web-flow
Merge b85ad7435 into a32e9aeba
Pull Request #16: Small buffer optimization

8860 of 16682 branches covered (53.11%)

Branch coverage included in aggregate %.

99 of 107 new or added lines in 8 files covered. (92.52%)

1 existing line in 1 file now uncovered.

5502 of 10635 relevant lines covered (51.73%)

7820224.37 hits per line

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

55.66
/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(
2,060✔
31
        bit_iterator<RandomAccessIt1> first,
32
        bit_iterator<RandomAccessIt1> last,
33
        bit_iterator<RandomAccessIt2> d_first
34
)
35
{
2,060✔
36
    // Types and constants
37
    using dst_word_type = typename bit_iterator<RandomAccessIt2>::word_type;
2,060✔
38
    using src_word_type = typename bit_iterator<RandomAccessIt1>::word_type;
2,060✔
39
    if constexpr (!std::is_same_v<src_word_type, dst_word_type> && bitsof<src_word_type>() != bitsof<dst_word_type>()) {
2,060✔
40
      if constexpr (bitsof<src_word_type>() > bitsof<dst_word_type>()) {
2✔
41
        bit_iterator<bit_iterator_adapter<RandomAccessIt2, RandomAccessIt1>> adapted_first(first);
2✔
42
        bit_iterator<bit_iterator_adapter<RandomAccessIt2, RandomAccessIt1>> adapted_last(last);
2✔
43
        return equal(adapted_first, adapted_last, d_first);
2✔
44
      } else {
1✔
45
        bit_iterator<bit_iterator_adapter<RandomAccessIt1, RandomAccessIt2>> adapted_d_first(d_first);
2✔
46
        return equal(first, last, adapted_d_first);
2✔
47
      }
1✔
48
    } else {
2,058✔
49
      static_assert(::std::is_same<dst_word_type, src_word_type>::value, "Underlying word types must be equal");
2,058✔
50
    }
2,058✔
UNCOV
51
    using word_type = dst_word_type;
52
    using size_type = typename bit_iterator<RandomAccessIt2>::size_type;
2,060✔
53
    constexpr size_type digits = binary_digits<word_type>::value;
4,118✔
54

55
    // Assertions
56
    _assert_range_viability(first, last);
4,118✔
57
    if (first == last) return true;
4,118!
58

59
    // Initialization
60
    const bool is_d_first_aligned = d_first.position() == 0;
3,962✔
61
    size_type total_bits_to_check = distance(first, last);
3,962✔
62
    size_type remaining_bits_to_check = total_bits_to_check;
3,962✔
63
    auto it = d_first.base();
3,962✔
64

65
    // d_first is not aligned.
66
    if (!is_d_first_aligned) {
3,962!
67
        const size_type partial_bits_to_check = ::std::min(
3,418✔
68
                remaining_bits_to_check,
1,719✔
69
                digits - d_first.position());
5,117✔
70
        const word_type mask = _mask<word_type>(partial_bits_to_check) << d_first.position();
3,418✔
71
        const word_type comp = static_cast<word_type>(
3,418✔
72
              get_word<word_type>(first, partial_bits_to_check)
4,975✔
73
                << d_first.position());
3,560✔
74
        if ((mask & *it) != (mask & comp)) { return false; }
3,418!
75
        remaining_bits_to_check -= partial_bits_to_check;
3,418✔
76
        advance(first, partial_bits_to_check);
3,418✔
77
        it++;
3,418✔
78
    }
1,719✔
79

80
    if (remaining_bits_to_check > 0) {
3,962!
81
        const bool is_first_aligned = first.position() == 0;
2,850✔
82
        // d_first will be aligned at this point
83
        if (is_first_aligned && remaining_bits_to_check >= digits) {
2,850!
84
            auto N = ::std::distance(first.base(), last.base());
1,826✔
85
            bool found_mismatch = !::std::equal(first.base(), last.base(), it);
1,826✔
86
            if (found_mismatch) {return false;}
1,826!
87
            it += N;
1,826✔
88
            first += digits * N;
1,826✔
89
            remaining_bits_to_check -= digits * N;
1,826✔
90
        } else {
1,826✔
91
            // TODO benchmark if its faster to ::std::check the entire range then shift
92
            while (remaining_bits_to_check >= digits) {
1,024!
93
                if (*it != get_word<word_type>(first, digits)) {return false;}
×
94
                remaining_bits_to_check -= digits;
×
95
                it++;
×
96
                advance(first, digits);
×
97
            }
98
        }
522✔
99
        if (remaining_bits_to_check > 0) {
2,850!
100
          const word_type mask = _mask<word_type>(remaining_bits_to_check);
2,750✔
101
          const word_type comp = get_word<word_type>(first, remaining_bits_to_check);
2,750✔
102
          if ((mask & *it) != (mask & comp)) {
2,750!
103
            return false;
6✔
104
          }
3✔
105
        }
1,378✔
106
    }
1,425✔
107
    return true;
3,956✔
108
}
1,934✔
109
// -------------------------------------------------------------------------- //
110

111

112

113
// ========================================================================== //
114
} // namespace bit
115
#endif // _EQUAL_HPP_INCLUDED
116
// ========================================================================== //
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