@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.
- package/LICENSE +18 -0
- package/README.md +19 -2
- package/package.json +25 -4
- package/preset/components/BigInt.h +5 -0
- package/preset/components/Combination.h +4 -0
- package/preset/components/Complex.h +3 -0
- package/preset/components/Date.h +1 -0
- package/preset/components/EXAM_BIGINT_CAP.h +3 -0
- package/preset/components/EXAM_ENUM_CAP.h +3 -0
- package/preset/components/ExamIntIndexPair.h +4 -0
- package/preset/components/Permutation.h +5 -0
- package/preset/components/Record.h +1 -0
- package/preset/components/Time.h +1 -0
- package/preset/components/argmax_doubles.h +7 -0
- package/preset/components/argmax_ints.h +6 -0
- package/preset/components/argmax_lls.h +6 -0
- package/preset/components/argmin_doubles.h +7 -0
- package/preset/components/argmin_ints.h +6 -0
- package/preset/components/argmin_lls.h +6 -0
- package/preset/components/argsort_ints.h +2 -0
- package/preset/components/argsort_ints_into.h +19 -0
- package/preset/components/base_to_int.h +20 -0
- package/preset/components/bigint_add.h +22 -0
- package/preset/components/bigint_compare.h +6 -0
- package/preset/components/bigint_div_small.h +19 -0
- package/preset/components/bigint_divmod.h +34 -0
- package/preset/components/bigint_factorial.h +10 -0
- package/preset/components/bigint_from_ll.h +16 -0
- package/preset/components/bigint_from_str.h +19 -0
- package/preset/components/bigint_mul.h +25 -0
- package/preset/components/bigint_mul_small.h +20 -0
- package/preset/components/bigint_print.h +7 -0
- package/preset/components/bigint_sub.h +7 -0
- package/preset/components/bigint_to_str.h +2 -0
- package/preset/components/bigint_to_str_into.h +10 -0
- package/preset/components/bigint_zero.h +5 -0
- package/preset/components/binary_search_doubles.h +4 -0
- package/preset/components/binary_search_ints.h +4 -0
- package/preset/components/binary_search_lls.h +5 -0
- package/preset/components/bisect_root.h +23 -0
- package/preset/components/bit_clear.h +3 -0
- package/preset/components/bit_count.h +5 -0
- package/preset/components/bit_get.h +3 -0
- package/preset/components/bit_highest_set_index.h +6 -0
- package/preset/components/bit_lowest_set_index.h +6 -0
- package/preset/components/bit_rotate_left.h +5 -0
- package/preset/components/bit_set.h +3 -0
- package/preset/components/bit_toggle.h +3 -0
- package/preset/components/bubble_sort_ints.h +11 -0
- package/preset/components/build_diff_ints.h +2 -0
- package/preset/components/build_diff_ints_into.h +8 -0
- package/preset/components/build_prefix_ints.h +2 -0
- package/preset/components/build_prefix_ints_into.h +7 -0
- package/preset/components/combination_new.h +8 -0
- package/preset/components/combination_next.h +12 -0
- package/preset/components/complex_abs.h +3 -0
- package/preset/components/complex_add.h +3 -0
- package/preset/components/complex_div.h +11 -0
- package/preset/components/complex_mul.h +3 -0
- package/preset/components/complex_new.h +4 -0
- package/preset/components/complex_print.h +5 -0
- package/preset/components/complex_sub.h +3 -0
- package/preset/components/copy_doubles.h +1 -0
- package/preset/components/copy_doubles_into.h +6 -0
- package/preset/components/copy_ints.h +2 -0
- package/preset/components/copy_ints_into.h +6 -0
- package/preset/components/copy_lls.h +1 -0
- package/preset/components/copy_lls_into.h +5 -0
- package/preset/components/count_doubles.h +6 -0
- package/preset/components/count_ints.h +6 -0
- package/preset/components/count_lls.h +6 -0
- package/preset/components/date_add_days.h +6 -0
- package/preset/components/date_compare.h +4 -0
- package/preset/components/date_day_of_year.h +6 -0
- package/preset/components/date_days_in_month.h +5 -0
- package/preset/components/date_diff.h +3 -0
- package/preset/components/date_from_serial.h +16 -0
- package/preset/components/date_is_leap.h +3 -0
- package/preset/components/date_new.h +5 -0
- package/preset/components/date_print.h +5 -0
- package/preset/components/date_to_serial.h +3 -0
- package/preset/components/date_valid.h +4 -0
- package/preset/components/diff_range_add.h +8 -0
- package/preset/components/diff_to_lls.h +2 -0
- package/preset/components/diff_to_lls_into.h +10 -0
- package/preset/components/digits_to_int.h +12 -0
- package/preset/components/exam_abs_ull.h +3 -0
- package/preset/components/exam_add_checked_ll.h +6 -0
- package/preset/components/exam_add_ll.h +6 -0
- package/preset/components/exam_algo_fail.h +4 -0
- package/preset/components/exam_bigint_add_abs.h +19 -0
- package/preset/components/exam_bigint_cmp_abs.h +6 -0
- package/preset/components/exam_bigint_normalize.h +5 -0
- package/preset/components/exam_bigint_sub_abs.h +14 -0
- package/preset/components/exam_bigint_sub_abs_inplace.h +10 -0
- package/preset/components/exam_bit_check.h +4 -0
- package/preset/components/exam_check_n.h +3 -0
- package/preset/components/exam_cmp_double.h +4 -0
- package/preset/components/exam_cmp_double_desc.h +4 -0
- package/preset/components/exam_cmp_int_asc.h +4 -0
- package/preset/components/exam_cmp_int_desc.h +4 -0
- package/preset/components/exam_cmp_ll.h +4 -0
- package/preset/components/exam_cmp_ll_desc.h +4 -0
- package/preset/components/exam_cmp_unsigned_char.h +5 -0
- package/preset/components/exam_date_start_of_year.h +4 -0
- package/preset/components/exam_digit_value.h +6 -0
- package/preset/components/exam_doubles_finite.h +5 -0
- package/preset/components/exam_int_index_compare.h +7 -0
- package/preset/components/exam_io_fail.h +4 -0
- package/preset/components/exam_io_get.h +5 -0
- package/preset/components/exam_io_line_buffer.h +19 -0
- package/preset/components/exam_io_partial_line.h +4 -0
- package/preset/components/exam_io_skip_current_line.h +9 -0
- package/preset/components/exam_io_space.h +3 -0
- package/preset/components/exam_io_token.h +17 -0
- package/preset/components/exam_io_unget.h +4 -0
- package/preset/components/exam_io_write.h +3 -0
- package/preset/components/exam_mod_add.h +5 -0
- package/preset/components/exam_mod_mul.h +11 -0
- package/preset/components/exam_mul_ll.h +8 -0
- package/preset/components/exam_record_qsort_cmp.h +3 -0
- package/preset/components/exam_sort_records.h +8 -0
- package/preset/components/exam_split_store.h +10 -0
- package/preset/components/exam_sub_checked_ll.h +6 -0
- package/preset/components/factorial_basic.h +6 -0
- package/preset/components/fill_doubles.h +1 -0
- package/preset/components/fill_doubles_into.h +6 -0
- package/preset/components/fill_ints.h +2 -0
- package/preset/components/fill_ints_into.h +5 -0
- package/preset/components/fill_lls.h +1 -0
- package/preset/components/fill_lls_into.h +5 -0
- package/preset/components/find_doubles.h +5 -0
- package/preset/components/find_ints.h +3 -0
- package/preset/components/find_lls.h +5 -0
- package/preset/components/first_true_int.h +13 -0
- package/preset/components/frequency_ints.h +3 -0
- package/preset/components/frequency_ints_into.h +15 -0
- package/preset/components/gcd.h +5 -0
- package/preset/components/int_digit_at.h +7 -0
- package/preset/components/int_digit_count.h +7 -0
- package/preset/components/int_digit_sum.h +7 -0
- package/preset/components/int_pow.h +9 -0
- package/preset/components/int_reverse_digits.h +15 -0
- package/preset/components/int_to_base.h +2 -0
- package/preset/components/int_to_base_into.h +19 -0
- package/preset/components/int_to_digits.h +2 -0
- package/preset/components/int_to_digits_into.h +13 -0
- package/preset/components/is_power_of_two.h +3 -0
- package/preset/components/is_prime.h +7 -0
- package/preset/components/lcm.h +7 -0
- package/preset/components/linear_search_ints.h +5 -0
- package/preset/components/longest_equal_run_ints.h +10 -0
- package/preset/components/longest_plus_one_run_ints.h +10 -0
- package/preset/components/lower_bound_doubles.h +9 -0
- package/preset/components/lower_bound_ints.h +10 -0
- package/preset/components/lower_bound_lls.h +9 -0
- package/preset/components/max_doubles.h +7 -0
- package/preset/components/max_forward_diff_ints.h +11 -0
- package/preset/components/max_ints.h +6 -0
- package/preset/components/max_lls.h +6 -0
- package/preset/components/max_subarray_sum_ints.h +9 -0
- package/preset/components/mean_doubles.h +7 -0
- package/preset/components/mean_ints.h +4 -0
- package/preset/components/mean_lls.h +6 -0
- package/preset/components/median_doubles.h +15 -0
- package/preset/components/median_ints.h +14 -0
- package/preset/components/median_lls.h +14 -0
- package/preset/components/min_doubles.h +7 -0
- package/preset/components/min_ints.h +6 -0
- package/preset/components/min_lls.h +6 -0
- package/preset/components/mod_pow.h +14 -0
- package/preset/components/nearly_equal.h +11 -0
- package/preset/components/permutation_new.h +9 -0
- package/preset/components/permutation_next.h +25 -0
- package/preset/components/poly_eval_lls.h +10 -0
- package/preset/components/prefix_range_sum.h +5 -0
- package/preset/components/print_char.h +4 -0
- package/preset/components/print_double.h +5 -0
- package/preset/components/print_doubles.h +9 -0
- package/preset/components/print_int.h +4 -0
- package/preset/components/print_ints.h +9 -0
- package/preset/components/print_ll.h +4 -0
- package/preset/components/print_lls.h +9 -0
- package/preset/components/print_text.h +4 -0
- package/preset/components/range_ints.h +2 -0
- package/preset/components/range_ints_into.h +14 -0
- package/preset/components/range_lls.h +3 -0
- package/preset/components/range_lls_into.h +17 -0
- package/preset/components/read_bigint.h +3 -0
- package/preset/components/read_char.h +5 -0
- package/preset/components/read_char_raw.h +5 -0
- package/preset/components/read_double.h +5 -0
- package/preset/components/read_doubles.h +2 -0
- package/preset/components/read_doubles_into.h +6 -0
- package/preset/components/read_int.h +5 -0
- package/preset/components/read_ints.h +2 -0
- package/preset/components/read_ints_into.h +6 -0
- package/preset/components/read_line.h +1 -0
- package/preset/components/read_line_into.h +4 -0
- package/preset/components/read_ll.h +5 -0
- package/preset/components/read_lls.h +2 -0
- package/preset/components/read_lls_into.h +6 -0
- package/preset/components/read_rest.h +1 -0
- package/preset/components/read_rest_into.h +4 -0
- package/preset/components/read_word.h +1 -0
- package/preset/components/read_word_into.h +4 -0
- package/preset/components/record_cmp.h +1 -0
- package/preset/components/reverse_doubles.h +6 -0
- package/preset/components/reverse_ints.h +8 -0
- package/preset/components/reverse_lls.h +6 -0
- package/preset/components/round_digits.h +8 -0
- package/preset/components/sieve_primes.h +2 -0
- package/preset/components/sieve_primes_into.h +13 -0
- package/preset/components/skip_line.h +3 -0
- package/preset/components/sort_doubles.h +4 -0
- package/preset/components/sort_doubles_desc.h +4 -0
- package/preset/components/sort_ints.h +4 -0
- package/preset/components/sort_ints_desc.h +4 -0
- package/preset/components/sort_lls.h +4 -0
- package/preset/components/sort_lls_desc.h +6 -0
- package/preset/components/sort_records.h +2 -0
- package/preset/components/stddev_doubles.h +3 -0
- package/preset/components/stddev_ints.h +3 -0
- package/preset/components/stddev_lls.h +3 -0
- package/preset/components/str_count_char.h +6 -0
- package/preset/components/str_count_substr.h +13 -0
- package/preset/components/str_erase.h +7 -0
- package/preset/components/str_insert.h +2 -0
- package/preset/components/str_insert_into.h +10 -0
- package/preset/components/str_is_palindrome.h +5 -0
- package/preset/components/str_join.h +4 -0
- package/preset/components/str_join_into.h +27 -0
- package/preset/components/str_longest_char_run.h +9 -0
- package/preset/components/str_lower.h +4 -0
- package/preset/components/str_remove_char.h +11 -0
- package/preset/components/str_replace_all.h +2 -0
- package/preset/components/str_replace_all_into.h +34 -0
- package/preset/components/str_replace_char.h +8 -0
- package/preset/components/str_reverse.h +6 -0
- package/preset/components/str_sort_chars.h +3 -0
- package/preset/components/str_split.h +3 -0
- package/preset/components/str_split_into.h +18 -0
- package/preset/components/str_split_whitespace.h +4 -0
- package/preset/components/str_split_whitespace_into.h +17 -0
- package/preset/components/str_trim.h +7 -0
- package/preset/components/str_unique_chars.h +10 -0
- package/preset/components/str_upper.h +4 -0
- package/preset/components/sum_doubles.h +7 -0
- package/preset/components/sum_ints.h +6 -0
- package/preset/components/sum_lls.h +11 -0
- package/preset/components/swap_ints.h +4 -0
- package/preset/components/time_add_seconds.h +5 -0
- package/preset/components/time_diff.h +3 -0
- package/preset/components/time_from_seconds.h +4 -0
- package/preset/components/time_new.h +5 -0
- package/preset/components/time_next_minute.h +3 -0
- package/preset/components/time_next_second.h +3 -0
- package/preset/components/time_print.h +5 -0
- package/preset/components/time_to_seconds.h +4 -0
- package/preset/components/time_valid.h +4 -0
- package/preset/components/try_read_bigint.h +7 -0
- package/preset/components/try_read_char.h +9 -0
- package/preset/components/try_read_char_raw.h +8 -0
- package/preset/components/try_read_double.h +12 -0
- package/preset/components/try_read_int.h +13 -0
- package/preset/components/try_read_line.h +1 -0
- package/preset/components/try_read_line_into.h +5 -0
- package/preset/components/try_read_ll.h +12 -0
- package/preset/components/try_read_rest.h +1 -0
- package/preset/components/try_read_rest_into.h +4 -0
- package/preset/components/try_read_word.h +1 -0
- package/preset/components/try_read_word_into.h +4 -0
- package/preset/components/unique_doubles.h +10 -0
- package/preset/components/unique_ints.h +11 -0
- package/preset/components/unique_lls.h +10 -0
- package/preset/components/unique_sorted_doubles.h +8 -0
- package/preset/components/unique_sorted_ints.h +8 -0
- package/preset/components/unique_sorted_lls.h +8 -0
- package/preset/components/upper_bound_doubles.h +9 -0
- package/preset/components/upper_bound_ints.h +10 -0
- package/preset/components/upper_bound_lls.h +9 -0
- package/preset/components/values_doubles.h +4 -0
- package/preset/components/values_doubles_into.h +9 -0
- package/preset/components/values_ints.h +3 -0
- package/preset/components/values_ints_into.h +7 -0
- package/preset/components/values_lls.h +4 -0
- package/preset/components/values_lls_into.h +9 -0
- package/preset/components/variance_doubles.h +14 -0
- package/preset/components/variance_ints.h +10 -0
- package/preset/components/variance_lls.h +11 -0
- 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
|
-
#
|
|
1
|
+
# @annouimo/c-exam-kit-preset (draft)
|
|
2
2
|
|
|
3
|
-
This
|
|
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.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
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 @@
|
|
|
1
|
+
typedef struct { int year, month, day; } Date;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
typedef struct Record Record;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
typedef struct { int hour, minute, second; } Time;
|
|
@@ -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,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,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,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,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,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,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,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
|
+
}
|