@annouimo/c-exam-kit-preset 0.0.0-stage → 0.6.4-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (291) hide show
  1. package/LICENSE +18 -0
  2. package/README.md +19 -2
  3. package/package.json +25 -4
  4. package/preset/components/BigInt.h +5 -0
  5. package/preset/components/Combination.h +4 -0
  6. package/preset/components/Complex.h +3 -0
  7. package/preset/components/Date.h +1 -0
  8. package/preset/components/EXAM_BIGINT_CAP.h +3 -0
  9. package/preset/components/EXAM_ENUM_CAP.h +3 -0
  10. package/preset/components/ExamIntIndexPair.h +4 -0
  11. package/preset/components/Permutation.h +5 -0
  12. package/preset/components/Record.h +1 -0
  13. package/preset/components/Time.h +1 -0
  14. package/preset/components/argmax_doubles.h +7 -0
  15. package/preset/components/argmax_ints.h +6 -0
  16. package/preset/components/argmax_lls.h +6 -0
  17. package/preset/components/argmin_doubles.h +7 -0
  18. package/preset/components/argmin_ints.h +6 -0
  19. package/preset/components/argmin_lls.h +6 -0
  20. package/preset/components/argsort_ints.h +2 -0
  21. package/preset/components/argsort_ints_into.h +19 -0
  22. package/preset/components/base_to_int.h +20 -0
  23. package/preset/components/bigint_add.h +22 -0
  24. package/preset/components/bigint_compare.h +6 -0
  25. package/preset/components/bigint_div_small.h +19 -0
  26. package/preset/components/bigint_divmod.h +34 -0
  27. package/preset/components/bigint_factorial.h +10 -0
  28. package/preset/components/bigint_from_ll.h +16 -0
  29. package/preset/components/bigint_from_str.h +19 -0
  30. package/preset/components/bigint_mul.h +25 -0
  31. package/preset/components/bigint_mul_small.h +20 -0
  32. package/preset/components/bigint_print.h +7 -0
  33. package/preset/components/bigint_sub.h +7 -0
  34. package/preset/components/bigint_to_str.h +2 -0
  35. package/preset/components/bigint_to_str_into.h +10 -0
  36. package/preset/components/bigint_zero.h +5 -0
  37. package/preset/components/binary_search_doubles.h +4 -0
  38. package/preset/components/binary_search_ints.h +4 -0
  39. package/preset/components/binary_search_lls.h +5 -0
  40. package/preset/components/bisect_root.h +23 -0
  41. package/preset/components/bit_clear.h +3 -0
  42. package/preset/components/bit_count.h +5 -0
  43. package/preset/components/bit_get.h +3 -0
  44. package/preset/components/bit_highest_set_index.h +6 -0
  45. package/preset/components/bit_lowest_set_index.h +6 -0
  46. package/preset/components/bit_rotate_left.h +5 -0
  47. package/preset/components/bit_set.h +3 -0
  48. package/preset/components/bit_toggle.h +3 -0
  49. package/preset/components/bubble_sort_ints.h +11 -0
  50. package/preset/components/build_diff_ints.h +2 -0
  51. package/preset/components/build_diff_ints_into.h +8 -0
  52. package/preset/components/build_prefix_ints.h +2 -0
  53. package/preset/components/build_prefix_ints_into.h +7 -0
  54. package/preset/components/combination_new.h +8 -0
  55. package/preset/components/combination_next.h +12 -0
  56. package/preset/components/complex_abs.h +3 -0
  57. package/preset/components/complex_add.h +3 -0
  58. package/preset/components/complex_div.h +11 -0
  59. package/preset/components/complex_mul.h +3 -0
  60. package/preset/components/complex_new.h +4 -0
  61. package/preset/components/complex_print.h +5 -0
  62. package/preset/components/complex_sub.h +3 -0
  63. package/preset/components/copy_doubles.h +1 -0
  64. package/preset/components/copy_doubles_into.h +6 -0
  65. package/preset/components/copy_ints.h +2 -0
  66. package/preset/components/copy_ints_into.h +6 -0
  67. package/preset/components/copy_lls.h +1 -0
  68. package/preset/components/copy_lls_into.h +5 -0
  69. package/preset/components/count_doubles.h +6 -0
  70. package/preset/components/count_ints.h +6 -0
  71. package/preset/components/count_lls.h +6 -0
  72. package/preset/components/date_add_days.h +6 -0
  73. package/preset/components/date_compare.h +4 -0
  74. package/preset/components/date_day_of_year.h +6 -0
  75. package/preset/components/date_days_in_month.h +5 -0
  76. package/preset/components/date_diff.h +3 -0
  77. package/preset/components/date_from_serial.h +16 -0
  78. package/preset/components/date_is_leap.h +3 -0
  79. package/preset/components/date_new.h +5 -0
  80. package/preset/components/date_print.h +5 -0
  81. package/preset/components/date_to_serial.h +3 -0
  82. package/preset/components/date_valid.h +4 -0
  83. package/preset/components/diff_range_add.h +8 -0
  84. package/preset/components/diff_to_lls.h +2 -0
  85. package/preset/components/diff_to_lls_into.h +10 -0
  86. package/preset/components/digits_to_int.h +12 -0
  87. package/preset/components/exam_abs_ull.h +3 -0
  88. package/preset/components/exam_add_checked_ll.h +6 -0
  89. package/preset/components/exam_add_ll.h +6 -0
  90. package/preset/components/exam_algo_fail.h +4 -0
  91. package/preset/components/exam_bigint_add_abs.h +19 -0
  92. package/preset/components/exam_bigint_cmp_abs.h +6 -0
  93. package/preset/components/exam_bigint_normalize.h +5 -0
  94. package/preset/components/exam_bigint_sub_abs.h +14 -0
  95. package/preset/components/exam_bigint_sub_abs_inplace.h +10 -0
  96. package/preset/components/exam_bit_check.h +4 -0
  97. package/preset/components/exam_check_n.h +3 -0
  98. package/preset/components/exam_cmp_double.h +4 -0
  99. package/preset/components/exam_cmp_double_desc.h +4 -0
  100. package/preset/components/exam_cmp_int_asc.h +4 -0
  101. package/preset/components/exam_cmp_int_desc.h +4 -0
  102. package/preset/components/exam_cmp_ll.h +4 -0
  103. package/preset/components/exam_cmp_ll_desc.h +4 -0
  104. package/preset/components/exam_cmp_unsigned_char.h +5 -0
  105. package/preset/components/exam_date_start_of_year.h +4 -0
  106. package/preset/components/exam_digit_value.h +6 -0
  107. package/preset/components/exam_doubles_finite.h +5 -0
  108. package/preset/components/exam_int_index_compare.h +7 -0
  109. package/preset/components/exam_io_fail.h +4 -0
  110. package/preset/components/exam_io_get.h +5 -0
  111. package/preset/components/exam_io_line_buffer.h +19 -0
  112. package/preset/components/exam_io_partial_line.h +4 -0
  113. package/preset/components/exam_io_skip_current_line.h +9 -0
  114. package/preset/components/exam_io_space.h +3 -0
  115. package/preset/components/exam_io_token.h +17 -0
  116. package/preset/components/exam_io_unget.h +4 -0
  117. package/preset/components/exam_io_write.h +3 -0
  118. package/preset/components/exam_mod_add.h +5 -0
  119. package/preset/components/exam_mod_mul.h +11 -0
  120. package/preset/components/exam_mul_ll.h +8 -0
  121. package/preset/components/exam_record_qsort_cmp.h +3 -0
  122. package/preset/components/exam_sort_records.h +8 -0
  123. package/preset/components/exam_split_store.h +10 -0
  124. package/preset/components/exam_sub_checked_ll.h +6 -0
  125. package/preset/components/factorial_basic.h +6 -0
  126. package/preset/components/fill_doubles.h +1 -0
  127. package/preset/components/fill_doubles_into.h +6 -0
  128. package/preset/components/fill_ints.h +2 -0
  129. package/preset/components/fill_ints_into.h +5 -0
  130. package/preset/components/fill_lls.h +1 -0
  131. package/preset/components/fill_lls_into.h +5 -0
  132. package/preset/components/find_doubles.h +5 -0
  133. package/preset/components/find_ints.h +3 -0
  134. package/preset/components/find_lls.h +5 -0
  135. package/preset/components/first_true_int.h +13 -0
  136. package/preset/components/frequency_ints.h +3 -0
  137. package/preset/components/frequency_ints_into.h +15 -0
  138. package/preset/components/gcd.h +5 -0
  139. package/preset/components/int_digit_at.h +7 -0
  140. package/preset/components/int_digit_count.h +7 -0
  141. package/preset/components/int_digit_sum.h +7 -0
  142. package/preset/components/int_pow.h +9 -0
  143. package/preset/components/int_reverse_digits.h +15 -0
  144. package/preset/components/int_to_base.h +2 -0
  145. package/preset/components/int_to_base_into.h +19 -0
  146. package/preset/components/int_to_digits.h +2 -0
  147. package/preset/components/int_to_digits_into.h +13 -0
  148. package/preset/components/is_power_of_two.h +3 -0
  149. package/preset/components/is_prime.h +7 -0
  150. package/preset/components/lcm.h +7 -0
  151. package/preset/components/linear_search_ints.h +5 -0
  152. package/preset/components/longest_equal_run_ints.h +10 -0
  153. package/preset/components/longest_plus_one_run_ints.h +10 -0
  154. package/preset/components/lower_bound_doubles.h +9 -0
  155. package/preset/components/lower_bound_ints.h +10 -0
  156. package/preset/components/lower_bound_lls.h +9 -0
  157. package/preset/components/max_doubles.h +7 -0
  158. package/preset/components/max_forward_diff_ints.h +11 -0
  159. package/preset/components/max_ints.h +6 -0
  160. package/preset/components/max_lls.h +6 -0
  161. package/preset/components/max_subarray_sum_ints.h +9 -0
  162. package/preset/components/mean_doubles.h +7 -0
  163. package/preset/components/mean_ints.h +4 -0
  164. package/preset/components/mean_lls.h +6 -0
  165. package/preset/components/median_doubles.h +15 -0
  166. package/preset/components/median_ints.h +14 -0
  167. package/preset/components/median_lls.h +14 -0
  168. package/preset/components/min_doubles.h +7 -0
  169. package/preset/components/min_ints.h +6 -0
  170. package/preset/components/min_lls.h +6 -0
  171. package/preset/components/mod_pow.h +14 -0
  172. package/preset/components/nearly_equal.h +11 -0
  173. package/preset/components/permutation_new.h +9 -0
  174. package/preset/components/permutation_next.h +25 -0
  175. package/preset/components/poly_eval_lls.h +10 -0
  176. package/preset/components/prefix_range_sum.h +5 -0
  177. package/preset/components/print_char.h +4 -0
  178. package/preset/components/print_double.h +5 -0
  179. package/preset/components/print_doubles.h +9 -0
  180. package/preset/components/print_int.h +4 -0
  181. package/preset/components/print_ints.h +9 -0
  182. package/preset/components/print_ll.h +4 -0
  183. package/preset/components/print_lls.h +9 -0
  184. package/preset/components/print_text.h +4 -0
  185. package/preset/components/range_ints.h +2 -0
  186. package/preset/components/range_ints_into.h +14 -0
  187. package/preset/components/range_lls.h +3 -0
  188. package/preset/components/range_lls_into.h +17 -0
  189. package/preset/components/read_bigint.h +3 -0
  190. package/preset/components/read_char.h +5 -0
  191. package/preset/components/read_char_raw.h +5 -0
  192. package/preset/components/read_double.h +5 -0
  193. package/preset/components/read_doubles.h +2 -0
  194. package/preset/components/read_doubles_into.h +6 -0
  195. package/preset/components/read_int.h +5 -0
  196. package/preset/components/read_ints.h +2 -0
  197. package/preset/components/read_ints_into.h +6 -0
  198. package/preset/components/read_line.h +1 -0
  199. package/preset/components/read_line_into.h +4 -0
  200. package/preset/components/read_ll.h +5 -0
  201. package/preset/components/read_lls.h +2 -0
  202. package/preset/components/read_lls_into.h +6 -0
  203. package/preset/components/read_rest.h +1 -0
  204. package/preset/components/read_rest_into.h +4 -0
  205. package/preset/components/read_word.h +1 -0
  206. package/preset/components/read_word_into.h +4 -0
  207. package/preset/components/record_cmp.h +1 -0
  208. package/preset/components/reverse_doubles.h +6 -0
  209. package/preset/components/reverse_ints.h +8 -0
  210. package/preset/components/reverse_lls.h +6 -0
  211. package/preset/components/round_digits.h +8 -0
  212. package/preset/components/sieve_primes.h +2 -0
  213. package/preset/components/sieve_primes_into.h +13 -0
  214. package/preset/components/skip_line.h +3 -0
  215. package/preset/components/sort_doubles.h +4 -0
  216. package/preset/components/sort_doubles_desc.h +4 -0
  217. package/preset/components/sort_ints.h +4 -0
  218. package/preset/components/sort_ints_desc.h +4 -0
  219. package/preset/components/sort_lls.h +4 -0
  220. package/preset/components/sort_lls_desc.h +6 -0
  221. package/preset/components/sort_records.h +2 -0
  222. package/preset/components/stddev_doubles.h +3 -0
  223. package/preset/components/stddev_ints.h +3 -0
  224. package/preset/components/stddev_lls.h +3 -0
  225. package/preset/components/str_count_char.h +6 -0
  226. package/preset/components/str_count_substr.h +13 -0
  227. package/preset/components/str_erase.h +7 -0
  228. package/preset/components/str_insert.h +2 -0
  229. package/preset/components/str_insert_into.h +10 -0
  230. package/preset/components/str_is_palindrome.h +5 -0
  231. package/preset/components/str_join.h +4 -0
  232. package/preset/components/str_join_into.h +27 -0
  233. package/preset/components/str_longest_char_run.h +9 -0
  234. package/preset/components/str_lower.h +4 -0
  235. package/preset/components/str_remove_char.h +11 -0
  236. package/preset/components/str_replace_all.h +2 -0
  237. package/preset/components/str_replace_all_into.h +34 -0
  238. package/preset/components/str_replace_char.h +8 -0
  239. package/preset/components/str_reverse.h +6 -0
  240. package/preset/components/str_sort_chars.h +3 -0
  241. package/preset/components/str_split.h +3 -0
  242. package/preset/components/str_split_into.h +18 -0
  243. package/preset/components/str_split_whitespace.h +4 -0
  244. package/preset/components/str_split_whitespace_into.h +17 -0
  245. package/preset/components/str_trim.h +7 -0
  246. package/preset/components/str_unique_chars.h +10 -0
  247. package/preset/components/str_upper.h +4 -0
  248. package/preset/components/sum_doubles.h +7 -0
  249. package/preset/components/sum_ints.h +6 -0
  250. package/preset/components/sum_lls.h +11 -0
  251. package/preset/components/swap_ints.h +4 -0
  252. package/preset/components/time_add_seconds.h +5 -0
  253. package/preset/components/time_diff.h +3 -0
  254. package/preset/components/time_from_seconds.h +4 -0
  255. package/preset/components/time_new.h +5 -0
  256. package/preset/components/time_next_minute.h +3 -0
  257. package/preset/components/time_next_second.h +3 -0
  258. package/preset/components/time_print.h +5 -0
  259. package/preset/components/time_to_seconds.h +4 -0
  260. package/preset/components/time_valid.h +4 -0
  261. package/preset/components/try_read_bigint.h +7 -0
  262. package/preset/components/try_read_char.h +9 -0
  263. package/preset/components/try_read_char_raw.h +8 -0
  264. package/preset/components/try_read_double.h +12 -0
  265. package/preset/components/try_read_int.h +13 -0
  266. package/preset/components/try_read_line.h +1 -0
  267. package/preset/components/try_read_line_into.h +5 -0
  268. package/preset/components/try_read_ll.h +12 -0
  269. package/preset/components/try_read_rest.h +1 -0
  270. package/preset/components/try_read_rest_into.h +4 -0
  271. package/preset/components/try_read_word.h +1 -0
  272. package/preset/components/try_read_word_into.h +4 -0
  273. package/preset/components/unique_doubles.h +10 -0
  274. package/preset/components/unique_ints.h +11 -0
  275. package/preset/components/unique_lls.h +10 -0
  276. package/preset/components/unique_sorted_doubles.h +8 -0
  277. package/preset/components/unique_sorted_ints.h +8 -0
  278. package/preset/components/unique_sorted_lls.h +8 -0
  279. package/preset/components/upper_bound_doubles.h +9 -0
  280. package/preset/components/upper_bound_ints.h +10 -0
  281. package/preset/components/upper_bound_lls.h +9 -0
  282. package/preset/components/values_doubles.h +4 -0
  283. package/preset/components/values_doubles_into.h +9 -0
  284. package/preset/components/values_ints.h +3 -0
  285. package/preset/components/values_ints_into.h +7 -0
  286. package/preset/components/values_lls.h +4 -0
  287. package/preset/components/values_lls_into.h +9 -0
  288. package/preset/components/variance_doubles.h +14 -0
  289. package/preset/components/variance_ints.h +10 -0
  290. package/preset/components/variance_lls.h +11 -0
  291. package/preset/manifest.json +2996 -0
