OpenMS
MSNumpress.h
Go to the documentation of this file.
1 /*
2  MSNumpress.hpp
3  johan.teleman@immun.lth.se
4 
5  This distribution goes under the BSD 3-clause license. If you prefer to use Apache
6  version 2.0, that is also available at https://github.com/fickludd/ms-numpress
7  Copyright (c) 2013, Johan Teleman
8  All rights reserved.
9 
10  Redistribution and use in source and binary forms, with or without modification,
11  are permitted provided that the following conditions are met:
12 
13 * Redistributions of source code must retain the above copyright notice, this list
14  of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice, this
16  list of conditions and the following disclaimer in the documentation and/or other
17  materials provided with the distribution.
18 * Neither the name of the Lund University nor the names of its contributors may be
19  used to endorse or promote products derived from this software without specific
20  prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25  SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33  ==================== encodeInt ====================
34  Some of the encodings described below use a integer compression referred to simply as
35 
36  encodeInt()
37 
38  The algorithm is similar to other variable length integer encodings,
39  such as the SQLite Variable-Length Integers encoding, but it uses half
40  bytes in its encoding procedure.
41 
42  This encoding works on a 4 byte integer, by truncating initial zeros or ones.
43  If the initial (most significant) half byte is 0x0 or 0xf, the number of such
44  halfbytes starting from the most significant is stored in a halfbyte. This initial
45  count is then followed by the rest of the ints halfbytes, in little-endian order.
46  A count halfbyte c of
47 
48  0 <= c <= 8 is interpreted as an initial c 0x0 halfbytes
49  9 <= c <= 15 is interpreted as an initial (c-8) 0xf halfbytes
50 
51  Example:
52 
53  int c rest
54  0 => 0x8
55  -1 => 0xf 0xf
56  2 => 0x7 0x2
57  23 => 0x6 0x7 0x1
58  2047 => 0x5 0xf 0xf 0xf
59 
60  Note that the algorithm returns a char array in which the half bytes are
61  stored in the lower 4 bits of each element. Since the first element is a
62  count half byte, the maximal length of the encoded data is 9 half bytes
63  (1 count half byte + 8 half bytes for a 4-byte integer).
64 
65  */
66 
67 #pragma once
68 
69 #include <cstddef>
70 #include <vector>
71 
72 // defines whether to throw an exception when a number cannot be encoded safely
73 // with the given parameters
74 #ifndef MS_NUMPRESS_THROW_ON_OVERFLOW
75 #define MS_NUMPRESS_THROW_ON_OVERFLOW true
76 #endif
77 
78 namespace ms {
79 namespace numpress {
80 
81 namespace MSNumpress {
82 
92  const double *data,
93  size_t dataSize);
94 
110  const double *data,
111  size_t dataSize,
112  double mass_acc);
113 
134  size_t encodeLinear(
135  const double *data,
136  const size_t dataSize,
137  unsigned char *result,
138  double fixedPoint);
139 
148  const std::vector<double> &data,
149  std::vector<unsigned char> &result,
150  double fixedPoint);
151 
166  size_t decodeLinear(
167  const unsigned char *data,
168  const size_t dataSize,
169  double *result);
170 
182  const std::vector<unsigned char> &data,
183  std::vector<double> &result);
184 
186 
187 
200  size_t encodeSafe(
201  const double *data,
202  const size_t dataSize,
203  unsigned char *result);
204 
205 
218  size_t decodeSafe(
219  const unsigned char *data,
220  const size_t dataSize,
221  double *result);
222 
224 
238  size_t encodePic(
239  const double *data,
240  const size_t dataSize,
241  unsigned char *result);
242 
249  void encodePic(
250  const std::vector<double> &data,
251  std::vector<unsigned char> &result);
252 
267  size_t decodePic(
268  const unsigned char *data,
269  const size_t dataSize,
270  double *result);
271 
282  void decodePic(
283  const std::vector<unsigned char> &data,
284  std::vector<double> &result);
285 
287 
288 
290  const double *data,
291  size_t dataSize);
292 
307  size_t encodeSlof(
308  const double *data,
309  const size_t dataSize,
310  unsigned char *result,
311  double fixedPoint);
312 
321  const std::vector<double> &data,
322  std::vector<unsigned char> &result,
323  double fixedPoint);
324 
337  size_t decodeSlof(
338  const unsigned char *data,
339  const size_t dataSize,
340  double *result);
341 
351  const std::vector<unsigned char> &data,
352  std::vector<double> &result);
353 
354 } // namespace MSNumpress
355 } // namespace msdata
356 } // namespace pwiz
357 
size_t encodePic(const double *data, const size_t dataSize, unsigned char *result)
size_t encodeSlof(const double *data, const size_t dataSize, unsigned char *result, double fixedPoint)
double optimalLinearFixedPoint(const double *data, size_t dataSize)
size_t decodeLinear(const unsigned char *data, const size_t dataSize, double *result)
double optimalSlofFixedPoint(const double *data, size_t dataSize)
size_t decodeSlof(const unsigned char *data, const size_t dataSize, double *result)
size_t encodeLinear(const double *data, const size_t dataSize, unsigned char *result, double fixedPoint)
size_t encodeSafe(const double *data, const size_t dataSize, unsigned char *result)
size_t decodeSafe(const unsigned char *data, const size_t dataSize, double *result)
size_t decodePic(const unsigned char *data, const size_t dataSize, double *result)
double optimalLinearFixedPointMass(const double *data, size_t dataSize, double mass_acc)
Definition: MSNumpress.h:78