Skip to content

Commit

Permalink
change examples of algorithm_replace_if and remove_copy_if, replace i…
Browse files Browse the repository at this point in the history
…n algorithm
  • Loading branch information
KaisenAmin committed Feb 4, 2024
1 parent e538495 commit 9670d0c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 75 deletions.
92 changes: 22 additions & 70 deletions algorithm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4496,47 +4496,34 @@ int main() {
## Example 137: Using `algorithm_replace`
```c
#include <stdio.h>
#include <string.h>
#include "algorithm/algorithm.h"
#include "fmt/fmt.h"
typedef int (*CompareFunc)(const void *, const void *);
int compare_ints(const void *a, const void *b) {
int num1 = *(int *)a;
int num2 = *(int *)b;
return num1 - num2;
}
void algorithm_replace(void *base, size_t num, size_t size, const void *old_val, const void *new_val, CompareFunc comp){
if (!base || !old_val || !new_val || !comp || size == 0 || num == 0) return;
char *ptr = (char *)base;
for (size_t i = 0; i < num; ++i) {
if (comp(ptr + i * size, old_val) == 0) {
memcpy(ptr + i * size, new_val, size);
}
}
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 5, 5, 8, 9, 10};
int old_val = 5;
int new_val = -1;
printf("Before replacement:\n");
fmt_printf("Before replacement:\n");
for (int i = 0; i < 10; ++i) {
printf("%d ", arr[i]);
fmt_printf("%d ", arr[i]);
}
printf("\n");
fmt_printf("\n");
algorithm_replace(arr, 10, sizeof(int), &old_val, &new_val, compare_ints);
printf("After replacement:\n");
fmt_printf("After replacement:\n");
for (int i = 0; i < 10; ++i) {
printf("%d ", arr[i]);
fmt_printf("%d ", arr[i]);
}
printf("\n");
fmt_printf("\n");
return 0;
}
Expand All @@ -4546,46 +4533,32 @@ int main() {
## Example 138: Using `algorithm_replace_if`

```c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "algorithm/algorithm.h"
#include "fmt/fmt.h"

typedef bool (*BoolPredicateFunc)(const void *);

bool is_even(const void *n) {
int num = *(int *)n;
return num % 2 == 0;
}

void algorithm_replace_if(void *base, size_t num, size_t size, const void *new_val, BoolPredicateFunc pred) {
if (!base || !new_val || !pred || size == 0 || num == 0) return;

char *ptr = (char *)base;

for (size_t i = 0; i < num; ++i) {
if (pred(ptr + i * size)) {
memcpy(ptr + i * size, new_val, size);
}
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int new_val = -1;

printf("Before replacement:\n");
fmt_printf("Before replacement:\n");
for (int i = 0; i < 10; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
fmt_printf("\n");

algorithm_replace_if(arr, 10, sizeof(int), &new_val, is_even);

printf("After replacement:\n");
fmt_printf("After replacement:\n");
for (int i = 0; i < 10; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
fmt_printf("\n");

return 0;
}
Expand All @@ -4595,52 +4568,31 @@ int main() {
## Example 139: Using `algorithm_remove_copy_if`
```c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
typedef bool (*BoolPredicateFunc)(const void *);
#include "algorithm/algorithm.h"
#include "fmt/fmt.h"
bool is_even(const void *n) {
int num = *(int *)n;
return num % 2 == 0;
}
size_t algorithm_remove_copy_if(const void *source, size_t num, size_t size, void *result, BoolPredicateFunc pred) {
if (!source || !result || !pred || size == 0 || num == 0) return 0;
const char *src = (const char *)source;
char *dst = (char *)result;
size_t count = 0;
for (size_t i = 0; i < num; ++i) {
if (!pred(src + i * size)) {
memcpy(dst, src + i * size, size);
dst += size;
count++;
}
}
return count;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int result[10] = {0};
printf("Source array:\n");
fmt_printf("Source array:\n");
for (int i = 0; i < 10; ++i) {
printf("%d ", arr[i]);
fmt_printf("%d ", arr[i]);
}
printf("\n");
fmt_printf("\n");
size_t count = algorithm_remove_copy_if(arr, 10, sizeof(int), result, is_even);
printf("Result array:\n");
fmt_printf("Result array:\n");
for (size_t i = 0; i < count; ++i) {
printf("%d ", result[i]);
fmt_printf("%d ", result[i]);
}
printf("\n");
fmt_printf("\n");
return 0;
}
Expand Down
12 changes: 9 additions & 3 deletions algorithm/algorithm.c
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,9 @@ void algorithm_remove_copy(const void *source, size_t num, size_t size, void *re
* @return The number of elements copied to the result array.
*/
size_t algorithm_remove_copy_if(const void *source, size_t num, size_t size, void *result, BoolPredicateFunc pred) {
if (!source || !result || !pred || size == 0 || num == 0) return 0;
if (!source || !result || !pred || size == 0 || num == 0) {
return 0;
}

const char *src = (const char *)source;
char *dst = (char *)result;
Expand Down Expand Up @@ -951,7 +953,9 @@ size_t algorithm_remove_copy_if(const void *source, size_t num, size_t size, voi
* @param comp Comparison function used to compare elements.
*/
void algorithm_replace(void *base, size_t num, size_t size, const void *old_val, const void *new_val, CompareFunc comp){
if (!base || !old_val || !new_val || !comp || size == 0 || num == 0) return;
if (!base || !old_val || !new_val || !comp || size == 0 || num == 0) {
return;
}

char *ptr = (char *)base;

Expand All @@ -977,7 +981,9 @@ void algorithm_replace(void *base, size_t num, size_t size, const void *old_val,
* @return None.
*/
void algorithm_replace_if(void *base, size_t num, size_t size, const void *new_val, BoolPredicateFunc pred) {
if (!base || !new_val || !pred || size == 0 || num == 0) return;
if (!base || !new_val || !pred || size == 0 || num == 0) {
return;
}

char *ptr = (char *)base;

Expand Down
3 changes: 1 addition & 2 deletions algorithm/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ void algorithm_swap_ranges(void *first1, void *first2, size_t num, size_t size);
void algorithm_rotate(void *first, void *middle, void *last, size_t size);
void algorithm_rotate_copy(const void *first, const void *middle, const void *last, size_t size, void *result);
void algorithm_remove_copy(const void *source, size_t num, size_t size, void *result, const void *val, CompareFunc comp);
size_t algorithm_remove_copy_if(const void *source, size_t num, size_t size, void *result, BoolPredicateFunc pred);
void algorithm_replace(void *base, size_t num, size_t size, const void *old_val, const void *new_val, CompareFunc comp);
void algorithm_replace_if(void *base, size_t num, size_t size, const void *val, BoolPredicateFunc pred);

Expand Down Expand Up @@ -73,7 +72,7 @@ size_t algorithm_unique(void *base, size_t num, size_t size, CompareFunc comp);
size_t algorithm_count(const void *base, size_t num, size_t size, const void *val, CompareFunc comp);
size_t algorithm_count_if(const void *base, size_t num, size_t size, BoolPredicateFunc pred);
size_t algorithm_unique_copy(const void *first, size_t num, size_t size, void *result, CompareFunc comp);

size_t algorithm_remove_copy_if(const void *source, size_t num, size_t size, void *result, BoolPredicateFunc pred);
Pair algorithm_equal_range(const void *base, size_t num, size_t size, const void *val, CompareFunc comp);
Pair algorithm_mismatch(const void *base1, size_t num1, size_t size1, const void *base2, size_t num2, size_t size2, CompareFuncBool comp);

Expand Down

0 comments on commit 9670d0c

Please sign in to comment.