2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_DETAIL_GROUPVARINTDETAIL_H_
18 #define FOLLY_DETAIL_GROUPVARINTDETAIL_H_
30 struct GroupVarintTraits;
33 struct GroupVarintTraits<uint32_t> {
41 struct GroupVarintTraits<uint64_t> {
49 class GroupVarintBase {
51 typedef GroupVarintTraits<T> Traits;
52 enum { kHeaderSize = Traits::kHeaderSize };
58 * Number of integers encoded / decoded in one pass.
60 enum { kGroupSize = Traits::kGroupSize };
63 * Maximum encoded size.
65 enum { kMaxSize = kHeaderSize + sizeof(type) * kGroupSize };
68 * Maximum size for n values.
70 static size_t maxSize(size_t n) {
72 size_t total = (n / kGroupSize) * kFullGroupSize;
73 // Incomplete last group, if any
76 total += kHeaderSize + n * sizeof(type);
82 * Size of n values starting at p.
84 static size_t totalSize(const T* p, size_t n) {
86 for (; n >= kGroupSize; n -= kGroupSize, p += kGroupSize) {
87 size += Derived::size(p);
90 size += Derived::partialSize(p, n);
96 typedef GroupVarint<T> Derived;
97 enum { kFullGroupSize = kHeaderSize + kGroupSize * sizeof(type) };
100 } // namespace detail
103 #endif /* FOLLY_DETAIL_GROUPVARINTDETAIL_H_ */