Prison

bitvector.cpp
1/*
2 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: MIT
5*/
6
7#include "bitvector_p.h"
8
9using namespace Prison;
10
11BitVector::BitVector() = default;
12BitVector::~BitVector() = default;
13
14void BitVector::appendLSB(int data, int bits)
15{
16 for (int i = 0; i < bits; ++i) {
17 appendBit(data & (1 << i));
18 }
19}
20
21void BitVector::appendMSB(int data, int bits)
22{
23 for (int i = bits - 1; i >= 0; --i) {
24 appendBit(data & (1 << i));
25 }
26}
27
28void BitVector::appendBit(bool bit)
29{
30 const auto subIdx = m_size % 8;
31 if (subIdx == 0) {
32 m_data.append('\0');
33 }
34 if (bit) {
35 m_data.data()[m_data.size() - 1] |= (1 << subIdx);
36 }
37 ++m_size;
38}
39
40void BitVector::append(const BitVector &other)
41{
42 for (int i = 0; i < other.size(); ++i) {
43 appendBit(other.at(i));
44 }
45}
46
47bool BitVector::at(int index) const
48{
49 const auto majIdx = index / 8;
50 const auto minIdx = index % 8;
51 return (m_data.at(majIdx) & (1 << minIdx)) >> minIdx;
52}
53
54void BitVector::clear()
55{
56 m_data.clear();
57 m_size = 0;
58}
59
60void BitVector::reserve(int size)
61{
62 m_data.reserve((size / 8) + 1);
63}
64
65int BitVector::size() const
66{
67 return m_size;
68}
69
70int BitVector::valueAtMSB(int index, int size) const
71{
72 int res = 0;
73 for (int i = 0; i < size; ++i) {
74 res = res << 1;
75 res |= (at(index + i) ? 1 : 0);
76 }
77 return res;
78}
79
80BitVector::iterator BitVector::begin() const
81{
82 iterator it;
83 it.m_index = 0;
84 it.m_vector = this;
85 return it;
86}
87
88BitVector::iterator BitVector::end() const
89{
90 iterator it;
91 it.m_index = m_size;
92 it.m_vector = this;
93 return it;
94}
95
96bool BitVector::operator==(const BitVector &other) const
97{
98 return m_size == other.m_size && m_data == other.m_data;
99}
100
101bool BitVector::operator!=(const Prison::BitVector &other) const
102{
103 return m_size != other.m_size || m_data != other.m_data;
104}
105
106QDebug operator<<(QDebug dbg, const Prison::BitVector &v)
107{
108 dbg << v.m_data.toHex();
109 return dbg;
110}
KCALENDARCORE_EXPORT QDataStream & operator<<(QDataStream &out, const KCalendarCore::Alarm::Ptr &)
Provides classes and methods for generating barcodes.
Definition barcode.h:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:51 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.