package/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ MIT No Attribution
2
+
3
+ Copyright (c) 2026 Annouimo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so.
11
+
12
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,20 @@
1
- # Temporary Holding Version
1
+ # @annouimo/c-exam-kit-preset (draft)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This npm package contains **C99 header/template data only**. It does not run code on installation, download assets at runtime, or replace the independent C Exam Kit project.
4
+
5
+ ## Build locally
6
+
7
+ From `npm/accoding-preset/`:
8
+
9
+ ```sh
10
+ npm pack --dry-run
11
+ npm pack
12
+ ```
13
+
14
+ The `prepack` step reads the maintained C99 headers in `scaffold/presets/exam-io-c99/`. It emits one component per definition and writes its transitive inputs as direct edges in `preset/manifest.json`. A component can be a function, a macro, a type, or a callback declaration. Identical definitions shared by source headers are emitted once. Conflicting definitions and dependency cycles fail the build.
15
+
16
+ The package carries the manifest and component fragments. Accoding Companion assembles the full IDE header once per workspace version and performs a separate dependency traversal for each submission.
17
+
18
+ Accoding Companion's external installer downloads this package into an explicitly versioned, user-owned data directory. Existing exercise snapshots are never changed just because a new version was downloaded.
19
+
20
+ Release is intentionally blocked by `private: true` until the package contents and integration tests are accepted.
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "@annouimo/c-exam-kit-preset",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.6.4-next.1",
4
+ "description": "Versioned C99 data-only presets for Accoding Companion",
5
+ "license": "MIT-0",
6
+ "files": [
7
+ "preset/",
8
+ "LICENSE"
9
+ ],
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "scripts": {
14
+ "prepack": "node scripts/build-preset.mjs"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/Annouimo/c-exam-kit.git",
19
+ "directory": "npm/accoding-preset"
20
+ },
21
+ "keywords": [
22
+ "accoding",
23
+ "c99",
24
+ "scaffold",
25
+ "data-only"
26
+ ]
27
+ }
@@ -0,0 +1,5 @@
1
+ typedef struct {
2
+ int sign; /* -1, 0, +1; 0 is represented by sign=0,len=1,d[0]=0 */
3
+ int len;
4
+ unsigned char d[EXAM_BIGINT_CAP]; /* least significant digit first */
5
+ } BigInt;
@@ -0,0 +1,4 @@
1
+ typedef struct {
2
+ int n, r, started, finished;
3
+ int indices[EXAM_ENUM_CAP];
4
+ } Combination;
@@ -0,0 +1,3 @@
1
+ typedef struct {
2
+ double re, im;
3
+ } Complex;
@@ -0,0 +1 @@
1
+ typedef struct { int year, month, day; } Date;
@@ -0,0 +1,3 @@
1
+ #ifndef EXAM_BIGINT_CAP
2
+ #define EXAM_BIGINT_CAP 2048
3
+ #endif
@@ -0,0 +1,3 @@
1
+ #ifndef EXAM_ENUM_CAP
2
+ #define EXAM_ENUM_CAP 64
3
+ #endif
@@ -0,0 +1,4 @@
1
+ typedef struct {
2
+ int value;
3
+ int index;
4
+ } ExamIntIndexPair;
@@ -0,0 +1,5 @@
1
+ typedef struct {
2
+ int n, r, started, finished;
3
+ int indices[EXAM_ENUM_CAP];
4
+ int pool[EXAM_ENUM_CAP], cycles[EXAM_ENUM_CAP];
5
+ } Permutation;
@@ -0,0 +1 @@
1
+ typedef struct Record Record;
@@ -0,0 +1 @@
1
+ typedef struct { int hour, minute, second; } Time;
@@ -0,0 +1,7 @@
1
+ static inline int argmax_doubles(const double *a, int n) {
2
+ if (n <= 0) exam_algo_fail("argmax_doubles requires nonempty array");
3
+ exam_doubles_finite(a, n);
4
+ int result = 0;
5
+ for (int i = 1; i < n; ++i) if (a[i] > a[result]) result = i;
6
+ return result;
7
+ }
@@ -0,0 +1,6 @@
1
+ static inline int argmax_ints(const int *a, int n) {
2
+ if (n <= 0) exam_algo_fail("argmax_ints requires nonempty array");
3
+ int pos = 0;
4
+ for (int i = 1; i < n; ++i) if (a[i] > a[pos]) pos = i;
5
+ return pos;
6
+ }
@@ -0,0 +1,6 @@
1
+ static inline int argmax_lls(const long long *a, int n) {
2
+ if (n <= 0) exam_algo_fail("argmax_lls requires nonempty array");
3
+ int result = 0;
4
+ for (int i = 1; i < n; ++i) if (a[i] > a[result]) result = i;
5
+ return result;
6
+ }
@@ -0,0 +1,7 @@
1
+ static inline int argmin_doubles(const double *a, int n) {
2
+ if (n <= 0) exam_algo_fail("argmin_doubles requires nonempty array");
3
+ exam_doubles_finite(a, n);
4
+ int result = 0;
5
+ for (int i = 1; i < n; ++i) if (a[i] < a[result]) result = i;
6
+ return result;
7
+ }
@@ -0,0 +1,6 @@
1
+ static inline int argmin_ints(const int *a, int n) {
2
+ if (n <= 0) exam_algo_fail("argmin_ints requires nonempty array");
3
+ int pos = 0;
4
+ for (int i = 1; i < n; ++i) if (a[i] < a[pos]) pos = i;
5
+ return pos;
6
+ }
@@ -0,0 +1,6 @@
1
+ static inline int argmin_lls(const long long *a, int n) {
2
+ if (n <= 0) exam_algo_fail("argmin_lls requires nonempty array");
3
+ int result = 0;
4
+ for (int i = 1; i < n; ++i) if (a[i] < a[result]) result = i;
5
+ return result;
6
+ }
@@ -0,0 +1,2 @@
1
+ #define argsort_ints(a, n, order) \
2
+ argsort_ints_into((a), (n), (order), sizeof(order) / sizeof((order)[0]))
@@ -0,0 +1,19 @@
1
+ static inline void argsort_ints_into(const int *a, int n, int *order,
2
+ size_t capacity) {
3
+ if (n < 0 || (size_t)n > capacity)
4
+ exam_algo_fail("argsort_ints output capacity too small");
5
+ if (n == 0) return;
6
+ if (!a || !order) exam_algo_fail("null argsort_ints array");
7
+ if ((size_t)n > SIZE_MAX / sizeof(ExamIntIndexPair))
8
+ exam_algo_fail("argsort_ints allocation size overflow");
9
+ ExamIntIndexPair *pairs = (ExamIntIndexPair *)malloc(
10
+ (size_t)n * sizeof(*pairs));
11
+ if (!pairs) exam_algo_fail("argsort_ints allocation failed");
12
+ for (int i = 0; i < n; ++i) {
13
+ pairs[i].value = a[i];
14
+ pairs[i].index = i;
15
+ }
16
+ qsort(pairs, (size_t)n, sizeof(*pairs), exam_int_index_compare);
17
+ for (int i = 0; i < n; ++i) order[i] = pairs[i].index;
18
+ free(pairs);
19
+ }
@@ -0,0 +1,20 @@
1
+ static inline long long base_to_int(const char *s, int base) {
2
+ if (base < 2 || base > 36) exam_algo_fail("base must be 2..36");
3
+ bool negative = false;
4
+ if (*s == '+' || *s == '-') { negative = (*s == '-'); ++s; }
5
+ if (!*s) exam_algo_fail("missing digits");
6
+ unsigned long long magnitude = 0;
7
+ unsigned long long limit = (unsigned long long)LLONG_MAX + (negative ? 1ULL : 0ULL);
8
+ for (; *s; ++s) {
9
+ int digit = exam_digit_value((unsigned char)*s);
10
+ if (digit < 0 || digit >= base) exam_algo_fail("invalid base digit");
11
+ if (magnitude > (limit - (unsigned long long)digit)/(unsigned)base)
12
+ exam_algo_fail("base conversion overflow");
13
+ magnitude = magnitude*(unsigned)base + (unsigned)digit;
14
+ }
15
+ if (negative) {
16
+ if (magnitude == (unsigned long long)LLONG_MAX+1ULL) return LLONG_MIN;
17
+ return -(long long)magnitude;
18
+ }
19
+ return (long long)magnitude;
20
+ }
@@ -0,0 +1,22 @@
1
+ static inline void bigint_add(BigInt *out, const BigInt *a,
2
+ const BigInt *b) {
3
+ if (!out || !a || !b) exam_algo_fail("null BigInt addition operand");
4
+ BigInt result;
5
+ if (!a->sign) result = *b;
6
+ else if (!b->sign) result = *a;
7
+ else if (a->sign == b->sign) {
8
+ exam_bigint_add_abs(&result, a, b);
9
+ if (result.sign) result.sign = a->sign;
10
+ } else {
11
+ int cmp = exam_bigint_cmp_abs(a, b);
12
+ if (cmp == 0) bigint_zero(&result);
13
+ else if (cmp > 0) {
14
+ exam_bigint_sub_abs(&result, a, b);
15
+ result.sign = a->sign;
16
+ } else {
17
+ exam_bigint_sub_abs(&result, b, a);
18
+ result.sign = b->sign;
19
+ }
20
+ }
21
+ *out = result;
22
+ }
@@ -0,0 +1,6 @@
1
+ static inline int bigint_compare(const BigInt *a, const BigInt *b) {
2
+ if (a->sign != b->sign) return (a->sign > b->sign) ? 1 : -1;
3
+ if (!a->sign) return 0;
4
+ int cmp = exam_bigint_cmp_abs(a, b);
5
+ return a->sign > 0 ? cmp : -cmp;
6
+ }
@@ -0,0 +1,19 @@
1
+ static inline void bigint_div_small(BigInt *quotient, int *remainder,
2
+ const BigInt *a, int divisor) {
3
+ if (!quotient || !remainder || !a || divisor == 0)
4
+ exam_algo_fail("invalid BigInt small division arguments");
5
+ unsigned long long x = divisor < 0 ? 0ULL-(unsigned)divisor : (unsigned)divisor;
6
+ BigInt result;
7
+ bigint_zero(&result);
8
+ result.len = a->len;
9
+ unsigned long long carry = 0;
10
+ for (int i = a->len-1; i >= 0; --i) {
11
+ unsigned long long current = carry*10ULL + a->d[i];
12
+ result.d[i] = (unsigned char)(current/x);
13
+ carry = current%x;
14
+ }
15
+ result.sign = a->sign*(divisor < 0 ? -1 : 1);
16
+ exam_bigint_normalize(&result);
17
+ *quotient = result;
18
+ *remainder = (int)carry*(a->sign < 0 ? -1 : 1);
19
+ }
@@ -0,0 +1,34 @@
1
+ static inline void bigint_divmod(BigInt *quotient, BigInt *remainder,
2
+ const BigInt *a, const BigInt *b) {
3
+ if (!quotient || !remainder || !a || !b || quotient == remainder)
4
+ exam_algo_fail("invalid BigInt division arguments");
5
+ if (!b->sign) exam_algo_fail("BigInt division by zero");
6
+ BigInt q, r;
7
+ bigint_zero(&q);
8
+ bigint_zero(&r);
9
+ q.len = a->len;
10
+ for (int i = a->len-1; i >= 0; --i) {
11
+ if (r.len == EXAM_BIGINT_CAP && r.sign)
12
+ exam_algo_fail("BigInt division work buffer overflow");
13
+ if (r.sign) {
14
+ for (int j = r.len; j > 0; --j) r.d[j] = r.d[j-1];
15
+ ++r.len;
16
+ }
17
+ r.d[0] = a->d[i];
18
+ r.sign = 1;
19
+ exam_bigint_normalize(&r);
20
+ int digit = 0;
21
+ while (exam_bigint_cmp_abs(&r, b) >= 0) {
22
+ exam_bigint_sub_abs_inplace(&r, b);
23
+ ++digit;
24
+ if (digit > 9) exam_algo_fail("BigInt division internal error");
25
+ }
26
+ q.d[i] = (unsigned char)digit;
27
+ }
28
+ q.sign = a->sign * b->sign;
29
+ r.sign = a->sign;
30
+ exam_bigint_normalize(&q);
31
+ exam_bigint_normalize(&r);
32
+ *quotient = q;
33
+ *remainder = r;
34
+ }
@@ -0,0 +1,10 @@
1
+ static inline void bigint_factorial(BigInt *out, int n) {
2
+ if (!out || n < 0) exam_algo_fail("invalid BigInt factorial");
3
+ BigInt result;
4
+ bigint_zero(&result);
5
+ result.d[0] = 1;
6
+ result.sign = 1;
7
+ for (int i = 2; i <= n; ++i)
8
+ bigint_mul_small(&result, &result, i);
9
+ *out = result;
10
+ }
@@ -0,0 +1,16 @@
1
+ static inline void bigint_from_ll(BigInt *out, long long value) {
2
+ if (!out) exam_algo_fail("null BigInt destination");
3
+ BigInt temp;
4
+ bigint_zero(&temp);
5
+ unsigned long long x = value < 0 ? 0ULL-(unsigned long long)value :
6
+ (unsigned long long)value;
7
+ int n = 0;
8
+ do {
9
+ if (n >= EXAM_BIGINT_CAP) exam_algo_fail("BigInt capacity too small");
10
+ temp.d[n++] = (unsigned char)(x % 10ULL);
11
+ x /= 10ULL;
12
+ } while (x);
13
+ temp.len = n;
14
+ temp.sign = value == 0 ? 0 : (value < 0 ? -1 : 1);
15
+ *out = temp;
16
+ }
@@ -0,0 +1,19 @@
1
+ static inline void bigint_from_str(BigInt *out, const char *text) {
2
+ if (!text || !out) exam_algo_fail("null BigInt string/destination");
3
+ bool negative = false;
4
+ if (*text == '+' || *text == '-') { negative = *text == '-'; ++text; }
5
+ if (!*text) exam_algo_fail("BigInt has no digits");
6
+ size_t len = strlen(text);
7
+ if (len > EXAM_BIGINT_CAP) exam_algo_fail("BigInt input too long");
8
+ BigInt temp;
9
+ bigint_zero(&temp);
10
+ temp.len = (int)len;
11
+ temp.sign = negative ? -1 : 1;
12
+ for (size_t i = 0; i < len; ++i) {
13
+ char c = text[len-1-i];
14
+ if (c < '0' || c > '9') exam_algo_fail("invalid BigInt digit");
15
+ temp.d[i] = (unsigned char)(c-'0');
16
+ }
17
+ exam_bigint_normalize(&temp);
18
+ *out = temp;
19
+ }
@@ -0,0 +1,25 @@
1
+ static inline void bigint_mul(BigInt *out, const BigInt *a,
2
+ const BigInt *b) {
3
+ if (!out || !a || !b) exam_algo_fail("null BigInt multiplication operand");
4
+ BigInt result;
5
+ bigint_zero(&result);
6
+ if (!a->sign || !b->sign) { *out = result; return; }
7
+ if (a->len + b->len - 1 > EXAM_BIGINT_CAP)
8
+ exam_algo_fail("BigInt multiplication overflow");
9
+ for (int i = 0; i < a->len; ++i) {
10
+ int carry = 0, j = 0;
11
+ for (; j < b->len || carry; ++j) {
12
+ int position = i+j;
13
+ if (position >= EXAM_BIGINT_CAP) exam_algo_fail("BigInt multiplication overflow");
14
+ int value = result.d[position] + carry +
15
+ (j < b->len ? a->d[i] * b->d[j] : 0);
16
+ result.d[position] = (unsigned char)(value % 10);
17
+ carry = value / 10;
18
+ }
19
+ }
20
+ int size = a->len+b->len;
21
+ result.len = size <= EXAM_BIGINT_CAP ? size : EXAM_BIGINT_CAP;
22
+ result.sign = a->sign*b->sign;
23
+ exam_bigint_normalize(&result);
24
+ *out = result;
25
+ }
@@ -0,0 +1,20 @@
1
+ static inline void bigint_mul_small(BigInt *out, const BigInt *a, int factor) {
2
+ if (!out || !a) exam_algo_fail("null BigInt multiplication operand");
3
+ if (factor == 0 || !a->sign) { bigint_zero(out); return; }
4
+ unsigned long long x = factor < 0 ? 0ULL-(unsigned)factor : (unsigned)factor;
5
+ BigInt result;
6
+ bigint_zero(&result);
7
+ unsigned long long carry = 0;
8
+ int i = 0;
9
+ for (; i < a->len || carry; ++i) {
10
+ if (i >= EXAM_BIGINT_CAP) exam_algo_fail("BigInt multiplication overflow");
11
+ unsigned long long value = carry +
12
+ (i < a->len ? (unsigned long long)a->d[i]*x : 0ULL);
13
+ result.d[i] = (unsigned char)(value%10ULL);
14
+ carry = value/10ULL;
15
+ }
16
+ result.len = i;
17
+ result.sign = a->sign * (factor < 0 ? -1 : 1);
18
+ exam_bigint_normalize(&result);
19
+ *out = result;
20
+ }
@@ -0,0 +1,7 @@
1
+ static inline void bigint_print(const BigInt *a, const char *end) {
2
+ if (!a || !end) exam_algo_fail("null BigInt/terminator");
3
+ if (a->sign < 0 && putchar('-') == EOF) exam_algo_fail("BigInt output error");
4
+ for (int i = a->len - 1; i >= 0; --i)
5
+ if (putchar('0' + a->d[i]) == EOF) exam_algo_fail("BigInt output error");
6
+ if (fputs(end, stdout) == EOF) exam_algo_fail("BigInt output error");
7
+ }
@@ -0,0 +1,7 @@
1
+ static inline void bigint_sub(BigInt *out, const BigInt *a,
2
+ const BigInt *b) {
3
+ if (!out || !a || !b) exam_algo_fail("null BigInt subtraction operand");
4
+ BigInt inverted = *b;
5
+ inverted.sign = -inverted.sign;
6
+ bigint_add(out, a, &inverted);
7
+ }
@@ -0,0 +1,2 @@
1
+ #define bigint_to_str(a, array) \
2
+ bigint_to_str_into((a), (array), sizeof(array))
@@ -0,0 +1,10 @@
1
+ static inline void bigint_to_str_into(const BigInt *a, char *out,
2
+ size_t capacity) {
3
+ if (!a || !out) exam_algo_fail("null BigInt string/destination");
4
+ size_t required = (size_t)a->len + (a->sign < 0 ? 1U : 0U) + 1U;
5
+ if (required > capacity) exam_algo_fail("BigInt string buffer too small");
6
+ size_t pos = 0;
7
+ if (a->sign < 0) out[pos++] = '-';
8
+ for (int i = a->len - 1; i >= 0; --i) out[pos++] = (char)('0'+a->d[i]);
9
+ out[pos] = '\0';
10
+ }
@@ -0,0 +1,5 @@
1
+ static inline void bigint_zero(BigInt *out) {
2
+ if (!out) exam_algo_fail("null BigInt destination");
3
+ memset(out, 0, sizeof(*out));
4
+ out->len = 1;
5
+ }
@@ -0,0 +1,4 @@
1
+ static inline int binary_search_doubles(const double *a, int n, double key) {
2
+ int pos = lower_bound_doubles(a, n, key);
3
+ return pos < n && a[pos] == key ? pos : -1;
4
+ }
@@ -0,0 +1,4 @@
1
+ static inline int binary_search_ints(const int *a, int n, int key) {
2
+ int i = lower_bound_ints(a, n, key);
3
+ return i < n && a[i] == key ? i : -1;
4
+ }
@@ -0,0 +1,5 @@
1
+ static inline int binary_search_lls(const long long *a, int n,
2
+ long long key) {
3
+ int pos = lower_bound_lls(a, n, key);
4
+ return pos < n && a[pos] == key ? pos : -1;
5
+ }
@@ -0,0 +1,23 @@
1
+ static inline double bisect_root(double (*f)(double), double left,
2
+ double right, double tolerance, int iterations) {
3
+ if(!f || !isfinite(left) || !isfinite(right) || left>=right ||
4
+ !isfinite(tolerance) || tolerance<=0 || iterations<=0)
5
+ exam_algo_fail("invalid bisect_root arguments");
6
+ double a=f(left), b=f(right);
7
+ if(!isfinite(a) || !isfinite(b))
8
+ exam_algo_fail("nonfinite endpoint function value");
9
+ if(a==0) return left;
10
+ if(b==0) return right;
11
+ if((a<0)==(b<0))
12
+ exam_algo_fail("bisect_root requires opposite endpoint signs");
13
+ for(int i=0;i<iterations && right-left>tolerance;++i) {
14
+ double mid=left+(right-left)/2;
15
+ if(mid==left || mid==right)break;
16
+ double value=f(mid);
17
+ if(!isfinite(value))exam_algo_fail("nonfinite function value");
18
+ if(value==0)return mid;
19
+ if((value<0)==(a<0)) { left=mid; a=value; }
20
+ else { right=mid; b=value; }
21
+ }
22
+ return left+(right-left)/2;
23
+ }
@@ -0,0 +1,3 @@
1
+ static inline unsigned long long bit_clear(unsigned long long x, int bit) {
2
+ return x & ~(1ULL << exam_bit_check(bit));
3
+ }
@@ -0,0 +1,5 @@
1
+ static inline int bit_count(unsigned long long x) {
2
+ int count = 0;
3
+ while (x) { x &= x - 1ULL; ++count; }
4
+ return count;
5
+ }
@@ -0,0 +1,3 @@
1
+ static inline int bit_get(unsigned long long x, int bit) {
2
+ return (int)((x >> exam_bit_check(bit)) & 1ULL);
3
+ }
@@ -0,0 +1,6 @@
1
+ static inline int bit_highest_set_index(unsigned long long x) {
2
+ if (!x) return -1;
3
+ int k = 0;
4
+ while (x >>= 1U) ++k;
5
+ return k;
6
+ }
@@ -0,0 +1,6 @@
1
+ static inline int bit_lowest_set_index(unsigned long long x) {
2
+ if (!x) return -1;
3
+ int k = 0;
4
+ while (!(x & 1ULL)) { x >>= 1U; ++k; }
5
+ return k;
6
+ }
@@ -0,0 +1,5 @@
1
+ static inline unsigned long long bit_rotate_left(unsigned long long x, int count) {
2
+ unsigned int n = (unsigned int)((count % 64 + 64) % 64);
3
+ if (n == 0) return x;
4
+ return (x << n) | (x >> (64U - n));
5
+ }
@@ -0,0 +1,3 @@
1
+ static inline unsigned long long bit_set(unsigned long long x, int bit) {
2
+ return x | (1ULL << exam_bit_check(bit));
3
+ }
@@ -0,0 +1,3 @@
1
+ static inline unsigned long long bit_toggle(unsigned long long x, int bit) {
2
+ return x ^ (1ULL << exam_bit_check(bit));
3
+ }
@@ -0,0 +1,11 @@
1
+ static inline void bubble_sort_ints(int *a, int n) {
2
+ exam_check_n(n);
3
+ for (int end = n; end > 1; --end) {
4
+ bool changed = false;
5
+ for (int j = 1; j < end; ++j) if (a[j-1] > a[j]) {
6
+ int temp = a[j-1]; a[j-1] = a[j]; a[j] = temp;
7
+ changed = true;
8
+ }
9
+ if (!changed) break;
10
+ }
11
+ }
@@ -0,0 +1,2 @@
1
+ #define build_diff_ints(a,n,difference) \
2
+ build_diff_ints_into((a),(n),(difference),sizeof(difference)/sizeof((difference)[0]))
@@ -0,0 +1,8 @@
1
+ static inline void build_diff_ints_into(const int *a, int n,
2
+ long long *difference, size_t capacity) {
3
+ if (n < 0 || (size_t)n >= capacity)
4
+ exam_algo_fail("difference array capacity must be at least n+1");
5
+ for (int i = 0; i < n; ++i)
6
+ difference[i] = (long long)a[i] - (i ? (long long)a[i-1] : 0);
7
+ difference[n] = 0;
8
+ }
@@ -0,0 +1,2 @@
1
+ #define build_prefix_ints(a, n, prefix) \
2
+ build_prefix_ints_into((a), (n), (prefix), sizeof(prefix)/sizeof((prefix)[0]))
@@ -0,0 +1,7 @@
1
+ static inline void build_prefix_ints_into(const int *a, int n, long long *prefix,
2
+ size_t capacity) {
3
+ if (n < 0 || (size_t)n >= capacity || !prefix)
4
+ exam_algo_fail("prefix array capacity must be at least n+1");
5
+ prefix[0] = 0;
6
+ for (int i = 0; i < n; ++i) prefix[i+1] = prefix[i] + (long long)a[i];
7
+ }
@@ -0,0 +1,8 @@
1
+ static inline Combination combination_new(int n, int r) {
2
+ if (n < 0 || n > EXAM_ENUM_CAP || r < 0 || r > n)
3
+ exam_algo_fail("invalid combination dimensions");
4
+ Combination it = {0};
5
+ it.n = n; it.r = r;
6
+ for (int i = 0; i < r; ++i) it.indices[i] = i;
7
+ return it;
8
+ }
@@ -0,0 +1,12 @@
1
+ static inline bool combination_next(Combination *it) {
2
+ if (!it) exam_algo_fail("null combination iterator");
3
+ if (it->finished) return false;
4
+ if (!it->started) { it->started = 1; return true; }
5
+ int i = it->r - 1;
6
+ while (i >= 0 && it->indices[i] == it->n - it->r + i) --i;
7
+ if (i < 0) { it->finished = 1; return false; }
8
+ ++it->indices[i];
9
+ for (int j = i + 1; j < it->r; ++j)
10
+ it->indices[j] = it->indices[j-1] + 1;
11
+ return true;
12
+ }
@@ -0,0 +1,3 @@
1
+ static inline double complex_abs(Complex z) {
2
+ return hypot(z.re, z.im);
3
+ }
@@ -0,0 +1,3 @@
1
+ static inline Complex complex_add(Complex a, Complex b) {
2
+ return complex_new(a.re + b.re, a.im + b.im);
3
+ }
@@ -0,0 +1,11 @@
1
+ static inline Complex complex_div(Complex a, Complex b) {
2
+ if (b.re == 0 && b.im == 0) exam_algo_fail("complex division by zero");
3
+ if (fabs(b.re) >= fabs(b.im)) {
4
+ double r = b.im / b.re;
5
+ double den = b.re + b.im * r;
6
+ return complex_new((a.re + a.im*r)/den, (a.im - a.re*r)/den);
7
+ }
8
+ double r = b.re / b.im;
9
+ double den = b.im + b.re * r;
10
+ return complex_new((a.re*r + a.im)/den, (a.im*r - a.re)/den);
11
+ }
@@ -0,0 +1,3 @@
1
+ static inline Complex complex_mul(Complex a, Complex b) {
2
+ return complex_new(a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re);
3
+ }