mongory 0.7.1 → 0.7.5

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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +2 -0
  3. data/README.md +34 -0
  4. data/Rakefile +28 -9
  5. data/ext/mongory_ext/mongory_ext.c +77 -29
  6. data/lib/mongory/matchers/in_matcher.rb +2 -2
  7. data/lib/mongory/matchers/nin_matcher.rb +2 -2
  8. data/lib/mongory/version.rb +1 -1
  9. data/lib/mongory.rb +4 -2
  10. metadata +7 -62
  11. data/ext/mongory_ext/mongory-core/LICENSE +0 -3
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -105
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -206
  14. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
  15. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -95
  16. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -108
  17. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
  18. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -76
  19. data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
  20. data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -246
  21. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -18
  22. data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -406
  23. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -30
  24. data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -30
  25. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -298
  26. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -65
  27. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -49
  28. data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -458
  29. data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -459
  30. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +0 -309
  31. data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.h +0 -47
  32. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -161
  33. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -115
  34. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -210
  35. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
  36. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -539
  37. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -125
  38. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c +0 -144
  39. data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.h +0 -48
  40. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -121
  41. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -46
  42. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -199
  43. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
  44. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +0 -334
  45. data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.h +0 -97
  46. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -198
  47. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -107
  48. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -50
  49. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -55
  50. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
  51. data/mongory.gemspec +0 -46
