File tree Expand file tree Collapse file tree 2 files changed +64
-0
lines changed
Expand file tree Collapse file tree 2 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ #include "etl.h"
2+ #include <ctype.h>
3+ #include <stdlib.h>
4+
5+ int convert (const legacy_map * input , const size_t input_len , new_map * * output ) {
6+ size_t total_key = 0 , key_count = 0 ;
7+ for (size_t i = 0 ; i < input_len ; i ++ ) {
8+ legacy_map temp = input [i ];
9+ while (* temp .keys != '\0' ) {
10+ total_key ++ ;
11+ temp .keys ++ ;
12+ }
13+ }
14+ if (total_key == 0 ) {
15+ * output = NULL ;
16+ return 0 ;
17+ }
18+
19+ * output = malloc (total_key * sizeof (new_map ));
20+ if (* output == NULL ) {
21+ return 0 ;
22+ }
23+ for (size_t i = 0 ; i < input_len ; i ++ ) {
24+ legacy_map temp = input [i ];
25+ while (* temp .keys != '\0' ) {
26+ (* output )[key_count ].key = tolower (* temp .keys );
27+ (* output )[key_count ++ ].value = temp .value ;
28+ temp .keys ++ ;
29+ }
30+ }
31+ for (size_t i = 0 ; i < key_count - 1 ; i ++ ) {
32+ int swapped = 0 ;
33+ for (size_t j = 0 ; j < key_count - i - 1 ; j ++ ) {
34+ if ((* output )[j ].key > (* output )[j + 1 ].key ) {
35+ new_map temp = (* output )[j ];
36+ (* output )[j ] = (* output )[j + 1 ];
37+ (* output )[j + 1 ] = temp ;
38+ swapped = 1 ;
39+ }
40+ }
41+ if (swapped == 0 ) {
42+ break ;
43+ }
44+ }
45+ return key_count ;
46+ }
Original file line number Diff line number Diff line change 1+ #ifndef ETL_H
2+ #define ETL_H
3+
4+ #include <stddef.h>
5+
6+ typedef struct {
7+ int value ;
8+ const char * keys ;
9+ } legacy_map ;
10+
11+ typedef struct {
12+ char key ;
13+ int value ;
14+ } new_map ;
15+
16+ int convert (const legacy_map * input , const size_t input_len , new_map * * output );
17+
18+ #endif
You can’t perform that action at this time.
0 commit comments