NumCpp  2.18.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
unpackbits.hpp
Go to the documentation of this file.
1
28#pragma once
29
31#include "NumCpp/NdArray.hpp"
32
33namespace nc
34{
35 //============================================================================
36 // Method Description:
50 {
51 switch (axis)
52 {
53 case Axis::NONE:
54 {
55 NdArray<uint8> result(1, a.size() * 8);
56
57 for (NdArray<uint8>::size_type byte = 0; byte < a.size(); ++byte)
58 {
59 const auto startIdx = byte * 8;
60 const auto byteValue = a[byte];
61
62 for (uint8 bit = 0; bit < 8; ++bit)
63 {
64 result[startIdx + bit] = static_cast<uint8>((byteValue & (uint8{ 1 } << bit)) >> bit);
65 }
66 }
67
68 return result;
69 }
70 case Axis::COL:
71 {
72 const auto aShape = a.shape();
73 NdArray<uint8> result(aShape.rows, aShape.cols * 8);
74 const auto resultCSlice = result.cSlice();
75 const auto aCSlice = a.cSlice();
76
77 for (NdArray<uint8>::size_type row = 0; row < aShape.rows; ++row)
78 {
79 result.put(row, resultCSlice, unpackbitsLittleEndian(a(row, aCSlice)));
80 }
81
82 return result;
83 }
84 case Axis::ROW:
85 {
86 return unpackbitsLittleEndian(a.transpose(), Axis::COL).transpose();
87 }
88 default:
89 {
90 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
91 return {}; // get rid of compiler warning
92 }
93 }
94 }
95
96 //============================================================================
97 // Method Description:
111 {
112 switch (axis)
113 {
114 case Axis::NONE:
115 {
116 NdArray<uint8> result(1, a.size() * 8);
117
118 for (NdArray<uint8>::size_type byte = 0; byte < a.size(); ++byte)
119 {
120 const auto startIdx = byte * 8;
121 const auto byteValue = a[byte];
122
123 for (uint8 bit = 0; bit < 8; ++bit)
124 {
125 const auto bitToMask = static_cast<uint8>(7 - bit);
126 result[startIdx + bit] =
127 static_cast<uint8>((byteValue & (uint8{ 1 } << bitToMask)) >> bitToMask);
128 }
129 }
130
131 return result;
132 }
133 case Axis::COL:
134 {
135 const auto aShape = a.shape();
136 NdArray<uint8> result(aShape.rows, aShape.cols * 8);
137 const auto resultCSlice = result.cSlice();
138 const auto aCSlice = a.cSlice();
139
140 for (NdArray<uint8>::size_type row = 0; row < aShape.rows; ++row)
141 {
142 result.put(row, resultCSlice, unpackbitsBigEndian(a(row, aCSlice)));
143 }
144
145 return result;
146 }
147 case Axis::ROW:
148 {
149 return unpackbitsBigEndian(a.transpose(), Axis::COL).transpose();
150 }
151 default:
152 {
153 THROW_INVALID_ARGUMENT_ERROR("Unimplemented axis type.");
154 return {}; // get rid of compiler warning
155 }
156 }
157 }
158
159} // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition Error.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition NdArrayCore.hpp:135
size_type size() const noexcept
Definition NdArrayCore.hpp:4600
self_type transpose() const
Definition NdArrayCore.hpp:4959
const Shape & shape() const noexcept
Definition NdArrayCore.hpp:4587
uint32 size_type
Definition NdArrayCore.hpp:152
Slice cSlice(index_type inStartIdx=0, size_type inStepSize=1) const
Definition NdArrayCore.hpp:1004
self_type & put(index_type inIndex, const value_type &inValue)
Definition NdArrayCore.hpp:3769
Definition Cartesian.hpp:39
std::uint8_t uint8
Definition Types.hpp:42
NdArray< uint8 > unpackbitsBigEndian(const NdArray< uint8 > &a, Axis axis=Axis::NONE)
Definition unpackbits.hpp:110
NdArray< uint8 > unpackbitsLittleEndian(const NdArray< uint8 > &a, Axis axis=Axis::NONE)
Definition unpackbits.hpp:49
Axis
Enum To describe an axis.
Definition Enums.hpp:36
@ ROW
Definition Enums.hpp:38
@ COL
Definition Enums.hpp:39
@ NONE
Definition Enums.hpp:37