improve profiled performance of C++ BitArray::get(). Closes issue 1101

From evansepdx. Thanks!

git-svn-id: https://zxing.googlecode.com/svn/trunk@2083 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
smparkes@smparkes.net 2011-12-12 22:54:51 +00:00
parent 1cd49dbcb1
commit a569f179ff
3 changed files with 18 additions and 38 deletions

View file

@ -1,8 +1,5 @@
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*- // -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
/* /*
* BitArray.cpp
* zxing
*
* Copyright 2010 ZXing authors. All rights reserved. * Copyright 2010 ZXing authors. All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -19,32 +16,14 @@
*/ */
#include <zxing/common/BitArray.h> #include <zxing/common/BitArray.h>
#include <iostream>
#include <limits>
using namespace std; using namespace std;
namespace zxing { namespace zxing {
static unsigned int logDigits(unsigned digits) {
unsigned log = 0;
unsigned val = 1;
while (val < digits) {
log++;
val <<= 1;
}
return log;
}
const unsigned int BitArray::bitsPerWord_ = numeric_limits<unsigned int>::digits;
const unsigned int BitArray::logBits_ = logDigits(bitsPerWord_);
const unsigned int BitArray::bitsMask_ = (1 << logBits_) - 1;
size_t BitArray::wordsForBits(size_t bits) { size_t BitArray::wordsForBits(size_t bits) {
int arraySize = bits >> logBits_; int arraySize = (bits + bitsPerWord_ - 1) >> logBits_;
if (bits - (arraySize << logBits_) != 0) {
arraySize++;
}
return arraySize; return arraySize;
} }

View file

@ -3,9 +3,6 @@
#define __BIT_ARRAY_H__ #define __BIT_ARRAY_H__
/* /*
* BitArray.h
* zxing
*
* Copyright 2010 ZXing authors. All rights reserved. * Copyright 2010 ZXing authors. All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -24,17 +21,26 @@
#include <zxing/common/Counted.h> #include <zxing/common/Counted.h>
#include <zxing/common/IllegalArgumentException.h> #include <zxing/common/IllegalArgumentException.h>
#include <vector> #include <vector>
#include <iostream> #include <limits>
namespace zxing { namespace zxing {
#define ZX_LOG_DIGITS(digits) \
((digits == 8) ? 3 : \
((digits == 16) ? 4 : \
((digits == 32) ? 5 : \
((digits == 64) ? 6 : \
((digits == 128) ? 7 : \
(-1))))))
class BitArray : public Counted { class BitArray : public Counted {
private: private:
size_t size_; size_t size_;
std::vector<unsigned int> bits_; std::vector<unsigned int> bits_;
static const unsigned int bitsPerWord_; static const unsigned int bitsPerWord_ =
static const unsigned int logBits_; std::numeric_limits<unsigned int>::digits;
static const unsigned int bitsMask_; static const unsigned int logBits_ = ZX_LOG_DIGITS(bitsPerWord_);
static const unsigned int bitsMask_ = (1 << logBits_) - 1;
static size_t wordsForBits(size_t bits); static size_t wordsForBits(size_t bits);
explicit BitArray(); explicit BitArray();

View file

@ -1,8 +1,5 @@
// -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*- // -*- mode:c++; tab-width:2; indent-tabs-mode:nil; c-basic-offset:2 -*-
/* /*
* BitMatrix.cpp
* zxing
*
* Copyright 2010 ZXing authors. All rights reserved. * Copyright 2010 ZXing authors. All rights reserved.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -35,26 +32,24 @@ using zxing::Ref;
namespace { namespace {
size_t wordsForSize(size_t width, size_t wordsForSize(size_t width,
size_t height, size_t height,
unsigned int bitsPerWord,
unsigned int logBits) { unsigned int logBits) {
size_t bits = width * height; size_t bits = width * height;
int arraySize = bits >> logBits; int arraySize = (bits + bitsPerWord - 1) >> logBits;
if (bits - (arraySize << logBits) != 0) {
arraySize++;
}
return arraySize; return arraySize;
} }
} }
BitMatrix::BitMatrix(size_t dimension) : BitMatrix::BitMatrix(size_t dimension) :
width_(dimension), height_(dimension), words_(0), bits_(NULL) { width_(dimension), height_(dimension), words_(0), bits_(NULL) {
words_ = wordsForSize(width_, height_, logBits); words_ = wordsForSize(width_, height_, bitsPerWord, logBits);
bits_ = new unsigned int[words_]; bits_ = new unsigned int[words_];
clear(); clear();
} }
BitMatrix::BitMatrix(size_t width, size_t height) : BitMatrix::BitMatrix(size_t width, size_t height) :
width_(width), height_(height), words_(0), bits_(NULL) { width_(width), height_(height), words_(0), bits_(NULL) {
words_ = wordsForSize(width_, height_, logBits); words_ = wordsForSize(width_, height_, bitsPerWord, logBits);
bits_ = new unsigned int[words_]; bits_ = new unsigned int[words_];
clear(); clear();
} }