|
| 1 | +// Copyright 2010-2015, Google Inc. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Redistribution and use in source and binary forms, with or without |
| 5 | +// modification, are permitted provided that the following conditions are |
| 6 | +// met: |
| 7 | +// |
| 8 | +// * Redistributions of source code must retain the above copyright |
| 9 | +// notice, this list of conditions and the following disclaimer. |
| 10 | +// * Redistributions in binary form must reproduce the above |
| 11 | +// copyright notice, this list of conditions and the following disclaimer |
| 12 | +// in the documentation and/or other materials provided with the |
| 13 | +// distribution. |
| 14 | +// * Neither the name of Google Inc. nor the names of its |
| 15 | +// contributors may be used to endorse or promote products derived from |
| 16 | +// this software without specific prior written permission. |
| 17 | +// |
| 18 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + |
| 30 | +#include "storage/louds/louds.h" |
| 31 | + |
| 32 | +namespace mozc { |
| 33 | +namespace storage { |
| 34 | +namespace louds { |
| 35 | + |
| 36 | +Louds::Louds() : select0_cache_size_(0), select1_cache_size_(0) {} |
| 37 | + |
| 38 | +Louds::~Louds() {} |
| 39 | + |
| 40 | +void Louds::Init(const uint8 *image, int length, |
| 41 | + size_t select0_cache_size, size_t select1_cache_size) { |
| 42 | + index_.Init(image, length); |
| 43 | + |
| 44 | + // Cap the cache sizes. |
| 45 | + if (select0_cache_size > index_.GetNum0Bits()) { |
| 46 | + select0_cache_size = index_.GetNum0Bits(); |
| 47 | + } |
| 48 | + if (select1_cache_size > index_.GetNum1Bits()) { |
| 49 | + select1_cache_size = index_.GetNum1Bits(); |
| 50 | + } |
| 51 | + |
| 52 | + // Initialize Select0 and Select1 cache for speed. In LOUDS traversal, nodes |
| 53 | + // close to the root are frequently accessed. Thus, we precompute select0 and |
| 54 | + // select1 values for such nodes. Since node IDs are assigned in BFS order, |
| 55 | + // the nodes close to the root are assigned smaller IDs. Hence, a simple |
| 56 | + // array can be used for the mapping from ID to cached value. |
| 57 | + select0_cache_size_ = select0_cache_size; |
| 58 | + select1_cache_size_ = select1_cache_size; |
| 59 | + const size_t cache_size = select0_cache_size + select1_cache_size; |
| 60 | + if (cache_size == 0) { |
| 61 | + return; |
| 62 | + } |
| 63 | + select_cache_.reset(new int[cache_size]); |
| 64 | + |
| 65 | + if (select0_cache_size > 0) { |
| 66 | + // Precompute Select0(i) + 1 for i in (0, select0_cache_size). |
| 67 | + select_cache_[0] = 0; |
| 68 | + for (size_t i = 1; i < select0_cache_size; ++i) { |
| 69 | + select_cache_[i] = index_.Select0(i) + 1; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + if (select1_cache_size > 0) { |
| 74 | + // Precompute Select1(i) for i in (0, select1_cache_size). |
| 75 | + select1_cache_ptr_ = select_cache_.get() + select0_cache_size; |
| 76 | + select1_cache_ptr_[0] = 0; |
| 77 | + for (size_t i = 1; i < select1_cache_size; ++i) { |
| 78 | + select1_cache_ptr_[i] = index_.Select1(i); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void Louds::Reset() { |
| 84 | + index_.Reset(); |
| 85 | + select_cache_.reset(nullptr); |
| 86 | + select0_cache_size_ = 0; |
| 87 | + select1_cache_size_ = 0; |
| 88 | +} |
| 89 | + |
| 90 | +} // namespace louds |
| 91 | +} // namespace storage |
| 92 | +} // namespace mozc |
0 commit comments