]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/dictBuilder/zdict.h
import zstd 1.3.7
[FreeBSD/FreeBSD.git] / lib / dictBuilder / zdict.h
1 /*
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10
11 #ifndef DICTBUILDER_H_001
12 #define DICTBUILDER_H_001
13
14 #if defined (__cplusplus)
15 extern "C" {
16 #endif
17
18
19 /*======  Dependencies  ======*/
20 #include <stddef.h>  /* size_t */
21
22
23 /* =====   ZDICTLIB_API : control library symbols visibility   ===== */
24 #ifndef ZDICTLIB_VISIBILITY
25 #  if defined(__GNUC__) && (__GNUC__ >= 4)
26 #    define ZDICTLIB_VISIBILITY __attribute__ ((visibility ("default")))
27 #  else
28 #    define ZDICTLIB_VISIBILITY
29 #  endif
30 #endif
31 #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
32 #  define ZDICTLIB_API __declspec(dllexport) ZDICTLIB_VISIBILITY
33 #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
34 #  define ZDICTLIB_API __declspec(dllimport) ZDICTLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
35 #else
36 #  define ZDICTLIB_API ZDICTLIB_VISIBILITY
37 #endif
38
39
40 /*! ZDICT_trainFromBuffer():
41  *  Train a dictionary from an array of samples.
42  *  Redirect towards ZDICT_optimizeTrainFromBuffer_fastCover() single-threaded, with d=8, steps=4,
43  *  f=20, and accel=1.
44  *  Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
45  *  supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
46  *  The resulting dictionary will be saved into `dictBuffer`.
47  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
48  *          or an error code, which can be tested with ZDICT_isError().
49  *  Note: ZDICT_trainFromBuffer() requires about 9 bytes of memory for each input byte.
50  *  Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
51  *        It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
52  *        In general, it's recommended to provide a few thousands samples, though this can vary a lot.
53  *        It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
54  */
55 ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity,
56                                     const void* samplesBuffer,
57                                     const size_t* samplesSizes, unsigned nbSamples);
58
59
60 /*======   Helper functions   ======*/
61 ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize);  /**< extracts dictID; @return zero if error (not a valid dictionary) */
62 ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode);
63 ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode);
64
65
66
67 #ifdef ZDICT_STATIC_LINKING_ONLY
68
69 /* ====================================================================================
70  * The definitions in this section are considered experimental.
71  * They should never be used with a dynamic library, as they may change in the future.
72  * They are provided for advanced usages.
73  * Use them only in association with static linking.
74  * ==================================================================================== */
75
76 typedef struct {
77     int      compressionLevel;   /* optimize for a specific zstd compression level; 0 means default */
78     unsigned notificationLevel;  /* Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */
79     unsigned dictID;             /* force dictID value; 0 means auto mode (32-bits random value) */
80 } ZDICT_params_t;
81
82 /*! ZDICT_cover_params_t:
83  *  k and d are the only required parameters.
84  *  For others, value 0 means default.
85  */
86 typedef struct {
87     unsigned k;                  /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */
88     unsigned d;                  /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */
89     unsigned steps;              /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */
90     unsigned nbThreads;          /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */
91     double splitPoint;           /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (1.0), 1.0 when all samples are used for both training and testing */
92     ZDICT_params_t zParams;
93 } ZDICT_cover_params_t;
94
95 typedef struct {
96     unsigned k;                  /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */
97     unsigned d;                  /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */
98     unsigned f;                  /* log of size of frequency array : constraint: 0 < f <= 31 : 1 means default(20)*/
99     unsigned steps;              /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */
100     unsigned nbThreads;          /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */
101     double splitPoint;           /* Percentage of samples used for training: Only used for optimization : the first nbSamples * splitPoint samples will be used to training, the last nbSamples * (1 - splitPoint) samples will be used for testing, 0 means default (0.75), 1.0 when all samples are used for both training and testing */
102     unsigned accel;              /* Acceleration level: constraint: 0 < accel <= 10, higher means faster and less accurate, 0 means default(1) */
103     ZDICT_params_t zParams;
104 } ZDICT_fastCover_params_t;
105
106 /*! ZDICT_trainFromBuffer_cover():
107  *  Train a dictionary from an array of samples using the COVER algorithm.
108  *  Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
109  *  supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
110  *  The resulting dictionary will be saved into `dictBuffer`.
111  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
112  *          or an error code, which can be tested with ZDICT_isError().
113  *  Note: ZDICT_trainFromBuffer_cover() requires about 9 bytes of memory for each input byte.
114  *  Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
115  *        It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
116  *        In general, it's recommended to provide a few thousands samples, though this can vary a lot.
117  *        It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
118  */
119 ZDICTLIB_API size_t ZDICT_trainFromBuffer_cover(
120           void *dictBuffer, size_t dictBufferCapacity,
121     const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples,
122           ZDICT_cover_params_t parameters);
123
124 /*! ZDICT_optimizeTrainFromBuffer_cover():
125  * The same requirements as above hold for all the parameters except `parameters`.
126  * This function tries many parameter combinations and picks the best parameters.
127  * `*parameters` is filled with the best parameters found,
128  * dictionary constructed with those parameters is stored in `dictBuffer`.
129  *
130  * All of the parameters d, k, steps are optional.
131  * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}.
132  * if steps is zero it defaults to its default value.
133  * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000].
134  *
135  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
136  *           or an error code, which can be tested with ZDICT_isError().
137  *           On success `*parameters` contains the parameters selected.
138  * Note: ZDICT_optimizeTrainFromBuffer_cover() requires about 8 bytes of memory for each input byte and additionally another 5 bytes of memory for each byte of memory for each thread.
139  */
140 ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover(
141           void* dictBuffer, size_t dictBufferCapacity,
142     const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
143           ZDICT_cover_params_t* parameters);
144
145 /*! ZDICT_trainFromBuffer_fastCover():
146  *  Train a dictionary from an array of samples using a modified version of COVER algorithm.
147  *  Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
148  *  supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
149  *  d and k are required.
150  *  All other parameters are optional, will use default values if not provided
151  *  The resulting dictionary will be saved into `dictBuffer`.
152  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
153  *          or an error code, which can be tested with ZDICT_isError().
154  *  Note: ZDICT_trainFromBuffer_fastCover() requires about 1 bytes of memory for each input byte and additionally another 6 * 2^f bytes of memory .
155  *  Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
156  *        It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
157  *        In general, it's recommended to provide a few thousands samples, though this can vary a lot.
158  *        It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
159  */
160 ZDICTLIB_API size_t ZDICT_trainFromBuffer_fastCover(void *dictBuffer,
161                     size_t dictBufferCapacity, const void *samplesBuffer,
162                     const size_t *samplesSizes, unsigned nbSamples,
163                     ZDICT_fastCover_params_t parameters);
164
165 /*! ZDICT_optimizeTrainFromBuffer_fastCover():
166  * The same requirements as above hold for all the parameters except `parameters`.
167  * This function tries many parameter combinations (specifically, k and d combinations)
168  * and picks the best parameters. `*parameters` is filled with the best parameters found,
169  * dictionary constructed with those parameters is stored in `dictBuffer`.
170  * All of the parameters d, k, steps, f, and accel are optional.
171  * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}.
172  * if steps is zero it defaults to its default value.
173  * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000].
174  * If f is zero, default value of 20 is used.
175  * If accel is zero, default value of 1 is used.
176  *
177  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
178  *           or an error code, which can be tested with ZDICT_isError().
179  *           On success `*parameters` contains the parameters selected.
180  * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 1 byte of memory for each input byte and additionally another 6 * 2^f bytes of memory for each thread.
181  */
182 ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_fastCover(void* dictBuffer,
183                     size_t dictBufferCapacity, const void* samplesBuffer,
184                     const size_t* samplesSizes, unsigned nbSamples,
185                     ZDICT_fastCover_params_t* parameters);
186
187 /*! ZDICT_finalizeDictionary():
188  * Given a custom content as a basis for dictionary, and a set of samples,
189  * finalize dictionary by adding headers and statistics.
190  *
191  * Samples must be stored concatenated in a flat buffer `samplesBuffer`,
192  * supplied with an array of sizes `samplesSizes`, providing the size of each sample in order.
193  *
194  * dictContentSize must be >= ZDICT_CONTENTSIZE_MIN bytes.
195  * maxDictSize must be >= dictContentSize, and must be >= ZDICT_DICTSIZE_MIN bytes.
196  *
197  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`),
198  *           or an error code, which can be tested by ZDICT_isError().
199  * Note: ZDICT_finalizeDictionary() will push notifications into stderr if instructed to, using notificationLevel>0.
200  * Note 2: dictBuffer and dictContent can overlap
201  */
202 #define ZDICT_CONTENTSIZE_MIN 128
203 #define ZDICT_DICTSIZE_MIN    256
204 ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dictBuffer, size_t dictBufferCapacity,
205                                 const void* dictContent, size_t dictContentSize,
206                                 const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
207                                 ZDICT_params_t parameters);
208
209 typedef struct {
210     unsigned selectivityLevel;   /* 0 means default; larger => select more => larger dictionary */
211     ZDICT_params_t zParams;
212 } ZDICT_legacy_params_t;
213
214 /*! ZDICT_trainFromBuffer_legacy():
215  *  Train a dictionary from an array of samples.
216  *  Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
217  *  supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
218  *  The resulting dictionary will be saved into `dictBuffer`.
219  * `parameters` is optional and can be provided with values set to 0 to mean "default".
220  * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
221  *          or an error code, which can be tested with ZDICT_isError().
222  *  Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
223  *        It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.
224  *        In general, it's recommended to provide a few thousands samples, though this can vary a lot.
225  *        It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
226  *  Note: ZDICT_trainFromBuffer_legacy() will send notifications into stderr if instructed to, using notificationLevel>0.
227  */
228 ZDICTLIB_API size_t ZDICT_trainFromBuffer_legacy(
229     void *dictBuffer, size_t dictBufferCapacity,
230     const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples,
231     ZDICT_legacy_params_t parameters);
232
233 /* Deprecation warnings */
234 /* It is generally possible to disable deprecation warnings from compiler,
235    for example with -Wno-deprecated-declarations for gcc
236    or _CRT_SECURE_NO_WARNINGS in Visual.
237    Otherwise, it's also possible to manually define ZDICT_DISABLE_DEPRECATE_WARNINGS */
238 #ifdef ZDICT_DISABLE_DEPRECATE_WARNINGS
239 #  define ZDICT_DEPRECATED(message) ZDICTLIB_API   /* disable deprecation warnings */
240 #else
241 #  define ZDICT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
242 #  if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
243 #    define ZDICT_DEPRECATED(message) [[deprecated(message)]] ZDICTLIB_API
244 #  elif (ZDICT_GCC_VERSION >= 405) || defined(__clang__)
245 #    define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated(message)))
246 #  elif (ZDICT_GCC_VERSION >= 301)
247 #    define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated))
248 #  elif defined(_MSC_VER)
249 #    define ZDICT_DEPRECATED(message) ZDICTLIB_API __declspec(deprecated(message))
250 #  else
251 #    pragma message("WARNING: You need to implement ZDICT_DEPRECATED for this compiler")
252 #    define ZDICT_DEPRECATED(message) ZDICTLIB_API
253 #  endif
254 #endif /* ZDICT_DISABLE_DEPRECATE_WARNINGS */
255
256 ZDICT_DEPRECATED("use ZDICT_finalizeDictionary() instead")
257 size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
258                                   const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
259
260
261 #endif   /* ZDICT_STATIC_LINKING_ONLY */
262
263 #if defined (__cplusplus)
264 }
265 #endif
266
267 #endif   /* DICTBUILDER_H_001 */