@@ -1,175 +0,0 @@
1
- #ifndef MONGORY_VALUE
2
- #define MONGORY_VALUE
3
-
4
- /**
5
- * @file value.h
6
- * @brief Defines the mongory_value structure, a generic value type for the
7
- * Mongory library.
8
- *
9
- * `mongory_value` is a tagged union that can represent various data types such
10
- * as null, boolean, integer, double, string, array, table, regex, pointers,
11
- * and an unsupported type. It includes functions for wrapping C types into
12
- * `mongory_value` objects, comparing values, and converting them to strings.
13
- */
14
-
15
- #include "mongory-core/foundations/memory_pool.h"
16
- #include <stdbool.h>
17
- #include <stdint.h> // For int64_t
18
-
19
- // Forward declarations for complex types that can be stored in mongory_value.
20
- struct mongory_array;
21
- struct mongory_table;
22
-
23
- // Forward declaration of mongory_value itself and its type enum.
24
- struct mongory_value;
25
- /**
26
- * @brief Alias for `struct mongory_value`.
27
- */
28
- typedef struct mongory_value mongory_value;
29
-
30
- enum mongory_type;
31
- /**
32
- * @brief Alias for `enum mongory_type`.
33
- */
34
- typedef enum mongory_type mongory_type;
35
-
36
- /**
37
- * @brief Function pointer type for comparing two mongory_value instances.
38
- *
39
- * @param a The first mongory_value.
40
- * @param b The second mongory_value.
41
- * @return int Returns:
42
- * - 0 if a is equal to b.
43
- * - A negative value if a is less than b.
44
- * - A positive value if a is greater than b.
45
- * - `mongory_value_compare_fail` (a specific constant) if the types
46
- * are incompatible for comparison or an error occurs.
47
- */
48
- typedef int (*mongory_value_compare_func)(mongory_value *a, mongory_value *b);
49
-
50
- /**
51
- * @brief Function pointer type for converting a mongory_value to a string representation.
52
- * @param value The mongory_value to convert.
53
- * @param pool The memory pool to allocate from.
54
- * @return char* A string literal representing the value, or NULL if allocation
55
- * fails. The string is a literal and should not be freed.
56
- */
57
- typedef char *(*mongory_value_to_str_func)(mongory_value *value, mongory_memory_pool *pool);
58
-
59
- /**
60
- * @brief Converts the type of a mongory_value to its string representation.
61
- * @param value A pointer to the mongory_value.
62
- * @return char* A string literal representing the type (e.g., "Int", "String").
63
- * Returns "UnknownType" if the type is not recognized. This string is a
64
- * literal and should not be freed.
65
- */
66
- char *mongory_type_to_string(mongory_value *value);
67
-
68
- /**
69
- * @brief Extracts a pointer to the raw data stored within a mongory_value.
70
- * The type of the returned pointer depends on the `mongory_value`'s type.
71
- * For example, for an MONGORY_TYPE_INT, it returns `int64_t*`.
72
- * @param value A pointer to the mongory_value.
73
- * @return void* A pointer to the internal data, or NULL if the type is unknown
74
- * or has no direct data pointer (e.g. MONGORY_TYPE_NULL).
75
- */
76
- void *mongory_value_extract(mongory_value *value);
77
-
78
- /** @name Mongory Value Wrapper Functions
79
- * Functions to create mongory_value instances from basic C types.
80
- * These functions allocate a new mongory_value from the provided pool.
81
- * @{
82
- */
83
- mongory_value *mongory_value_wrap_n(mongory_memory_pool *pool, void *n);
84
- mongory_value *mongory_value_wrap_b(mongory_memory_pool *pool, bool b);
85
- mongory_value *mongory_value_wrap_i(mongory_memory_pool *pool, int i);
86
- mongory_value *mongory_value_wrap_d(mongory_memory_pool *pool, double d);
87
- mongory_value *mongory_value_wrap_s(mongory_memory_pool *pool, char *s);
88
- mongory_value *mongory_value_wrap_a(mongory_memory_pool *pool, struct mongory_array *a);
89
- mongory_value *mongory_value_wrap_t(mongory_memory_pool *pool, struct mongory_table *t);
90
- mongory_value *mongory_value_wrap_regex(mongory_memory_pool *pool, void *regex);
91
- mongory_value *mongory_value_wrap_ptr(mongory_memory_pool *pool, void *ptr);
92
- mongory_value *mongory_value_wrap_u(mongory_memory_pool *pool, void *u);
93
- /** @} */
94
-
95
- /**
96
- * @def MONGORY_TYPE_MACRO
97
- * @brief X-Macro for defining mongory_type enum members and associated data.
98
- * Simplifies the definition of types, their string names, and corresponding
99
- * union fields.
100
- * _(ENUM_NAME, UNIQUE_NUM_SUFFIX, "STRING_NAME", UNION_FIELD_NAME)
101
- * Note: For MONGORY_TYPE_NULL, the 'i' field (int64_t) is arbitrarily used in
102
- * the macro as the union needs a field, but it's not meaningful for null.
103
- */
104
- #define MONGORY_TYPE_MACRO(_) \
105
- _(MONGORY_TYPE_NULL, 0, "Null", i) /* Field 'i' is arbitrary for NULL */ \
106
- _(MONGORY_TYPE_BOOL, 10, "Bool", b) \
107
- _(MONGORY_TYPE_INT, 11, "Int", i) \
108
- _(MONGORY_TYPE_DOUBLE, 12, "Double", d) \
109
- _(MONGORY_TYPE_STRING, 13, "String", s) \
110
- _(MONGORY_TYPE_ARRAY, 14, "Array", a) \
111
- _(MONGORY_TYPE_TABLE, 15, "Table", t) \
112
- _(MONGORY_TYPE_REGEX, 16, "Regex", regex) /* Custom regex object pointer */ \
113
- _(MONGORY_TYPE_POINTER, 17, "Pointer", ptr) /* Generic void pointer */ \
114
- _(MONGORY_TYPE_UNSUPPORTED, 999, "Unsupported", u) /* External/unknown type pointer */
115
-
116
- /**
117
- * @def MONGORY_ENUM_MAGIC
118
- * @brief A magic number used in generating enum values for mongory_type.
119
- * Helps in creating somewhat unique integer values for the enums.
120
- */
121
- #define MONGORY_ENUM_MAGIC 103
122
-
123
- /**
124
- * @enum mongory_type
125
- * @brief Enumerates the possible data types a mongory_value can hold.
126
- * Values are generated using MONGORY_TYPE_MACRO and MONGORY_ENUM_MAGIC.
127
- */
128
- enum mongory_type {
129
- #define DEFINE_ENUM(name, num, str, field) name = num * MONGORY_ENUM_MAGIC,
130
- MONGORY_TYPE_MACRO(DEFINE_ENUM)
131
- #undef DEFINE_ENUM
132
- };
133
-
134
- /**
135
- * @var mongory_value_compare_fail
136
- * @brief Special return value from compare functions indicating comparison
137
- * failure (e.g. incompatible types).
138
- */
139
- static const int mongory_value_compare_fail = 97;
140
-
141
- /**
142
- * @struct mongory_value
143
- * @brief Represents a generic value in the Mongory system.
144
- *
145
- * It's a tagged union, where `type` indicates which field in the `data` union
146
- * is active. Each value also carries a pointer to the `mongory_memory_pool`
147
- * it was allocated from (or is associated with) and a `comp` function for
148
- * comparisons. The `origin` field can be used to store a pointer to an
149
- * original external data structure if the `mongory_value` is a bridge or
150
- * wrapper.
151
- */
152
- struct mongory_value {
153
- mongory_memory_pool *pool; /**< Memory pool associated with this value. */
154
- mongory_type type; /**< The type of data stored in the union. */
155
- mongory_value_compare_func comp; /**< Function to compare this value with
156
- another. */
157
- mongory_value_to_str_func to_str; /**< Function to convert this value to a
158
- string representation. */
159
- union {
160
- bool b; /**< Boolean data. */
161
- int64_t i; /**< Integer data (64-bit). */
162
- double d; /**< Double-precision floating-point data. */
163
- char *s; /**< String data (null-terminated). String memory
164
- is typically managed by the pool. */
165
- struct mongory_array *a; /**< Pointer to a mongory_array. */
166
- struct mongory_table *t; /**< Pointer to a mongory_table. */
167
- void *regex; /**< Pointer to a custom regex object/structure. */
168
- void *ptr; /**< Generic void pointer for other data. */
169
- void *u; /**< Pointer for unsupported/external types. */
170
- } data; /**< Union holding the actual data based on type. */
171
- void *origin; /**< Optional pointer to an original external value, useful for
172
- bridging with other data systems. */
173
- };
174
-
175
- #endif /* MONGORY_VALUE */
@@ -1,76 +0,0 @@
1
- #ifndef MONGORY_MATCHER_H
2
- #define MONGORY_MATCHER_H
3
-
4
- /**
5
- * @file matcher.h
6
- * @brief Defines the core mongory_matcher structure and related types.
7
- *
8
- * This file provides the basic structure for all matchers in the Mongory
9
- * library. A matcher is responsible for determining if a given `mongory_value`
10
- * meets certain criteria defined by a `condition`.
11
- */
12
-
13
- #include "mongory-core/foundations/array.h" // For mongory_array (used in context)
14
- #include "mongory-core/foundations/memory_pool.h"
15
- #include "mongory-core/foundations/value.h"
16
-
17
- // Forward declaration for the main matcher structure.
18
- typedef struct mongory_matcher mongory_matcher;
19
- /**
20
- * @brief Creates a new generic matcher instance.
21
- *
22
- * This function typically serves as a high-level entry point for creating
23
- * matchers. In the current implementation, it delegates to
24
- * `mongory_matcher_table_cond_new`, implying that conditions are often
25
- * table-based (similar to query documents).
26
- *
27
- * @param pool The memory pool to use for the matcher's allocations.
28
- * @param condition A `mongory_value` representing the condition for this
29
- * matcher.
30
- * @return mongory_matcher* A pointer to the newly created matcher, or NULL on
31
- * failure.
32
- */
33
- mongory_matcher *mongory_matcher_new(mongory_memory_pool *pool, mongory_value *condition, void *extern_ctx);
34
-
35
- /**
36
- * @brief Matches a value against a matcher.
37
- * @param matcher The matcher to match against.
38
- * @param value The value to match.
39
- * @return True if the value matches the matcher, false otherwise.
40
- */
41
- bool mongory_matcher_match(mongory_matcher *matcher, mongory_value *value);
42
-
43
- /**
44
- * @brief Explains a matcher.
45
- * @param matcher The matcher to explain.
46
- * @param temp_pool The temporary pool to use for the explanation.
47
- */
48
- void mongory_matcher_explain(mongory_matcher *matcher, mongory_memory_pool *temp_pool);
49
-
50
- /**
51
- * @brief Traces a matcher.
52
- * @param matcher The matcher to trace.
53
- * @param value The value to trace.
54
- */
55
- bool mongory_matcher_trace(mongory_matcher *matcher, mongory_value *value);
56
-
57
- /**
58
- * @brief Prints the trace of a matcher.
59
- * @param matcher The matcher to print the trace of.
60
- */
61
- void mongory_matcher_print_trace(mongory_matcher *matcher);
62
-
63
- /**
64
- * @brief Enables the trace of a matcher.
65
- * @param matcher The matcher to enable the trace of.
66
- * @param temp_pool The temporary pool to use for the trace.
67
- */
68
- void mongory_matcher_enable_trace(mongory_matcher *matcher, mongory_memory_pool *temp_pool);
69
-
70
- /**
71
- * @brief Disables the trace of a matcher.
72
- * @param matcher The matcher to disable the trace of.
73
- */
74
- void mongory_matcher_disable_trace(mongory_matcher *matcher);
75
-
76
- #endif /* MONGORY_MATCHER_H */
@@ -1,12 +0,0 @@
1
- #ifndef MONGORY_CORE_H
2
- #define MONGORY_CORE_H
3
-
4
- #include "mongory-core/foundations/array.h"
5
- #include "mongory-core/foundations/config.h"
6
- #include "mongory-core/foundations/error.h"
7
- #include "mongory-core/foundations/memory_pool.h"
8
- #include "mongory-core/foundations/table.h"
9
- #include "mongory-core/foundations/value.h"
10
- #include "mongory-core/matchers/matcher.h"
11
-
12
- #endif
@@ -1,246 +0,0 @@
1
- /**
2
- * @file array.c
3
- * @brief Implements the mongory_array dynamic array.
4
- *
5
- * This file contains the internal implementation details for the mongory_array,
6
- * including memory management, resizing, and the core array operations.
7
- * It uses a private structure `mongory_array_private` which extends
8
- * `mongory_array` with capacity information and the actual item storage.
9
- */
10
-
11
- #include <string.h>
12
- #include "array_private.h"
13
- #include <mongory-core/foundations/array.h>
14
- #include <mongory-core/foundations/memory_pool.h>
15
- #include <mongory-core/foundations/value.h>
16
-
17
- /**
18
- * @def MONGORY_ARRAY_INIT_SIZE
19
- * @brief Initial capacity of a newly created mongory_array.
20
- */
21
- #define MONGORY_ARRAY_INIT_SIZE 4
22
-
23
- /**
24
- * @brief Resizes the internal storage of the array.
25
- *
26
- * Allocates a new block of memory for items and copies existing items to it.
27
- * The old item storage is not explicitly freed here as it's managed by the
28
- * memory pool; this function assumes the new allocation replaces the old one
29
- * in terms of where `internal->items` points.
30
- *
31
- * @param self Pointer to the mongory_array instance.
32
- * @param size The new capacity (number of elements) for the array.
33
- * @return true if resizing was successful, false otherwise (e.g., memory
34
- * allocation failure).
35
- */
36
- bool mongory_array_resize(mongory_array *self, size_t size) {
37
- mongory_array_private *internal = (mongory_array_private *)self;
38
-
39
- // Allocate a new, larger block of memory for the items.
40
- mongory_value **new_items = MG_ALLOC_ARY(self->pool, mongory_value*, size);
41
- if (!new_items) {
42
- // TODO: Propagate error via self->pool->error.
43
- return false;
44
- }
45
-
46
- // Copy existing item pointers to the new memory block.
47
- memcpy(new_items, internal->items, sizeof(mongory_value *) * self->count);
48
-
49
- internal->items = new_items;
50
- internal->capacity = size;
51
- return true;
52
- }
53
-
54
- /**
55
- * @brief Ensures the array has enough capacity for a given size, growing it if
56
- * necessary.
57
- *
58
- * If the required size is smaller than the current capacity, this function does
59
- * nothing. Otherwise, it calculates a new capacity (typically double the
60
- * current, or more if needed to fit `size`) and calls mongory_array_resize.
61
- *
62
- * @param self Pointer to the mongory_array instance.
63
- * @param size The minimum number of elements the array needs to accommodate.
64
- * @return true if the array has sufficient capacity (or was successfully
65
- * grown), false otherwise.
66
- */
67
- // ============================================================================
68
- // Static Helper Functions
69
- // ============================================================================
70
- static inline bool mongory_array_grow_if_needed(mongory_array *self, size_t size) {
71
- mongory_array_private *internal = (mongory_array_private *)self;
72
- if (size < internal->capacity) {
73
- return true;
74
- }
75
-
76
- // Calculate new capacity, typically doubling, until it's sufficient.
77
- size_t new_capacity = internal->capacity * 2;
78
- while (new_capacity <= size) {
79
- new_capacity *= 2;
80
- }
81
-
82
- return mongory_array_resize(self, new_capacity);
83
- }
84
-
85
- /**
86
- * @brief Internal implementation for iterating over array elements.
87
- * @param self Pointer to the mongory_array instance.
88
- * @param acc Accumulator/context passed to the callback.
89
- * @param func Callback function executed for each item.
90
- * @return true if iteration completed, false if callback stopped it.
91
- */
92
- static inline bool mongory_array_each(mongory_array *self, void *acc, mongory_array_callback_func func) {
93
- mongory_array_private *internal = (mongory_array_private *)self;
94
- for (size_t i = 0; i < self->count; i++) {
95
- mongory_value *item = internal->items[i];
96
- if (!func(item, acc)) {
97
- return false; // Callback requested to stop iteration.
98
- }
99
- }
100
- return true;
101
- }
102
-
103
- /**
104
- * @brief Internal implementation for adding an element to the end of the array.
105
- * Grows the array if necessary.
106
- * @param self Pointer to the mongory_array instance.
107
- * @param value The mongory_value to add.
108
- * @return true if successful, false on failure (e.g., memory allocation).
109
- */
110
- static inline bool mongory_array_push(mongory_array *self, mongory_value *value) {
111
- mongory_array_private *internal = (mongory_array_private *)self;
112
- // Ensure there's space for one more element (current count).
113
- if (!mongory_array_grow_if_needed(self, self->count)) {
114
- return false;
115
- }
116
-
117
- internal->items[self->count++] = value;
118
- return true;
119
- }
120
-
121
- /**
122
- * @brief Internal implementation for retrieving an element by index.
123
- * @param self Pointer to the mongory_array instance.
124
- * @param index The index of the element.
125
- * @return Pointer to the mongory_value, or NULL if index is out of bounds.
126
- */
127
- static inline mongory_value *mongory_array_get(mongory_array *self, size_t index) {
128
- mongory_array_private *internal = (mongory_array_private *)self;
129
- if (index >= self->count) {
130
- return NULL; // Index out of bounds.
131
- }
132
- return internal->items[index];
133
- }
134
-
135
- /**
136
- * @brief Internal implementation for setting an element at a specific index.
137
- *
138
- * Grows the array if necessary. If the index is beyond the current count,
139
- * intermediate elements are set to NULL, and the array's count is updated.
140
- *
141
- * @param self Pointer to the mongory_array instance.
142
- * @param index The index at which to set the value.
143
- * @param value The mongory_value to set.
144
- * @return true if successful, false on failure (e.g., memory allocation).
145
- */
146
- static inline bool mongory_array_set(mongory_array *self, size_t index, mongory_value *value) {
147
- mongory_array_private *internal = (mongory_array_private *)self;
148
- // Ensure capacity for the given index (index + 1 elements).
149
- if (!mongory_array_grow_if_needed(self, index + 1)) {
150
- return false;
151
- }
152
-
153
- // If setting beyond the current count, fill intermediate spots with NULL.
154
- if (index >= self->count) {
155
- for (size_t i = self->count; i < index; i++) {
156
- internal->items[i] = NULL;
157
- }
158
- self->count = index + 1; // Update count to include the new element.
159
- }
160
-
161
- internal->items[index] = value;
162
- return true;
163
- }
164
-
165
- /**
166
- * @brief Creates and initializes a new mongory_array.
167
- *
168
- * Allocates memory for the array structure itself and its initial item storage
169
- * using the provided memory pool. Assigns function pointers for array
170
- * operations.
171
- *
172
- * @param pool The memory pool to use for allocations.
173
- * @return Pointer to the new mongory_array, or NULL on allocation failure.
174
- */
175
- mongory_array *mongory_array_new(mongory_memory_pool *pool) {
176
- // First, allocate the private structure that holds all array metadata.
177
- mongory_array_private *internal = MG_ALLOC_PTR(pool, mongory_array_private);
178
- if (!internal) {
179
- // TODO: Propagate error via pool->error (if pool is not NULL)
180
- return NULL;
181
- }
182
-
183
- // Then, allocate the initial block of memory for the array items.
184
- mongory_value **items = MG_ALLOC_ARY(pool, mongory_value*, MONGORY_ARRAY_INIT_SIZE);
185
- if (!items) {
186
- // If this fails, the 'internal' struct allocated above will be cleaned up
187
- // by the pool, assuming it's a tracing pool.
188
- // TODO: Propagate error via pool->error (if pool is not NULL)
189
- return NULL;
190
- }
191
-
192
- // Initialize the public part of the array structure.
193
- internal->base.pool = pool;
194
- internal->base.count = 0;
195
- internal->base.each = mongory_array_each;
196
- internal->base.get = mongory_array_get;
197
- internal->base.push = mongory_array_push;
198
- internal->base.set = mongory_array_set;
199
-
200
- // Initialize the private part of the array structure.
201
- internal->items = items;
202
- internal->capacity = MONGORY_ARRAY_INIT_SIZE;
203
-
204
- return &internal->base; // Return pointer to the public structure.
205
- }
206
-
207
- static mongory_value** mongory_array_merge(mongory_memory_pool *pool, mongory_value **left, mongory_value **right, size_t count, void *ctx, size_t(*callback)(mongory_value *value, void *ctx)) {
208
- mongory_value **result = MG_ALLOC_ARY(pool, mongory_value*, count);
209
- size_t i = 0, j = 0, k = 0;
210
- while (i < count / 2 && j < count - count / 2) {
211
- if (callback(left[i], ctx) < callback(right[j], ctx)) {
212
- result[k++] = left[i++];
213
- } else {
214
- result[k++] = right[j++];
215
- }
216
- }
217
- while (i < count / 2) {
218
- result[k++] = left[i++];
219
- }
220
- while (j < count - count / 2) {
221
- result[k++] = right[j++];
222
- }
223
- return result;
224
- }
225
-
226
- static mongory_value** mongory_array_merge_sort(mongory_memory_pool *pool, mongory_value **origin_items, size_t count, void *ctx, size_t(*callback)(mongory_value *value, void *ctx)) {
227
- if (count <= 1) {
228
- return origin_items;
229
- }
230
- size_t mid = count / 2;
231
- mongory_value **left = mongory_array_merge_sort(pool, origin_items, mid, ctx, callback);
232
- mongory_value **right = mongory_array_merge_sort(pool, origin_items + mid, count - mid, ctx, callback);
233
- return mongory_array_merge(pool, left, right, count, ctx, callback);
234
- }
235
-
236
- mongory_array *mongory_array_sort_by(mongory_array *self, mongory_memory_pool *temp_pool, void *ctx, size_t(*callback)(mongory_value *value, void *ctx)) {
237
- mongory_array_private *internal = (mongory_array_private *)self;
238
-
239
- mongory_value **new_items = mongory_array_merge_sort(temp_pool, internal->items, self->count, ctx, callback);
240
- mongory_array_private *new_array = (mongory_array_private *)mongory_array_new(self->pool);
241
- new_array->capacity = internal->capacity;
242
- new_array->base.count = self->count;
243
- new_array->items = MG_ALLOC_ARY(self->pool, mongory_value*, new_array->capacity);
244
- memcpy(new_array->items, new_items, sizeof(mongory_value *) * self->count);
245
- return &new_array->base;
246
- }
@@ -1,18 +0,0 @@
1
- #ifndef MONGORY_ARRAY_PRIVATE_H
2
- #define MONGORY_ARRAY_PRIVATE_H
3
- #include "mongory-core/foundations/array.h"
4
- #include "mongory-core/foundations/memory_pool.h"
5
- #include "mongory-core/foundations/value.h"
6
- #include <stdbool.h>
7
-
8
- bool mongory_array_resize(mongory_array *self, size_t size); // resize array
9
-
10
- typedef struct mongory_array_private {
11
- mongory_array base; // public array
12
- mongory_value **items; // items pointer
13
- size_t capacity; // capacity
14
- } mongory_array_private;
15
-
16
- mongory_array *mongory_array_sort_by(mongory_array *self, mongory_memory_pool *temp_pool, void *ctx, size_t(*callback)(mongory_value *value, void *ctx));
17
-
18
- #endif // MONGORY_ARRAY_PRIVATE_H