| 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 // Homogeneously-typed signature for functions that compute and apply deltas. | |
| 35 template <typename T> | |
| 36 using BinaryFunction = T (*)(const T&, const T&); | |
| 37 | |
| 38 // Uses the subtraction operator to compute the delta across two values. | |
| 39 template <typename T> | |
| 40 inline T default_compute_delta(const T& lhs, const T& rhs) { | |
| 41 return lhs - rhs; | |
| 42 } | |
| 43 | |
| 44 // Uses the addition operator to add a delta value to a base value. | |
| 45 template <typename T> | |
| 46 inline T default_apply_delta(const T& lhs, const T& rhs) { | |
| 47 return lhs + rhs; | |
| 48 } | |
| 49 | |
| 50 // Uses the less-than (<) operator to determine if |lhs| is less-than |rhs|. | |
| 51 template <typename T> | |
| 52 inline bool lessthan(const T& lhs, const T& rhs) { | |
| 53 return lhs < rhs; | |
| 54 } | |
| 55 | |
| 56 // Delta-encodes a iterated sequence of values in-place. | |
| 57 // |begin|: An iterator pointing to the beginning of the sequence. | |
| 58 // |end|: An iterator pointing to the end of the sequence. | |
| 59 // |default_compute_delta_fn|: Optional function which computes the delta | |
| 60 // between | |
| 61 // every element and its next successor. | |
| 62 template <typename I> | |
| 63 void DeltaEncode(I begin, | |
| 64 I end, | |
| 65 BinaryFunction<typename std::iterator_traits<I>::value_type> | |
| 66 default_compute_delta_fn = &default_compute_delta) { | |
| 67 if (begin == end) { | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 I current = begin; | |
| 72 typename std::iterator_traits<I>::value_type value = *current; | |
| 73 ++current; | |
| 74 for (; current != end; ++current) { | |
| 75 value = (*default_compute_delta_fn)(*current, value); | |
| 76 | |
| 77 // Put the delta in place and store the actual value into |value|, ready for | |
| 78 // the next iteration. | |
| 79 std::swap(*current, value); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 // Delta-decodes a iterated sequence of values in-place. | |
| 84 // |begin|: An iterator pointing to the beginning of the sequence. | |
| 85 // |end|: An iterator pointing to the end of the sequence. | |
| 86 // |apply_fn|: Optional binary function which combines the value of | |
| 87 // every element with that of its immediate predecessor. | |
| 88 template <typename I> | |
| 89 void DeltaDecode(I begin, | |
| 90 I end, | |
| 91 BinaryFunction<typename std::iterator_traits<I>::value_type> | |
| 92 apply_fn = &default_apply_delta) { | |
| 93 if (begin == end) { | |
| 94 return; | |
| 95 } | |
| 96 | |
| 97 I current = begin; | |
| 98 typename std::iterator_traits<I>::value_type previous_value = *current; | |
| 99 ++current; | |
| 100 for (; current != end; ++current) { | |
| 101 *current = apply_fn(previous_value, *current); | |
| 102 previous_value = *current; | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 // Convenience function for sorting and delta-encoding a sequence of values. | |
| 107 // The value type must support std::sort(), either by natively supporting | |
| 108 // inequality comparison or with custom comparison function |comparison_fn|. | |
| 109 template <typename I> | |
| 110 inline void SortAndDeltaEncode( | |
| 111 I begin, | |
| 112 I end, | |
| 113 BinaryFunction<typename std::iterator_traits<I>::value_type> | |
| 114 default_compute_delta_fn = &default_compute_delta, | |
| 115 bool (*comparison_fn)(const typename std::iterator_traits<I>::value_type&, | |
| 116 const typename std::iterator_traits<I>::value_type&) = | |
| 117 lessthan<typename std::iterator_traits<I>::value_type>) { | |
| 118 std::sort(begin, end, comparison_fn); | |
| 119 DeltaEncode(begin, end, default_compute_delta_fn); | |
| 120 } | |
| 121 | |
| 122 } // namespace blimp | |
| 123 | |
| 124 #endif // BLIMP_NET_DELTA_ENCODING_H_ | |
| OLD | NEW |