William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 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. |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 42 | * Redirect towards ZDICT_optimizeTrainFromBuffer_fastCover() single-threaded, with d=8, steps=4, |
| 43 | * f=20, and accel=1. |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 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(). |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 49 | * Note: Dictionary training will fail if there are not enough samples to construct a |
| 50 | * dictionary, or if most of the samples are too small (< 8 bytes being the lower limit). |
| 51 | * If dictionary training fails, you should use zstd without a dictionary, as the dictionary |
| 52 | * would've been ineffective anyways. If you believe your samples would benefit from a dictionary |
| 53 | * please open an issue with details, and we can look into it. |
| 54 | * Note: ZDICT_trainFromBuffer()'s memory usage is about 6 MB. |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 55 | * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. |
| 56 | * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. |
| 57 | * In general, it's recommended to provide a few thousands samples, though this can vary a lot. |
| 58 | * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. |
| 59 | */ |
| 60 | ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCapacity, |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 61 | const void* samplesBuffer, |
| 62 | const size_t* samplesSizes, unsigned nbSamples); |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 63 | |
| 64 | |
| 65 | /*====== Helper functions ======*/ |
| 66 | ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize); /**< extracts dictID; @return zero if error (not a valid dictionary) */ |
| 67 | ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode); |
| 68 | ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode); |
| 69 | |
| 70 | |
| 71 | |
| 72 | #ifdef ZDICT_STATIC_LINKING_ONLY |
| 73 | |
| 74 | /* ==================================================================================== |
| 75 | * The definitions in this section are considered experimental. |
| 76 | * They should never be used with a dynamic library, as they may change in the future. |
| 77 | * They are provided for advanced usages. |
| 78 | * Use them only in association with static linking. |
| 79 | * ==================================================================================== */ |
| 80 | |
| 81 | typedef struct { |
| 82 | int compressionLevel; /* optimize for a specific zstd compression level; 0 means default */ |
| 83 | unsigned notificationLevel; /* Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug; */ |
| 84 | unsigned dictID; /* force dictID value; 0 means auto mode (32-bits random value) */ |
| 85 | } ZDICT_params_t; |
| 86 | |
| 87 | /*! ZDICT_cover_params_t: |
| 88 | * k and d are the only required parameters. |
| 89 | * For others, value 0 means default. |
| 90 | */ |
| 91 | typedef struct { |
| 92 | unsigned k; /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */ |
| 93 | unsigned d; /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */ |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 94 | unsigned steps; /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */ |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 95 | unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */ |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 96 | 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 */ |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 97 | ZDICT_params_t zParams; |
| 98 | } ZDICT_cover_params_t; |
| 99 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 100 | typedef struct { |
| 101 | unsigned k; /* Segment size : constraint: 0 < k : Reasonable range [16, 2048+] */ |
| 102 | unsigned d; /* dmer size : constraint: 0 < d <= k : Reasonable range [6, 16] */ |
| 103 | unsigned f; /* log of size of frequency array : constraint: 0 < f <= 31 : 1 means default(20)*/ |
| 104 | unsigned steps; /* Number of steps : Only used for optimization : 0 means default (40) : Higher means more parameters checked */ |
| 105 | unsigned nbThreads; /* Number of threads : constraint: 0 < nbThreads : 1 means single-threaded : Only used for optimization : Ignored if ZSTD_MULTITHREAD is not defined */ |
| 106 | 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 */ |
| 107 | unsigned accel; /* Acceleration level: constraint: 0 < accel <= 10, higher means faster and less accurate, 0 means default(1) */ |
| 108 | ZDICT_params_t zParams; |
| 109 | } ZDICT_fastCover_params_t; |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 110 | |
| 111 | /*! ZDICT_trainFromBuffer_cover(): |
| 112 | * Train a dictionary from an array of samples using the COVER algorithm. |
| 113 | * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, |
| 114 | * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. |
| 115 | * The resulting dictionary will be saved into `dictBuffer`. |
| 116 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
| 117 | * or an error code, which can be tested with ZDICT_isError(). |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 118 | * See ZDICT_trainFromBuffer() for details on failure modes. |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 119 | * Note: ZDICT_trainFromBuffer_cover() requires about 9 bytes of memory for each input byte. |
| 120 | * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. |
| 121 | * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. |
| 122 | * In general, it's recommended to provide a few thousands samples, though this can vary a lot. |
| 123 | * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. |
| 124 | */ |
| 125 | ZDICTLIB_API size_t ZDICT_trainFromBuffer_cover( |
| 126 | void *dictBuffer, size_t dictBufferCapacity, |
| 127 | const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples, |
| 128 | ZDICT_cover_params_t parameters); |
| 129 | |
| 130 | /*! ZDICT_optimizeTrainFromBuffer_cover(): |
| 131 | * The same requirements as above hold for all the parameters except `parameters`. |
| 132 | * This function tries many parameter combinations and picks the best parameters. |
| 133 | * `*parameters` is filled with the best parameters found, |
| 134 | * dictionary constructed with those parameters is stored in `dictBuffer`. |
| 135 | * |
| 136 | * All of the parameters d, k, steps are optional. |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 137 | * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}. |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 138 | * if steps is zero it defaults to its default value. |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 139 | * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000]. |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 140 | * |
| 141 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 142 | * or an error code, which can be tested with ZDICT_isError(). |
| 143 | * On success `*parameters` contains the parameters selected. |
| 144 | * See ZDICT_trainFromBuffer() for details on failure modes. |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 145 | * 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. |
| 146 | */ |
| 147 | ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_cover( |
| 148 | void* dictBuffer, size_t dictBufferCapacity, |
| 149 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, |
| 150 | ZDICT_cover_params_t* parameters); |
| 151 | |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 152 | /*! ZDICT_trainFromBuffer_fastCover(): |
| 153 | * Train a dictionary from an array of samples using a modified version of COVER algorithm. |
| 154 | * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, |
| 155 | * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. |
| 156 | * d and k are required. |
| 157 | * All other parameters are optional, will use default values if not provided |
| 158 | * The resulting dictionary will be saved into `dictBuffer`. |
| 159 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
| 160 | * or an error code, which can be tested with ZDICT_isError(). |
| 161 | * See ZDICT_trainFromBuffer() for details on failure modes. |
| 162 | * Note: ZDICT_trainFromBuffer_fastCover() requires 6 * 2^f bytes of memory. |
| 163 | * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. |
| 164 | * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. |
| 165 | * In general, it's recommended to provide a few thousands samples, though this can vary a lot. |
| 166 | * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. |
| 167 | */ |
| 168 | ZDICTLIB_API size_t ZDICT_trainFromBuffer_fastCover(void *dictBuffer, |
| 169 | size_t dictBufferCapacity, const void *samplesBuffer, |
| 170 | const size_t *samplesSizes, unsigned nbSamples, |
| 171 | ZDICT_fastCover_params_t parameters); |
| 172 | |
| 173 | /*! ZDICT_optimizeTrainFromBuffer_fastCover(): |
| 174 | * The same requirements as above hold for all the parameters except `parameters`. |
| 175 | * This function tries many parameter combinations (specifically, k and d combinations) |
| 176 | * and picks the best parameters. `*parameters` is filled with the best parameters found, |
| 177 | * dictionary constructed with those parameters is stored in `dictBuffer`. |
| 178 | * All of the parameters d, k, steps, f, and accel are optional. |
| 179 | * If d is non-zero then we don't check multiple values of d, otherwise we check d = {6, 8}. |
| 180 | * if steps is zero it defaults to its default value. |
| 181 | * If k is non-zero then we don't check multiple values of k, otherwise we check steps values in [50, 2000]. |
| 182 | * If f is zero, default value of 20 is used. |
| 183 | * If accel is zero, default value of 1 is used. |
| 184 | * |
| 185 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
| 186 | * or an error code, which can be tested with ZDICT_isError(). |
| 187 | * On success `*parameters` contains the parameters selected. |
| 188 | * See ZDICT_trainFromBuffer() for details on failure modes. |
| 189 | * Note: ZDICT_optimizeTrainFromBuffer_fastCover() requires about 6 * 2^f bytes of memory for each thread. |
| 190 | */ |
| 191 | ZDICTLIB_API size_t ZDICT_optimizeTrainFromBuffer_fastCover(void* dictBuffer, |
| 192 | size_t dictBufferCapacity, const void* samplesBuffer, |
| 193 | const size_t* samplesSizes, unsigned nbSamples, |
| 194 | ZDICT_fastCover_params_t* parameters); |
| 195 | |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 196 | /*! ZDICT_finalizeDictionary(): |
| 197 | * Given a custom content as a basis for dictionary, and a set of samples, |
| 198 | * finalize dictionary by adding headers and statistics. |
| 199 | * |
| 200 | * Samples must be stored concatenated in a flat buffer `samplesBuffer`, |
| 201 | * supplied with an array of sizes `samplesSizes`, providing the size of each sample in order. |
| 202 | * |
| 203 | * dictContentSize must be >= ZDICT_CONTENTSIZE_MIN bytes. |
| 204 | * maxDictSize must be >= dictContentSize, and must be >= ZDICT_DICTSIZE_MIN bytes. |
| 205 | * |
| 206 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`), |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 207 | * or an error code, which can be tested by ZDICT_isError(). |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 208 | * Note: ZDICT_finalizeDictionary() will push notifications into stderr if instructed to, using notificationLevel>0. |
| 209 | * Note 2: dictBuffer and dictContent can overlap |
| 210 | */ |
| 211 | #define ZDICT_CONTENTSIZE_MIN 128 |
| 212 | #define ZDICT_DICTSIZE_MIN 256 |
| 213 | ZDICTLIB_API size_t ZDICT_finalizeDictionary(void* dictBuffer, size_t dictBufferCapacity, |
| 214 | const void* dictContent, size_t dictContentSize, |
| 215 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples, |
| 216 | ZDICT_params_t parameters); |
| 217 | |
| 218 | typedef struct { |
| 219 | unsigned selectivityLevel; /* 0 means default; larger => select more => larger dictionary */ |
| 220 | ZDICT_params_t zParams; |
| 221 | } ZDICT_legacy_params_t; |
| 222 | |
| 223 | /*! ZDICT_trainFromBuffer_legacy(): |
| 224 | * Train a dictionary from an array of samples. |
| 225 | * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, |
| 226 | * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. |
| 227 | * The resulting dictionary will be saved into `dictBuffer`. |
| 228 | * `parameters` is optional and can be provided with values set to 0 to mean "default". |
| 229 | * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) |
| 230 | * or an error code, which can be tested with ZDICT_isError(). |
Abhilash S.L | 3b49463 | 2019-07-16 15:51:09 +0530 | [diff] [blame] | 231 | * See ZDICT_trainFromBuffer() for details on failure modes. |
William Kurkian | ea86948 | 2019-04-09 15:16:11 -0400 | [diff] [blame] | 232 | * Tips: In general, a reasonable dictionary has a size of ~ 100 KB. |
| 233 | * It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`. |
| 234 | * In general, it's recommended to provide a few thousands samples, though this can vary a lot. |
| 235 | * It's recommended that total size of all samples be about ~x100 times the target size of dictionary. |
| 236 | * Note: ZDICT_trainFromBuffer_legacy() will send notifications into stderr if instructed to, using notificationLevel>0. |
| 237 | */ |
| 238 | ZDICTLIB_API size_t ZDICT_trainFromBuffer_legacy( |
| 239 | void *dictBuffer, size_t dictBufferCapacity, |
| 240 | const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples, |
| 241 | ZDICT_legacy_params_t parameters); |
| 242 | |
| 243 | /* Deprecation warnings */ |
| 244 | /* It is generally possible to disable deprecation warnings from compiler, |
| 245 | for example with -Wno-deprecated-declarations for gcc |
| 246 | or _CRT_SECURE_NO_WARNINGS in Visual. |
| 247 | Otherwise, it's also possible to manually define ZDICT_DISABLE_DEPRECATE_WARNINGS */ |
| 248 | #ifdef ZDICT_DISABLE_DEPRECATE_WARNINGS |
| 249 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API /* disable deprecation warnings */ |
| 250 | #else |
| 251 | # define ZDICT_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
| 252 | # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ |
| 253 | # define ZDICT_DEPRECATED(message) [[deprecated(message)]] ZDICTLIB_API |
| 254 | # elif (ZDICT_GCC_VERSION >= 405) || defined(__clang__) |
| 255 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated(message))) |
| 256 | # elif (ZDICT_GCC_VERSION >= 301) |
| 257 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API __attribute__((deprecated)) |
| 258 | # elif defined(_MSC_VER) |
| 259 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API __declspec(deprecated(message)) |
| 260 | # else |
| 261 | # pragma message("WARNING: You need to implement ZDICT_DEPRECATED for this compiler") |
| 262 | # define ZDICT_DEPRECATED(message) ZDICTLIB_API |
| 263 | # endif |
| 264 | #endif /* ZDICT_DISABLE_DEPRECATE_WARNINGS */ |
| 265 | |
| 266 | ZDICT_DEPRECATED("use ZDICT_finalizeDictionary() instead") |
| 267 | size_t ZDICT_addEntropyTablesFromBuffer(void* dictBuffer, size_t dictContentSize, size_t dictBufferCapacity, |
| 268 | const void* samplesBuffer, const size_t* samplesSizes, unsigned nbSamples); |
| 269 | |
| 270 | |
| 271 | #endif /* ZDICT_STATIC_LINKING_ONLY */ |
| 272 | |
| 273 | #if defined (__cplusplus) |
| 274 | } |
| 275 | #endif |
| 276 | |
| 277 | #endif /* DICTBUILDER_H_001 */ |