2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
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.
11 #ifndef DICTBUILDER_H_001
12 #define DICTBUILDER_H_001
14 #if defined (__cplusplus)
19 /*====== Dependencies ======*/
20 #include <stddef.h> /* size_t */
23 /* ===== ZDICTLIB_API : control library symbols visibility ===== */
24 #ifndef ZDICTLIB_VISIBILITY
25 # if defined(__GNUC__) && (__GNUC__ >= 4)
26 # define ZDICTLIB_VISIBILITY __attribute__ ((visibility ("default")))
28 # define ZDICTLIB_VISIBILITY
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.*/
36 # define ZDICTLIB_API ZDICTLIB_VISIBILITY
40 /*! ZDICT_trainFromBuffer():
41 * Train a dictionary from an array of samples.
42 * Uses ZDICT_optimizeTrainFromBuffer_cover() single-threaded, with d=8 and steps=4.
43 * Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
44 * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
45 * The resulting dictionary will be saved into `dictBuffer`.
46 * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
47 * or an error code, which can be tested with ZDICT_isError().
48 * Note: ZDICT_trainFromBuffer() requires about 9 bytes of memory for each input byte.
49 * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
50 * It's obviously possible to target smaller or larger ones, just by specifying different `dictBufferCapacity`.
51 * In general, it's recommended to provide a few thousands samples, but this can vary a lot.
52 * It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
54 ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity,
55 const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
58 /*====== Helper functions ======*/
59 ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize); /**< extracts dictID; @return zero if error (not a valid dictionary) */
60 ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode);
61 ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode);
65 #ifdef ZDICT_STATIC_LINKING_ONLY
67 /* ====================================================================================
68 * The definitions in this section are considered experimental.
69 * They should never be used with a dynamic library, as they may change in the future.
70 * They are provided for advanced usages.
71 * Use them only in association with static linking.
72 * ==================================================================================== */
75 int compressionLevel; /* 0 means default; target a specific zstd compression level */
76 unsigned notificationLevel; /* Write to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */
77 unsigned dictID; /* 0 means auto mode (32-bits random value); other : force dictID value */
80 /*! ZDICT_cover_params_t:
81 * For all values 0 means default.
82 * k and d are the only required parameters.
85 unsigned k; /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */
86 unsigned d; /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */
87 unsigned steps; /* Number of steps : Only used for optimization : 0 means default (32) : Higher means more parameters checked */
88 unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */
89 ZDICT_params_t zParams;
90 } ZDICT_cover_params_t;
93 /*! ZDICT_trainFromBuffer_cover():
94 * Train a dictionary from an array of samples using the COVER algorithm.
95 * Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
96 * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
97 * The resulting dictionary will be saved into `dictBuffer`.
98 * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
99 * or an error code, which can be tested with ZDICT_isError().
100 * Note: ZDICT_trainFromBuffer_cover() requires about 9 bytes of memory for each input byte.
101 * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
102 * It's obviously possible to target smaller or larger ones, just by specifying different `dictBufferCapacity`.
103 * In general, it's recommended to provide a few thousands samples, but this can vary a lot.
104 * It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
106 ZDICTLIB_API size_t ZDICT_trainFromBuffer_cover(
107 void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer,
108 const size_t *samplesSizes, unsigned nbSamples,
109 ZDICT_cover_params_t parameters);
111 /*! ZDICT_optimizeTrainFromBuffer_cover():
112 * The same requirements as above hold for all the parameters except `parameters`.
113 * This function tries many parameter combinations and picks the best parameters.
114 * `*parameters` is filled with the best parameters found, and the dictionary
115 * constructed with those parameters is stored in `dictBuffer`.
117 * All of the parameters d, k, steps are optional.
118 * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8, 10, 12, 14, 16}.
119 * if steps is zero it defaults to its default value.
120 * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [16, 2048].
122 * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
123 * or an error code, which can be tested with ZDICT_isError().
124 * On success `*parameters` contains the parameters selected.
125 * 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.
127 ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover(
128 void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer,
129 const size_t *samplesSizes, unsigned nbSamples,
130 ZDICT_cover_params_t *parameters);
132 /*! ZDICT_finalizeDictionary():
133 * Given a custom content as a basis for dictionary, and a set of samples,
134 * finalize dictionary by adding headers and statistics.
136 * Samples must be stored concatenated in a flat buffer `samplesBuffer`,
137 * supplied with an array of sizes `samplesSizes`, providing the size of each sample in order.
139 * dictContentSize must be >= ZDICT_CONTENTSIZE_MIN bytes.
140 * maxDictSize must be >= dictContentSize, and must be >= ZDICT_DICTSIZE_MIN bytes.
142 * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`),
143 * or an error code, which can be tested by ZDICT_isError().
144 * Note: ZDICT_finalizeDictionary() will push notifications into stderr if instructed to, using notificationLevel>0.
145 * Note 2: dictBuffer and dictContent can overlap
147 #define ZDICT_CONTENTSIZE_MIN 128
148 #define ZDICT_DICTSIZE_MIN 256
149 ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dictBuffer, size_t dictBufferCapacity,
150 const void* dictContent, size_t dictContentSize,
151 const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples,
152 ZDICT_params_t parameters);
155 unsigned selectivityLevel; /* 0 means default; larger => select more => larger dictionary */
156 ZDICT_params_t zParams;
157 } ZDICT_legacy_params_t;
159 /*! ZDICT_trainFromBuffer_legacy():
160 * Train a dictionary from an array of samples.
161 * Samples must be stored concatenated in a single flat buffer `samplesBuffer`,
162 * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.
163 * The resulting dictionary will be saved into `dictBuffer`.
164 * `parameters` is optional and can be provided with values set to 0 to mean "default".
165 * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)
166 * or an error code, which can be tested with ZDICT_isError().
167 * Tips: In general, a reasonable dictionary has a size of ~ 100 KB.
168 * It's obviously possible to target smaller or larger ones, just by specifying different `dictBufferCapacity`.
169 * In general, it's recommended to provide a few thousands samples, but this can vary a lot.
170 * It's recommended that total size of all samples be about ~x100 times the target size of dictionary.
171 * Note: ZDICT_trainFromBuffer_legacy() will send notifications into stderr if instructed to, using notificationLevel>0.
173 ZDICTLIB_API size_t ZDICT_trainFromBuffer_legacy(
174 void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer,
175 const size_t *samplesSizes, unsigned nbSamples, ZDICT_legacy_params_t parameters);
177 /* Deprecation warnings */
178 /* It is generally possible to disable deprecation warnings from compiler,
179 for example with -Wno-deprecated-declarations for gcc
180 or _CRT_SECURE_NO_WARNINGS in Visual.
181 Otherwise, it's also possible to manually define ZDICT_DISABLE_DEPRECATE_WARNINGS */
182 #ifdef ZDICT_DISABLE_DEPRECATE_WARNINGS
183 # define ZDICT_DEPRECATED(message) ZDICTLIB_API /* disable deprecation warnings */
185 # define ZDICT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
186 # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */
187 # define ZDICT_DEPRECATED(message) [[deprecated(message)]] ZDICTLIB_API
188 # elif (ZDICT_GCC_VERSION >= 405) || defined(__clang__)
189 # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated(message)))
190 # elif (ZDICT_GCC_VERSION >= 301)
191 # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated))
192 # elif defined(_MSC_VER)
193 # define ZDICT_DEPRECATED(message) ZDICTLIB_API __declspec(deprecated(message))
195 # pragma message("WARNING: You need to implement ZDICT_DEPRECATED for this compiler")
196 # define ZDICT_DEPRECATED(message) ZDICTLIB_API
198 #endif /* ZDICT_DISABLE_DEPRECATE_WARNINGS */
200 ZDICT_DEPRECATED("use ZDICT_finalizeDictionary() instead")
201 size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity,
202 const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples);
205 #endif /* ZDICT_STATIC_LINKING_ONLY */
207 #if defined (__cplusplus)
211 #endif /* DICTBUILDER_H_001 */