zdict.h 15 KB

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