Skip to content

Commit

Permalink
Renamed i_more => i_retain (avoids undef of template parameters o…
Browse files Browse the repository at this point in the history
…n next STC container inclusion).
  • Loading branch information
tylov committed Jun 20, 2023
1 parent de4f8fa commit 3fed667
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/ccommon_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ int main() {
}
```
Containers with random access may also be sorted. Even sorting cdeq/cqueue (with ring buffer) is
possible and very fast. Note that `i_more` must be defined to pick up template parameters from the container:
possible and very fast. Note that `i_retain` must be defined to retain specified template parameters for use by sort:
```c
#define i_type MyDeq
#define i_val int
#define i_more
#define i_retain
#include <stc/cdeq.h> // deque
#include <stc/algo/sort.h>
#include <stdio.h>
Expand Down
23 changes: 21 additions & 2 deletions include/stc/algo/sort.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,39 @@ template params:
#define i_less - less function. default: *x < *y
#define i_type name - define {{name}}_sort_n(), else {{i_val}}array_sort_n().
// test:
// ex1:
#include <stdio.h>
#define i_val int
#include <stc/algo/sort.h>
int main() {
int nums[] = {23, 321, 5434, 25, 245, 1, 654, 33, 543, 21};
intarray_sort_n(nums, c_arraylen(arr));
intarray_sort_n(nums, c_arraylen(nums));
for (int i = 0; i < c_arraylen(nums); i++)
printf(" %d", nums[i]);
puts("");
}
// ex2:
#define i_val int
#define i_type IDeq
#define i_retain // retain input template params to be reused by sort.h
#include <stc/cdeq.h>
#include <stc/algo/sort.h>
int main() {
IDeq nums = c_init(IDeq, {5434, 25, 245, 1, 654, 33, 543, 21});
IDeq_push_front(&nums, 23);
IDeq_push_front(&nums, 321);
IDeq_sort_n(&nums, IDeq_size(&nums));
c_foreach (i, IDeq, nums)
printf(" %d", *i.ref);
IDeq_drop(&nums);
}
*/
#include "../ccommon.h"

Expand Down
6 changes: 3 additions & 3 deletions include/stc/cdeq.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
#define _i_prefix cdeq_
#define _pop _pop_front
#define _pull _pull_front
#ifdef i_more
#ifdef i_retain
#include "cqueue.h"
#define i_more
#define i_retain
#else
#define i_more
#define i_retain
#include "cqueue.h"
#endif
#undef _pop
Expand Down
4 changes: 2 additions & 2 deletions include/stc/priv/template2.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifdef i_more
#undef i_more
#ifdef i_retain
#undef i_retain
#else
#undef i_type
#undef i_tag
Expand Down
2 changes: 1 addition & 1 deletion misc/benchmarks/various/csort_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define NDEBUG
#define i_type Ints
#define i_val int
#define i_more
#define i_retain
#include <stc/cvec.h>
#include <stc/algo/sort.h>

Expand Down

0 comments on commit 3fed667

Please sign in to comment.