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

PeterCDMcLean / BitLib / 16780993828

06 Aug 2025 03:13PM UTC coverage: 71.949% (-2.8%) from 74.757%
16780993828

Pull #28

github

web-flow
Merge 77a02a220 into 61746a6ab
Pull Request #28: Use upload / download action instead of cache

3451 of 5352 branches covered (64.48%)

Branch coverage included in aggregate %.

2438 of 2833 relevant lines covered (86.06%)

16239128.44 hits per line

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

66.98
/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
//              Peter McLean [2025]
6
// License:     BSD 3-Clause License
7
// ========================================================================== //
8
#ifndef _EQUAL_HPP_INCLUDED
9
#define _EQUAL_HPP_INCLUDED
10
// ========================================================================== //
11

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

24
// Status: Does not work for Input/Output iterators due to distance call
25
template <typename RandomAccessIt1, typename RandomAccessIt2>
26
constexpr bool equal(
27
    const bit_iterator<RandomAccessIt1>& first,
28
    const bit_iterator<RandomAccessIt1>& last,
29
    const bit_iterator<RandomAccessIt2>& d_first) {
7,606✔
30
  return with_bit_iterator_adapter<equal_impl>(first, last, d_first);
7,606✔
31
}
7,606✔
32

33
// ---------------------------- Equal Algorithms ----------------------------- //
34

35
// Status: Does not work for Input/Output iterators due to distance call
36
struct equal_impl {
37
  template <typename RandomAccessIt1, typename RandomAccessIt2>
38
  constexpr bool operator()(
39
      bit_iterator<RandomAccessIt1> first,
40
      bit_iterator<RandomAccessIt1> last,
41
      bit_iterator<RandomAccessIt2> d_first) {
7,606✔
42
    // Types and constants
43
    using dst_word_type = typename bit_iterator<RandomAccessIt2>::word_type;
7,606✔
44
    using src_word_type = typename bit_iterator<RandomAccessIt1>::word_type;
7,606✔
45
    static_assert(::std::is_same<dst_word_type, src_word_type>::value, "Underlying word types must be equal");
7,606✔
46
    using word_type = dst_word_type;
7,606✔
47
    using size_type = typename bit_iterator<RandomAccessIt2>::size_type;
7,606✔
48
    constexpr size_type digits = binary_digits<word_type>::value;
7,606✔
49

50
    // Assertions
51
    _assert_range_viability(first, last);
7,606✔
52
    if (first == last) {
7,606!
53
      return true;
300✔
54
    }
300✔
55

56
    // Initialization
57
    const bool is_d_first_aligned = d_first.position() == 0;
7,306✔
58
    size_type total_bits_to_check = distance(first, last);
7,306✔
59
    size_type remaining_bits_to_check = total_bits_to_check;
7,306✔
60
    auto it = d_first.base();
7,306✔
61

62
    // d_first is not aligned.
63
    if (!is_d_first_aligned) {
7,306!
64
      const size_type partial_bits_to_check = ::std::min(
6,035✔
65
          remaining_bits_to_check,
6,035✔
66
          digits - d_first.position());
6,035✔
67
      const word_type mask = _mask<word_type>(partial_bits_to_check) << d_first.position();
6,035✔
68
      const word_type comp = static_cast<word_type>(
6,035✔
69
          get_word<word_type>(first, partial_bits_to_check)
6,035✔
70
          << d_first.position());
6,035✔
71
      if ((mask & *it) != (mask & comp)) {
6,035!
72
        return false;
1,073✔
73
      }
1,073✔
74
      remaining_bits_to_check -= partial_bits_to_check;
4,962✔
75
      advance(first, partial_bits_to_check);
4,962✔
76
      it++;
4,962✔
77
    }
4,962✔
78

79
    if (remaining_bits_to_check > 0) {
6,233!
80
      const bool is_first_aligned = first.position() == 0;
5,126✔
81
      // d_first will be aligned at this point
82
      if (is_first_aligned && remaining_bits_to_check >= digits) {
5,126!
83
        auto N = ::std::distance(first.base(), last.base());
4,077✔
84
        bool found_mismatch = !::std::equal(first.base(), last.base(), it);
4,077✔
85
        if (found_mismatch) {
4,077!
86
          return false;
959✔
87
        }
959✔
88
        it += N;
3,118✔
89
        first += digits * N;
3,118✔
90
        remaining_bits_to_check -= digits * N;
3,118✔
91
      } else {
3,118✔
92
        // TODO benchmark if its faster to ::std::check the entire range then shift
93
        while (remaining_bits_to_check >= digits) {
1,049!
94
          if (*it != get_word<word_type>(first, digits)) {
×
95
            return false;
×
96
          }
×
97
          remaining_bits_to_check -= digits;
×
98
          it++;
×
99
          advance(first, digits);
×
100
        }
×
101
      }
1,049✔
102
      if (remaining_bits_to_check > 0) {
4,167!
103
        const word_type mask = _mask<word_type>(remaining_bits_to_check);
3,940✔
104
        const word_type comp = get_word<word_type>(first, remaining_bits_to_check);
3,940✔
105
        if ((mask & *it) != (mask & comp)) {
3,940!
106
          return false;
85✔
107
        }
85✔
108
      }
3,940✔
109
    }
4,167✔
110
    return true;
5,189✔
111
  }
6,233✔
112
};
113
// -------------------------------------------------------------------------- //
114

115

116

117
// ========================================================================== //
118
} // namespace bit
119
#endif // _EQUAL_HPP_INCLUDED
120
// ========================================================================== //
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

© 2026 Coveralls, Inc