|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html |
| 4 | + |
| 5 | +#ifndef OPENCV_CONTOURS_BLOCKSTORAGE_HPP |
| 6 | +#define OPENCV_CONTOURS_BLOCKSTORAGE_HPP |
| 7 | + |
| 8 | +#include "precomp.hpp" |
| 9 | + |
| 10 | +#include <array> |
| 11 | + |
| 12 | +namespace cv { |
| 13 | + |
| 14 | +// BLOCK_SIZE_ELEM - number of elements in a block |
| 15 | +// STATIC_CAPACITY_BYTES - static memory in bytes for preallocated blocks |
| 16 | +template <typename T, size_t BLOCK_SIZE_ELEM = 1024, size_t STATIC_CAPACITY_BYTES = 4096> |
| 17 | +class BlockStorage { |
| 18 | + public: |
| 19 | + using value_type = T; |
| 20 | + typedef struct {value_type data[BLOCK_SIZE_ELEM];} block_type; |
| 21 | + |
| 22 | + BlockStorage() |
| 23 | + { |
| 24 | + const size_t minDynamicBlocks = !staticBlocksCount ? 1 : 0; |
| 25 | + for(size_t i = 0 ; i<minDynamicBlocks ; ++i) |
| 26 | + dynamicBlocks.push_back(new block_type); |
| 27 | + } |
| 28 | + BlockStorage(const BlockStorage&) = delete; |
| 29 | + BlockStorage(BlockStorage&&) noexcept = default; |
| 30 | + ~BlockStorage() { |
| 31 | + for(const auto & block : dynamicBlocks) { |
| 32 | + delete block; |
| 33 | + } |
| 34 | + } |
| 35 | + BlockStorage& operator=(const BlockStorage&) = delete; |
| 36 | + BlockStorage& operator=(BlockStorage&&) noexcept = default; |
| 37 | + |
| 38 | + void clear(void) { |
| 39 | + const size_t minDynamicBlocks = !staticBlocksCount ? 1 : 0; |
| 40 | + for(size_t i = minDynamicBlocks, count = dynamicBlocks.size() ; i<count ; ++i ) { |
| 41 | + delete dynamicBlocks[i]; |
| 42 | + } |
| 43 | + dynamicBlocks.resize(minDynamicBlocks); |
| 44 | + sz = 0; |
| 45 | + } |
| 46 | + |
| 47 | + void push_back(const value_type& value) { |
| 48 | + const size_t blockIndex = sz / BLOCK_SIZE_ELEM; |
| 49 | + const size_t currentBlocksCount = staticBlocksCount+dynamicBlocks.size(); |
| 50 | + if (blockIndex == currentBlocksCount) |
| 51 | + dynamicBlocks.push_back(new block_type); |
| 52 | + block_type& cur_block = |
| 53 | + (blockIndex < staticBlocksCount) ? staticBlocks[blockIndex] : |
| 54 | + *dynamicBlocks[blockIndex-staticBlocksCount]; |
| 55 | + cur_block.data[sz % BLOCK_SIZE_ELEM] = value; |
| 56 | + ++sz; |
| 57 | + } |
| 58 | + |
| 59 | + size_t size() const { return sz; } |
| 60 | + |
| 61 | + const value_type& at(size_t index) const { |
| 62 | + const size_t blockIndex = index / BLOCK_SIZE_ELEM; |
| 63 | + const block_type& cur_block = |
| 64 | + (blockIndex < staticBlocksCount) ? staticBlocks[blockIndex] : |
| 65 | + *dynamicBlocks[blockIndex-staticBlocksCount]; |
| 66 | + return cur_block.data[index % BLOCK_SIZE_ELEM]; |
| 67 | + } |
| 68 | + value_type& at(size_t index) { |
| 69 | + const size_t blockIndex = index / BLOCK_SIZE_ELEM; |
| 70 | + block_type& cur_block = |
| 71 | + (blockIndex < staticBlocksCount) ? staticBlocks[blockIndex] : |
| 72 | + *dynamicBlocks[blockIndex-staticBlocksCount]; |
| 73 | + return cur_block.data[index % BLOCK_SIZE_ELEM]; |
| 74 | + } |
| 75 | + const value_type& operator[](size_t index) const {return at(index);} |
| 76 | + value_type& operator[](size_t index) {return at(index);} |
| 77 | + public: |
| 78 | + friend class RangeIterator; |
| 79 | + class RangeIterator |
| 80 | + { |
| 81 | + public: |
| 82 | + RangeIterator(const BlockStorage* _owner, size_t _first, size_t _last) |
| 83 | + :owner(_owner),remaining(_last-_first), |
| 84 | + blockIndex(_first/BLOCK_SIZE_ELEM),offset(_first%BLOCK_SIZE_ELEM) { |
| 85 | + } |
| 86 | + private: |
| 87 | + const BlockStorage* owner = nullptr; |
| 88 | + size_t remaining = 0; |
| 89 | + size_t blockIndex = 0; |
| 90 | + size_t offset = 0; |
| 91 | + public: |
| 92 | + bool done(void) const {return !remaining;} |
| 93 | + std::pair<const value_type*, size_t> operator*(void) const {return get();} |
| 94 | + std::pair<const value_type*, size_t> get(void) const { |
| 95 | + const block_type& cur_block = |
| 96 | + (blockIndex < owner->staticBlocksCount) ? owner->staticBlocks[blockIndex] : |
| 97 | + *owner->dynamicBlocks[blockIndex-owner->staticBlocksCount]; |
| 98 | + const value_type* rangeStart = cur_block.data+offset; |
| 99 | + const size_t rangeLength = std::min(remaining, BLOCK_SIZE_ELEM-offset); |
| 100 | + return std::make_pair(rangeStart, rangeLength); |
| 101 | + } |
| 102 | + RangeIterator& operator++() { |
| 103 | + std::pair<const value_type*, size_t> range = get(); |
| 104 | + remaining -= range.second; |
| 105 | + offset = 0; |
| 106 | + ++blockIndex; |
| 107 | + return *this; |
| 108 | + } |
| 109 | + }; |
| 110 | + RangeIterator getRangeIterator(size_t first, size_t last) const { |
| 111 | + return RangeIterator(this, first, last); |
| 112 | + } |
| 113 | + private: |
| 114 | + std::array<block_type, STATIC_CAPACITY_BYTES/(BLOCK_SIZE_ELEM*sizeof(value_type))> staticBlocks; |
| 115 | + const size_t staticBlocksCount = STATIC_CAPACITY_BYTES/(BLOCK_SIZE_ELEM*sizeof(value_type)); |
| 116 | + std::vector<block_type*> dynamicBlocks; |
| 117 | + size_t sz = 0; |
| 118 | +}; |
| 119 | + |
| 120 | +} // namespace cv |
| 121 | + |
| 122 | +#endif // OPENCV_CONTOURS_BLOCKSTORAGE_HPP |
0 commit comments