Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BLIMP_NET_DELTA_ENCODING_H_ | |
| 6 #define BLIMP_NET_DELTA_ENCODING_H_ | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <functional> | |
| 10 #include <utility> | |
| 11 | |
| 12 // This file contains utility functions to concisely compute and apply | |
| 13 // delta encodings of arbitrarily-typed sequences of values. Delta-encoded | |
| 14 // numeric values tend to be smaller in magnitude than their source values | |
| 15 // which, when combined with variable integer encoding (a la protobuf), results | |
| 16 // in a smaller wire size. | |
| 17 // | |
| 18 // e.g. DeltaEncode([4, 1, 5]) => [4, -3, 4] | |
| 19 // DeltaDecode([4, -3, 4]) => [4, 1, 5] | |
| 20 // | |
| 21 // If ordering doesn't matter, then the function SortAndDeltaEncode() | |
| 22 // can be used to compute a delta encoding with the smallest possible values. | |
| 23 // | |
| 24 // e.g. SortAndDeltaEncode([4, 1, 5]) => [1, 3, 1] | |
| 25 // DeltaDecode([1, 3, 1]) => [1, 4, 5] | |
| 26 // | |
| 27 // Delta encodings can also be computed for structured or non-numeric data | |
| 28 // types as well, so long as functions are provided for differencing and | |
| 29 // reconstituting the data. This can also be applied to delta encode or elide | |
| 30 // across fields of a struct, for example. | |
| 31 | |
| 32 namespace blimp { | |
| 33 | |
| 34 template <typename T> | |
| 35 inline T minus(const T& lhs, const T& rhs) { | |
| 36 return lhs - rhs; | |
| 37 } | |
| 38 | |
| 39 template <typename T> | |
| 40 inline T plus(const T& lhs, const T& rhs) { | |
| 41 return lhs + rhs; | |
| 42 } | |
| 43 | |
| 44 template <typename T> | |
| 45 inline bool less(const T& lhs, const T& rhs) { | |
|
Wez
2016/09/02 20:01:09
nit: this would be lessthan, I think.
But, again,
Kevin M
2016/09/02 21:12:44
Done.
| |
| 46 return lhs < rhs; | |
| 47 } | |
| 48 | |
| 49 // Homogeneously-typed signature for functions that compute and apply deltas. | |
| 50 template <typename T> | |
| 51 using BinaryFunction = T (*)(const T&, const T&); | |
| 52 | |
| 53 // Delta-encodes a iterated sequence of values in-place. | |
| 54 // |begin|: An iterator pointing to the beginning of the sequence. | |
| 55 // |end|: An iterator pointing to the end of the sequence. | |
| 56 // |compute_delta_fn|: Optional function which computes the delta between | |
| 57 // every element and its next successor. | |
| 58 template <typename I> | |
| 59 void DeltaEncode(I begin, | |
| 60 I end, | |
| 61 BinaryFunction<typename std::iterator_traits<I>::value_type> | |
| 62 compute_delta_fn = &minus) { | |
| 63 if (begin == end) { | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 I current = begin; | |
| 68 typename std::iterator_traits<I>::value_type value = *current; | |
| 69 ++current; | |
| 70 for (; current != end; ++current) { | |
| 71 value = (*compute_delta_fn)(*current, value); | |
| 72 | |
| 73 // Put the delta in place and store the actual value into |value|, ready for | |
| 74 // the next iteration. | |
| 75 std::swap(*current, value); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 // Delta-decodes a iterated sequence of values in-place. | |
| 80 // |begin|: An iterator pointing to the beginning of the sequence. | |
| 81 // |end|: An iterator pointing to the end of the sequence. | |
| 82 // |apply_fn|: Optional binary function which combines the value of every | |
| 83 // element with that of its immediate predecessor. | |
| 84 template <typename I> | |
| 85 void DeltaDecode(I begin, | |
| 86 I end, | |
| 87 BinaryFunction<typename std::iterator_traits<I>::value_type> | |
| 88 apply_fn = &plus) { | |
| 89 if (begin == end) { | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 I current = begin; | |
| 94 typename std::iterator_traits<I>::value_type previous_value = *current; | |
| 95 ++current; | |
| 96 for (; current != end; ++current) { | |
| 97 *current = apply_fn(previous_value, *current); | |
| 98 previous_value = *current; | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 // Convenience function for sorting and delta-encoding a sequence of values. | |
| 103 // The value type must support std::sort(), either by natively supporting | |
| 104 // inequality comparison or with custom comparison function |comparison_fn|. | |
| 105 template <typename I> | |
| 106 inline void SortAndDeltaEncode( | |
| 107 I begin, | |
| 108 I end, | |
| 109 BinaryFunction<typename std::iterator_traits<I>::value_type> | |
| 110 compute_delta_fn = &minus, | |
| 111 bool (*comparison_fn)(const typename std::iterator_traits<I>::value_type&, | |
| 112 const typename std::iterator_traits<I>::value_type&) = | |
| 113 less<typename std::iterator_traits<I>::value_type>) { | |
| 114 std::sort(begin, end, comparison_fn); | |
| 115 DeltaEncode(begin, end, compute_delta_fn); | |
| 116 } | |
| 117 | |
| 118 } // namespace blimp | |
| 119 | |
| 120 #endif // BLIMP_NET_DELTA_ENCODING_H_ | |
| OLD | NEW |