KItinerary

bitvectorview.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "bitvectorview.h"
7
8#include <QByteArray>
9
10using namespace KItinerary;
11
12BitVectorView::BitVectorView() = default;
13
14BitVectorView::BitVectorView(std::string_view data)
15 : m_data(data)
16{
17}
18
19BitVectorView::~BitVectorView() = default;
20
21BitVectorView::size_type BitVectorView::size() const
22{
23 return m_data.size() * 8;
24}
25
26uint8_t BitVectorView::at(BitVectorView::size_type index) const
27{
28 const auto majIdx = index / 8;
29 const auto minIdx = 7 - (index % 8);
30 if (majIdx >= m_data.size()) {
31 return 0;
32 }
33 return (m_data.at(majIdx) & (1 << minIdx)) >> minIdx;
34}
35
36QByteArray BitVectorView::byteArrayAt(BitVectorView::size_type index, BitVectorView::size_type bytes) const
37{
38 QByteArray result;
39 result.reserve(bytes);
40 for (size_type i = 0; i < bytes; ++i) {
41 const auto b = valueAtMSB<uint8_t>(index + i * 8, 8);
42 result.push_back(b);
43 }
44 return result;
45}
uint8_t at(size_type index) const
Returns the bit value at index.
size_type size() const
Size of this vector in bits.
QByteArray byteArrayAt(size_type index, size_type bytes) const
Returns bytes starting at bit offset index.
T valueAtMSB(size_type index, size_type bits) const
Read a big endian unsigned number at bit offset index and bits in length.
Classes for reservation/travel data models, data extraction and data augmentation.
Definition berelement.h:17
void push_back(QByteArrayView str)
void reserve(qsizetype size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:36 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.