@@ -28,7 +28,7 @@ int private_dtwResource_compare(const void *item1,const void*item2){
2828void DtwResource_map (DtwResource * self ,DtwResourceMapProps props ){
2929 //printf("%p\n",ordenation_callback);
3030 if (DtwResource_error (self )){
31- return ;;
31+ return ;
3232 }
3333
3434 DtwResourceArray * itens = NULL ;
@@ -42,91 +42,137 @@ void DtwResource_map(DtwResource *self,DtwResourceMapProps props){
4242
4343 privateDtwResource_map_element * * mapped_elements = NULL ;
4444 int total_mapped_elements = 0 ;
45+
46+ // Perform ordering before the main loop
4547 if (props .ordenation_callback ) {
4648 mapped_elements = (privateDtwResource_map_element * * )malloc (
4749 (itens -> size + 1 ) * sizeof (privateDtwResource_map_element * * )
48- );
50+ );
51+
52+ // First, collect all elements for ordering
53+ for (int i = 0 ; i < itens -> size ; i ++ ){
54+ DtwResource * current = itens -> resources [i ];
55+
56+ if (props .filtrage_callback ){
57+ bool result = props .filtrage_callback (current , props .args );
58+ if (DtwResource_error (self )){
59+ return ;
60+ }
61+ if (!result ){
62+ continue ;
63+ }
64+ }
65+
66+ privateDtwResource_map_element * created = (privateDtwResource_map_element * )malloc (sizeof (privateDtwResource_map_element ));
67+ * created = (privateDtwResource_map_element ){0 };
68+ created -> current = current ;
69+ created -> ordenation_callback = props .ordenation_callback ;
70+ created -> args = props .args ;
71+ mapped_elements [total_mapped_elements ] = created ;
72+ total_mapped_elements ++ ;
73+ }
74+
75+ // Sort the elements
76+ qsort (
77+ mapped_elements ,
78+ total_mapped_elements ,
79+ sizeof (privateDtwResource_map_element * ),
80+ private_dtwResource_compare
81+ );
4982 }
5083
5184 int total = 0 ;
5285 int total_skipded = 0 ;
53- for (int i = 0 ; i < itens -> size ; i ++ ){
54-
55- DtwResource * current = itens -> resources [i ];
56-
57- if (props .filtrage_callback ){
58- bool result = props .filtrage_callback (current , props .args );
86+
87+ if (props .ordenation_callback ) {
88+ // Process sorted elements
89+ for (int i = 0 ; i < total_mapped_elements ; i ++ ){
90+ if (i < props .start ){
91+ continue ;
92+ }
93+
94+ if (total >= props .qtd && props .qtd != -1 ){
95+ break ;
96+ }
97+
98+ privateDtwResource_map_element * element = mapped_elements [i ];
99+ void * result = props .callback (element -> current , props .args );
100+
59101 if (DtwResource_error (self )){
60- return ;;
102+ if (result ){
103+ props .append (props .main_array , result );
104+ }
105+ // Clean up
106+ for (int j = i ; j < total_mapped_elements ; j ++ ){
107+ free (mapped_elements [j ]);
108+ }
109+ free (mapped_elements );
110+ return ;
61111 }
62- if (!result ){
63- continue ;
112+
113+ if (result != NULL ){
114+ props .append (props .main_array , result );
115+ if (DtwResource_error (self )){
116+ // Clean up
117+ for (int j = i ; j < total_mapped_elements ; j ++ ){
118+ free (mapped_elements [j ]);
119+ }
120+ free (mapped_elements );
121+ return ;
122+ }
123+ total ++ ;
64124 }
65125 }
66-
67- total_skipded ++ ;
68-
69- if (total_skipded <= props .start ){
70- continue ;
126+
127+ // Clean up
128+ for (int i = 0 ; i < total_mapped_elements ; i ++ ){
129+ free (mapped_elements [i ]);
71130 }
131+ free (mapped_elements );
132+ }
133+ else {
134+ // Original logic for non-sorted processing
135+ for (int i = 0 ; i < itens -> size ; i ++ ){
136+ DtwResource * current = itens -> resources [i ];
137+
138+ if (props .filtrage_callback ){
139+ bool result = props .filtrage_callback (current , props .args );
140+ if (DtwResource_error (self )){
141+ return ;
142+ }
143+ if (!result ){
144+ continue ;
145+ }
146+ }
72147
73- if (total + 1 > props .qtd && props .qtd != -1 ){
74- break ;
75- }
148+ total_skipded ++ ;
76149
77- void * result = props .callback (current , props .args );
78- if (DtwResource_error (self )){
79- if (result ){
80- props .append (props .main_array ,result );
150+ if (total_skipded <= props .start ){
151+ continue ;
81152 }
82- return ;
83- }
84- if (result == NULL ){
85- continue ;
86- }
87- total += 1 ;
88153
89- if (props .ordenation_callback == NULL ) {
154+ if (total + 1 > props .qtd && props .qtd != -1 ){
155+ break ;
156+ }
157+
158+ void * result = props .callback (current , props .args );
159+ if (DtwResource_error (self )){
160+ if (result ){
161+ props .append (props .main_array ,result );
162+ }
163+ return ;
164+ }
165+ if (result == NULL ){
166+ continue ;
167+ }
168+
90169 props .append (props .main_array ,result );
91170 if (DtwResource_error (self )){
92- return ;;
171+ return ;
93172 }
173+ total ++ ;
94174 }
95-
96- if (props .ordenation_callback ){
97- privateDtwResource_map_element * created = (privateDtwResource_map_element * )malloc (sizeof (privateDtwResource_map_element ));
98- * created = (privateDtwResource_map_element ){0 };
99- created -> result = result ;
100- created -> current = current ;
101- created -> ordenation_callback = props .ordenation_callback ;
102- created -> args = props .args ;
103- // printf("criado %p\n",created);
104- mapped_elements [total_mapped_elements ] = created ;
105- total_mapped_elements += 1 ;
106- }
107-
108- }
109-
110- if (props .ordenation_callback ) {
111-
112- qsort (
113- mapped_elements ,
114- total_mapped_elements ,
115- sizeof (privateDtwResource_map_element * ),
116- private_dtwResource_compare
117- );
118- if (DtwResource_error (self )){
119- return ;;
120- }
121- for (int i = 0 ; i < total_mapped_elements ; i ++ ) {
122- privateDtwResource_map_element * current = mapped_elements [i ];
123- props .append (props .main_array ,current -> result );
124- free (current );
125- }
126- free (mapped_elements );
127175 }
128-
129-
130176}
131177void DtwResource_schema_map (DtwResource * self ,DtwResourceMapProps props ){
132178
0 commit comments