OpenMS  2.7.0
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 
147  const std::vector<double> &data,
148  std::vector<unsigned char> &result,
149  double fixedPoint);
150 
165  size_t decodeLinear(
166  const unsigned char *data,
167  const size_t dataSize,
168  double *result);
169 
181  const std::vector<unsigned char> &data,
182  std::vector<double> &result);
183 
185 
186 
199  size_t encodeSafe(
200  const double *data,
201  const size_t dataSize,
202  unsigned char *result);
203 
204 
217  size_t decodeSafe(
218  const unsigned char *data,
219  const size_t dataSize,
220  double *result);
221 
223 
237  size_t encodePic(
238  const double *data,
239  const size_t dataSize,
240  unsigned char *result);
241 
248  void encodePic(
249  const std::vector<double> &data,
250  std::vector<unsigned char> &result);
251 
266  size_t decodePic(
267  const unsigned char *data,
268  const size_t dataSize,
269  double *result);
270 
281  void decodePic(
282  const std::vector<unsigned char> &data,
283  std::vector<double> &result);
284 
286 
287 
289  const double *data,
290  size_t dataSize);
291 
305  size_t encodeSlof(
306  const double *data,
307  const size_t dataSize,
308  unsigned char *result,
309  double fixedPoint);
310 
318  const std::vector<double> &data,
319  std::vector<unsigned char> &result,
320  double fixedPoint);
321 
334  size_t decodeSlof(
335  const unsigned char *data,
336  const size_t dataSize,
337  double *result);
338 
348  const std::vector<unsigned char> &data,
349  std::vector<double> &result);
350 
351 } // namespace MSNumpress
352 } // namespace msdata
353 } // namespace pwiz
354 
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