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