| 1 |
/* |
| 2 |
* Copyright (C) 2018 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef TNT_FILAMAT_BLOBDICTIONARY_H |
| 18 |
#define TNT_FILAMAT_BLOBDICTIONARY_H |
| 19 |
|
| 20 |
#include <cassert> |
| 21 |
#include <string> |
| 22 |
#include <unordered_map> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
namespace filamat { |
| 26 |
|
| 27 |
// Establish a blob <-> id mapping. Note that std::string may binary data with null characters. |
| 28 |
class BlobDictionary { |
| 29 |
public: |
| 30 |
BlobDictionary() : mStorageSize(0) { |
| 31 |
} |
| 32 |
|
| 33 |
~BlobDictionary() = default; |
| 34 |
|
| 35 |
// Adds a blob if it's not already a duplicate and returns its index. |
| 36 |
size_t addBlob(const std::vector<uint32_t>& blob) noexcept; |
| 37 |
|
| 38 |
size_t getBlobCount() const noexcept { |
| 39 |
return mBlobs.size(); |
| 40 |
} |
| 41 |
|
| 42 |
// Returns the total storage size, assuming that each blob is prefixed with a 64-bit size. |
| 43 |
size_t getSize() const noexcept { |
| 44 |
return mStorageSize + 8 * getBlobCount(); |
| 45 |
} |
| 46 |
|
| 47 |
bool isEmpty() const noexcept { |
| 48 |
return mBlobs.size() == 0; |
| 49 |
} |
| 50 |
|
| 51 |
const std::string& getBlob(size_t index) const noexcept { |
| 52 |
assert(index < mBlobs.size()); |
| 53 |
return mBlobs[index]; |
| 54 |
} |
| 55 |
|
| 56 |
private: |
| 57 |
std::unordered_map<std::string, size_t> mBlobIndices; |
| 58 |
std::vector<std::string> mBlobs; |
| 59 |
size_t mStorageSize; |
| 60 |
}; |
| 61 |
|
| 62 |
} // namespace filamat |
| 63 |
|
| 64 |
#endif // TNT_FILAMAT_BLOBDICTIONARY_H |