Skip to content

Commit

Permalink
style: experiment with clang-format / random indentation fixes
Browse files Browse the repository at this point in the history
Two goals:
 a) come closer to what the current code base looks like
 b) make up my mind about what I prefer

This is a WIP and definitively not globally applicable, yet. This partly
fixes inconsistencies with what was already established style.
  • Loading branch information
axxel committed May 6, 2022
1 parent 67deb6a commit bec670d
Show file tree
Hide file tree
Showing 36 changed files with 330 additions and 426 deletions.
12 changes: 8 additions & 4 deletions .clang-format
Expand Up @@ -8,22 +8,26 @@ TabWidth: 4
UseTab: ForContinuationAndIndentation # ForIndentation

AccessModifierOffset: -4
BreakBeforeBraces: Mozilla
ColumnLimit: 120
ColumnLimit: 135

#AlignConsecutiveAssignments: true
AlignConsecutiveBitFields: true
AlignEscapedNewlines: DontAlign
AlignTrailingComments: true
AllowShortCaseLabelsOnASingleLine: true

AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: Inline
#AllowShortLambdasOnASingleLine: Inline
AllowShortEnumsOnASingleLine: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true

AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Mozilla
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: true
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
FixNamespaceComments: true
IncludeBlocks: Regroup
KeepEmptyLinesAtTheStartOfBlocks: false
Expand Down
85 changes: 42 additions & 43 deletions core/src/BitArray.h
Expand Up @@ -36,7 +36,8 @@ namespace ZXing {
class ByteArray;

template <typename Iterator>
struct Range {
struct Range
{
Iterator begin, end;
explicit operator bool() const { return begin < end; }
int size() const { return static_cast<int>(end - begin); }
Expand Down Expand Up @@ -129,48 +130,50 @@ class BitArray

BitArray() = default;

explicit BitArray(int size) :
explicit BitArray(int size)
:
#ifdef ZX_FAST_BIT_STORAGE
_bits(size, 0) {}
_bits(size, 0) {}
#else
_size(size), _bits((size + 31) / 32, 0) {}
_size(size),
_bits((size + 31) / 32, 0) {}
#endif

BitArray(BitArray&& other) noexcept :
BitArray(BitArray&& other) noexcept
:
#ifndef ZX_FAST_BIT_STORAGE
_size(other._size),
_size(other._size),
#endif
_bits(std::move(other._bits)) {}
_bits(std::move(other._bits)) {}

BitArray& operator=(BitArray&& other) noexcept {
BitArray& operator=(BitArray&& other) noexcept
{
#ifndef ZX_FAST_BIT_STORAGE
_size = other._size;
#endif
_bits = std::move(other._bits);
return *this;
}

BitArray copy() const {
return *this;
}
BitArray copy() const { return *this; }

int size() const noexcept {
int size() const noexcept
{
#ifdef ZX_FAST_BIT_STORAGE
return Size(_bits);
#else
return _size;
#endif
}

int sizeInBytes() const noexcept {
return (size() + 7) / 8;
}

int sizeInBytes() const noexcept { return (size() + 7) / 8; }

/**
* @param i bit to get
* @return true iff bit i is set
*/
bool get(int i) const {
bool get(int i) const
{
#ifdef ZX_FAST_BIT_STORAGE
return _bits.at(i) != 0;
#else
Expand All @@ -188,7 +191,8 @@ class BitArray
Iterator end() const noexcept { return _bits.cend(); }

template <typename ITER>
static ITER getNextSetTo(ITER begin, ITER end, bool v) noexcept {
static ITER getNextSetTo(ITER begin, ITER end, bool v) noexcept
{
while( begin != end && *begin != static_cast<int>(v) )
++begin;
return begin;
Expand All @@ -198,7 +202,8 @@ class BitArray
Iterator begin() const noexcept { return iterAt(0); }
Iterator end() const noexcept { return iterAt(_size); }

static Iterator getNextSetTo(Iterator begin, Iterator end, bool v) {
static Iterator getNextSetTo(Iterator begin, Iterator end, bool v)
{
auto i = begin;
// reconstruct _bits.end()
auto bitsEnd = end._mask == 0x1 ? end._value : std::next(end._value);
Expand All @@ -216,16 +221,15 @@ class BitArray
return i;
}

static ReverseIterator getNextSetTo(ReverseIterator begin, ReverseIterator end, bool v) {
static ReverseIterator getNextSetTo(ReverseIterator begin, ReverseIterator end, bool v)
{
while( begin != end && *begin != v )
++begin;
return begin;
}
#endif

Iterator getNextSetTo(Iterator i, bool v) const {
return getNextSetTo(i, end(), v);
}
Iterator getNextSetTo(Iterator i, bool v) const { return getNextSetTo(i, end(), v); }

Iterator getNextSet(Iterator i) const { return getNextSetTo(i, true); }
Iterator getNextUnset(Iterator i) const { return getNextSetTo(i, false); }
Expand All @@ -238,7 +242,8 @@ class BitArray
*
* @param i bit to set
*/
void set(int i, bool val) {
void set(int i, bool val)
{
#ifdef ZX_FAST_BIT_STORAGE
_bits.at(i) = val;
#else
Expand All @@ -252,9 +257,7 @@ class BitArray
/**
* Clears all bits (sets to false).
*/
void clearBits() {
std::fill(_bits.begin(), _bits.end(), 0);
}
void clearBits() { std::fill(_bits.begin(), _bits.end(), 0); }

/**
* Efficient method to check if a range of bits is set, or not set.
Expand All @@ -266,7 +269,8 @@ class BitArray
* @throws IllegalArgumentException if end is less than or equal to start
*/
#ifdef ZX_FAST_BIT_STORAGE
bool isRange(int start, int end, bool value) const {
bool isRange(int start, int end, bool value) const
{
return std::all_of(std::begin(_bits) + start, std::begin(_bits) + end, [value](uint8_t v) { return v == static_cast<int>(value); });
}
#else
Expand All @@ -276,7 +280,8 @@ class BitArray
// Little helper method to make common isRange use case more readable.
// Pass positive zone size to look for quiet zone after i and negative for zone in front of i.
// Set allowClippedZone to false if clipping the zone at the image border is not acceptable.
bool hasQuietZone(Iterator i, int signedZoneSize, bool allowClippedZone = true) const {
bool hasQuietZone(Iterator i, int signedZoneSize, bool allowClippedZone = true) const
{
int index = static_cast<int>(i - begin());
if (signedZoneSize > 0) {
if (!allowClippedZone && index + signedZoneSize >= size())
Expand All @@ -289,7 +294,8 @@ class BitArray
}
}

bool hasQuietZone(ReverseIterator i, int signedZoneSize, bool allowClippedZone = true) const {
bool hasQuietZone(ReverseIterator i, int signedZoneSize, bool allowClippedZone = true) const
{
return hasQuietZone(i.base(), -signedZoneSize, allowClippedZone);
}

Expand All @@ -302,25 +308,20 @@ class BitArray
* @param numBits bits from value to append
*/
#ifdef ZX_FAST_BIT_STORAGE
void appendBits(int value, int numBits) {
void appendBits(int value, int numBits)
{
for (; numBits; --numBits)
_bits.push_back((value >> (numBits-1)) & 1);
}

void appendBit(bool bit) {
_bits.push_back(bit);
}
void appendBit(bool bit) { _bits.push_back(bit); }

void appendBitArray(const BitArray& other) {
_bits.insert(_bits.end(), other.begin(), other.end());
}
void appendBitArray(const BitArray& other) { _bits.insert(_bits.end(), other.begin(), other.end()); }

/**
* Reverses all bits in the array.
*/
void reverse() {
std::reverse(_bits.begin(), _bits.end());
}
void reverse() { std::reverse(_bits.begin(), _bits.end()); }
#else
void appendBits(int value, int numBits);

Expand All @@ -331,9 +332,7 @@ class BitArray
/**
* Reverses all bits in the array.
*/
void reverse() {
BitHacks::Reverse(_bits, _bits.size() * 32 - _size);
}
void reverse() { BitHacks::Reverse(_bits, _bits.size() * 32 - _size); }
#endif

void bitwiseXOR(const BitArray& other);
Expand Down
55 changes: 27 additions & 28 deletions core/src/BitMatrix.h
Expand Up @@ -81,28 +81,31 @@ class BitMatrix

BitMatrix(int width, int height) : _width(width), _height(height), _rowSize(width), _bits(width * height, UNSET_V) {}
#else
BitMatrix(int width, int height) : _width(width), _height(height), _rowSize((width + 31) / 32), _bits(((width + 31) / 32) * _height, 0) {}
BitMatrix(int width, int height)
: _width(width), _height(height), _rowSize((width + 31) / 32), _bits(((width + 31) / 32) * _height, 0)
{}
#endif

explicit BitMatrix(int dimension) : BitMatrix(dimension, dimension) {} // Construct a square matrix.

BitMatrix(BitMatrix&& other) noexcept : _width(other._width), _height(other._height), _rowSize(other._rowSize), _bits(std::move(other._bits)) {}
BitMatrix(BitMatrix&& other) noexcept
: _width(other._width), _height(other._height), _rowSize(other._rowSize), _bits(std::move(other._bits))
{}

BitMatrix& operator=(BitMatrix&& other) noexcept {
BitMatrix& operator=(BitMatrix&& other) noexcept
{
_width = other._width;
_height = other._height;
_rowSize = other._rowSize;
_bits = std::move(other._bits);
return *this;
}

BitMatrix copy() const {
return *this;
}
BitMatrix copy() const { return *this; }

#ifdef ZX_FAST_BIT_STORAGE
// experimental iterator based access
template<typename iterator>
template <typename iterator>
struct Row
{
iterator _begin, _end;
Expand All @@ -120,7 +123,8 @@ class BitMatrix
* @param y The vertical component (i.e. which row)
* @return value of given bit in matrix
*/
bool get(int x, int y) const {
bool get(int x, int y) const
{
#ifdef ZX_FAST_BIT_STORAGE
return isSet(get(y * _width + x));
#else
Expand All @@ -134,23 +138,26 @@ class BitMatrix
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
*/
void set(int x, int y) {
void set(int x, int y)
{
#ifdef ZX_FAST_BIT_STORAGE
get(y * _width + x) = SET_V;
#else
get(y * _rowSize + (x / 32)) |= 1 << (x & 0x1f);
#endif
}

void unset(int x, int y) {
void unset(int x, int y)
{
#ifdef ZX_FAST_BIT_STORAGE
get(y * _width + x) = UNSET_V;
#else
get(y * _rowSize + (x / 32)) &= ~(1 << (x & 0x1f));
#endif
}

void set(int x, int y, bool val) {
void set(int x, int y, bool val)
{
#ifdef ZX_FAST_BIT_STORAGE
get(y * _width + x) = val ? SET_V : UNSET_V;
#else
Expand All @@ -164,7 +171,8 @@ class BitMatrix
* @param x The horizontal component (i.e. which column)
* @param y The vertical component (i.e. which row)
*/
void flip(int x, int y) {
void flip(int x, int y)
{
#ifdef ZX_FAST_BIT_STORAGE
auto& v =get(y * _width + x);
v = !v;
Expand All @@ -173,7 +181,8 @@ class BitMatrix
#endif
}

void flipAll() {
void flipAll()
{
for (auto& i : _bits) {
i = ~i;
}
Expand All @@ -182,9 +191,7 @@ class BitMatrix
/**
* Clears all bits (sets to false).
*/
void clear() {
std::fill(_bits.begin(), _bits.end(), 0);
}
void clear() { std::fill(_bits.begin(), _bits.end(), 0); }

/**
* <p>Sets a square region of the bit matrix to true.</p>
Expand Down Expand Up @@ -241,27 +248,19 @@ class BitMatrix
/**
* @return The width of the matrix
*/
int width() const {
return _width;
}
int width() const { return _width; }

/**
* @return The height of the matrix
*/
int height() const {
return _height;
}
int height() const { return _height; }

/**
* @return The row size of the matrix. That is the number of 32-bits blocks that one row takes.
*/
int rowSize() const {
return _rowSize;
}
int rowSize() const { return _rowSize; }

bool empty() const {
return _bits.empty();
}
bool empty() const { return _bits.empty(); }

friend bool operator==(const BitMatrix& a, const BitMatrix& b)
{
Expand Down
2 changes: 1 addition & 1 deletion core/src/BitMatrixCursor.h
Expand Up @@ -21,7 +21,7 @@

namespace ZXing {

enum class Direction {LEFT = -1, RIGHT = 1};
enum class Direction { LEFT = -1, RIGHT = 1 };

inline Direction opposite(Direction dir) noexcept
{
Expand Down

0 comments on commit bec670d

Please sign in to comment.