1 | /* Copyright Vladimir Prus 2003. Distributed under the Boost */ |
---|
2 | /* Software License, Version 1.0. (See accompanying */ |
---|
3 | /* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) */ |
---|
4 | |
---|
5 | #include "../native.h" |
---|
6 | |
---|
7 | # ifndef max |
---|
8 | # define max( a,b ) ((a)>(b)?(a):(b)) |
---|
9 | # endif |
---|
10 | |
---|
11 | |
---|
12 | LIST *sequence_select_highest_ranked( PARSE *parse, FRAME *frame ) |
---|
13 | { |
---|
14 | /* Returns all of 'elements' for which corresponding element in parallel */ |
---|
15 | /* list 'rank' is equal to the maximum value in 'rank'. */ |
---|
16 | |
---|
17 | LIST* elements = lol_get( frame->args, 0 ); |
---|
18 | LIST* rank = lol_get( frame->args, 1 ); |
---|
19 | |
---|
20 | LIST* result = 0; |
---|
21 | LIST* tmp; |
---|
22 | int highest_rank = -1; |
---|
23 | |
---|
24 | for (tmp = rank; tmp; tmp = tmp->next) |
---|
25 | highest_rank = max(highest_rank, atoi(tmp->string)); |
---|
26 | |
---|
27 | for (; rank; rank = rank->next, elements = elements->next) |
---|
28 | if (atoi(rank->string) == highest_rank) |
---|
29 | result = list_new(result, elements->string); |
---|
30 | |
---|
31 | return result; |
---|
32 | } |
---|
33 | |
---|
34 | void init_sequence() |
---|
35 | { |
---|
36 | { |
---|
37 | char* args[] = { "elements", "*", ":", "rank", "*", 0 }; |
---|
38 | declare_native_rule("sequence", "select-highest-ranked", args, |
---|
39 | sequence_select_highest_ranked, 1); |
---|
40 | } |
---|
41 | |
---|
42 | } |
---|