Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: blimp/net/delta_encoding.h

Issue 2275763002: Blimp: add delta encoding family of functions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clean up includes Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
11 // This file contains utility functions to concisely compute and apply
12 // delta encodings of arbitrarily-typed sequences of values. Delta-encoded
13 // numeric values tend to be smaller in magnitude than their source values
14 // which, when combined with variable integer encoding (a la protobuf), results
15 // in a smaller wire size.
16 //
17 // e.g. DeltaEncode([4, 1, 5]) => [4, -3, 4]
18 // DeltaDecode([4, -3, 4]) => [4, 1, 5]
19 //
20 // If ordering doesn't matter, then the function SortAndDeltaEncode()
21 // can be used to compute a delta encoding with the smallest possible values.
22 //
23 // e.g. DeltaEncode([4, 1, 5]) => [1, 3, 1]
Wez 2016/08/29 23:25:23 nit: You mean SortAndDeltaEncode here?
Kevin M 2016/08/30 19:06:33 Done.
24 // DeltaDecode([1, 3, 1]) => [1, 4, 5]
25 //
26 // Delta encodings can also be computed for structured or non-numeric data
27 // types as well, so long as functions are provided for differencing and
28 // reconstituting the data. Some interesting things that can be done by these
Wez 2016/08/29 23:25:23 nit: Wording seems verbose. How about "This can al
Kevin M 2016/08/30 19:06:33 Done.
29 // functions include elision of redundant data or delta encoding subfields.
30 // See the unit test "DeltaEncodeStruct" for an example.
31
32 namespace blimp {
33
34 template <typename T>
35 inline T minus(const T& lhs, const T& rhs) {
Wez 2016/08/29 23:25:23 Suggest renaming this to GetDeltaBetween(from, to)
Kevin M 2016/08/30 19:06:33 I would prefer to constrain the types. It seems to
Wez 2016/09/02 20:01:09 OK, but can we rename it to something less generic
Kevin M 2016/09/02 21:12:44 Done: "minus" => "default_compute_delta" "plus"
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 using BinaryFunction = T (*)(const T&, const T&);
Wez 2016/08/29 23:25:23 nit: Type name is too generic - what's important i
Kevin M 2016/08/30 19:06:33 The same signature is used for computing and mergi
46
47 // Delta-encodes a iterated sequence of values in-place.
48 // |begin|: An iterator pointing to the beginning of the sequence.
49 // |end|: An iterator pointing to the end of the sequence.
50 // |compute_delta_fn|: Optional binary function which computes the delta between
51 // every element and its next successor.
Wez 2016/08/29 23:25:23 Why is that a run-time rather than a template para
Kevin M 2016/08/30 19:06:33 It's really clunky. The computed function signatur
52 template <typename Iterator,
53 typename Type = typename std::iterator_traits<Iterator>::value_type>
54 void DeltaEncode(Iterator begin,
55 Iterator end,
56 BinaryFunction<Type> compute_delta_fn = &minus<Type>) {
57 if (begin == end) {
58 return;
59 }
60
61 Iterator current = begin;
62 auto current_value = *current;
Wez 2016/08/29 23:25:23 nit: Don't use auto here, or below, since the type
Kevin M 2016/08/30 19:06:33 Done.
63 ++current;
64 for (; current != end; ++current) {
65 auto next_value = *current;
66 *current = (*compute_delta_fn)(next_value, current_value);
67 current_value = next_value;
68 }
Wez 2016/08/29 23:25:23 Naming of the intermediates makes this harder to r
Kevin M 2016/08/30 19:06:33 Done.
69 }
70
71 // Delta-decodes a iterated sequence of values in-place.
72 // |begin|: An iterator pointing to the beginning of the sequence.
73 // |end|: An iterator pointing to the end of the sequence.
74 // |merge_fn|: Optional binary function which combines the value of every
75 // element with that of its immediate predecessor.
76 template <typename Iterator,
77 typename Type = typename std::iterator_traits<Iterator>::value_type>
78 void DeltaDecode(Iterator begin,
79 Iterator end,
80 BinaryFunction<Type> merge_fn = &plus<Type>) {
81 if (begin == end) {
82 return;
83 }
84
85 Iterator current = begin;
86 auto current_value = *current;
Wez 2016/08/29 23:25:23 Again, this naming is confusing, since by the time
Kevin M 2016/08/30 19:06:33 Done.
87 ++current;
88 for (; current != end; ++current) {
89 *current = (*merge_fn)(current_value, *current);
90 current_value = *current;
91 }
92 }
93
94 // Convenience function for sorting and delta-encoding a sequence of values.
95 // The value type must support less-than inequality comparisons (operator<).
Wez 2016/08/29 23:25:23 nit: Safer to simply state that it must support st
Kevin M 2016/08/30 19:06:33 Done.
96 template <typename Iterator,
97 typename Type = typename std::iterator_traits<Iterator>::value_type>
98 inline void SortAndDeltaEncode(
99 Iterator begin,
100 Iterator end,
101 BinaryFunction<Type> compute_delta_fn = &minus<Type>) {
102 std::sort(begin, end);
103 DeltaEncode(begin, end, compute_delta_fn);
104 }
105
106 } // namespace blimp
107
108 #endif // BLIMP_NET_DELTA_ENCODING_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698