summaryrefslogtreecommitdiff
path: root/jam-files/boost-build/util/sequence.jam
blob: 73919a65dd8a1940ff9dcceb47330a47e847592a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# Copyright 2001, 2002, 2003 Dave Abrahams 
# Copyright 2006 Rene Rivera 
# Copyright 2002, 2003 Vladimir Prus 
# Distributed under the Boost Software License, Version 1.0. 
# (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) 

import assert ;
import numbers ; 
import modules ;


# Note that algorithms in this module execute largely in the caller's module
# namespace, so that local rules can be used as function objects. Also note that
# most predicates can be multi-element lists. In that case, all but the first
# element are prepended to the first argument which is passed to the rule named
# by the first element.


# Return the elements e of $(sequence) for which [ $(predicate) e ] has a
# non-null value.
#
rule filter ( predicate + : sequence * )
{
    local caller = [ CALLER_MODULE ] ;
    local result ;

    for local e in $(sequence)
    {
        if [ modules.call-in $(caller) : $(predicate) $(e) ]
        {
            result += $(e) ;
        }
    }
    return $(result) ;
}


# Return a new sequence consisting of [ $(function) $(e) ] for each element e of
# $(sequence).
#
rule transform ( function + : sequence * )
{
    local caller = [ CALLER_MODULE ] ;
    local result ;

    for local e in $(sequence)
    {
        result += [ modules.call-in $(caller) : $(function) $(e) ] ;
    }
    return $(result) ;
}


rule reverse ( s * )
{
    local r ;
    for local x in $(s)
    {
        r = $(x) $(r) ;
    }
    return $(r) ;
}


rule less ( a b )
{
    if $(a) < $(b)
    {
        return true ;
    }
}


# Insertion-sort s using the BinaryPredicate ordered.
#
rule insertion-sort ( s * : ordered * )
{
    if ! $(ordered)
    {
        return [ SORT $(s) ] ;
    }
    else 
    {           
        local caller = [ CALLER_MODULE ] ;
        ordered ?= sequence.less ;
        local result = $(s[1]) ;
        if $(ordered) = sequence.less
        {
            local head tail ;
            for local x in $(s[2-])
            {
                head = ;
                tail = $(result) ;
                while $(tail) && ( $(tail[1]) < $(x) )
                {
                    head += $(tail[1]) ;
                    tail = $(tail[2-]) ;
                }
                result = $(head) $(x) $(tail) ;
            }
        }
        else
        {
            for local x in $(s[2-])
            {
                local head tail ;
                tail = $(result) ;
                while $(tail) && [ modules.call-in $(caller) : $(ordered) $(tail[1]) $(x) ]
                {
                    head += $(tail[1]) ;
                    tail = $(tail[2-]) ;
                }
                result = $(head) $(x) $(tail) ;
            }
        }
        
        return $(result) ;
    }    
}


# Merge two ordered sequences using the BinaryPredicate ordered.
#
rule merge ( s1 * : s2 * : ordered * )
{
    ordered ?= sequence.less ;
    local result__ ;
    local caller = [ CALLER_MODULE ] ;

    while $(s1) && $(s2)
    {
        if [ modules.call-in $(caller) : $(ordered) $(s1[1]) $(s2[1]) ]
        {
            result__ += $(s1[1]) ;
            s1 = $(s1[2-]) ;
        }
        else if [ modules.call-in $(caller) : $(ordered) $(s2[1]) $(s1[1]) ]
        {
            result__ += $(s2[1]) ;
            s2 = $(s2[2-]) ;
        }
        else 
        {           
            s2 = $(s2[2-]) ;
        }
        
    }
    result__ += $(s1) ;
    result__ += $(s2) ;

    return $(result__) ;
}


# Join the elements of s into one long string. If joint is supplied, it is used
# as a separator.
#
rule join ( s * : joint ? )
{
    joint ?= "" ;
    return $(s:J=$(joint)) ;
}


# Find the length of any sequence.
#
rule length ( s * )
{
    local result = 0 ;
    for local i in $(s)
    {
        result = [ CALC $(result) + 1 ] ;
    }
    return $(result) ;
}


rule unique ( list * : stable ? )
{
    local result ;
    local prev ;
    if $(stable)
    {
        for local f in $(list)
        {
            if ! $(f) in $(result)
            {
                result += $(f) ;
            }
        }
    }
    else
    {
        for local i in [ SORT $(list) ]
        {
            if $(i) != $(prev)
            {
                result += $(i) ;
            }
            prev = $(i) ;
        }
    }
    return $(result) ;
}


# Returns the maximum number in 'elements'. Uses 'ordered' for comparisons or
# 'numbers.less' if none is provided.
#
rule max-element ( elements + : ordered ? )
{
    ordered ?= numbers.less ;

    local max = $(elements[1]) ;
    for local e in $(elements[2-]) 
    {
        if [ $(ordered) $(max) $(e) ] 
        {
            max = $(e) ;
        }
    }
    return $(max) ;           
}


# Returns all of 'elements' for which corresponding element in parallel list
# 'rank' is equal to the maximum value in 'rank'.
#
rule select-highest-ranked ( elements * : ranks * )
{
    if $(elements)
    {        
        local max-rank = [ max-element $(ranks) ] ;
        local result ;
        while $(elements) 
        {
            if $(ranks[1]) = $(max-rank)
            {
                result += $(elements[1]) ;
            }
            elements = $(elements[2-]) ;
            ranks = $(ranks[2-]) ;
        }
        return $(result) ;
    }    
}
NATIVE_RULE sequence : select-highest-ranked ;


rule __test__ ( )
{
    # Use a unique module so we can test the use of local rules.
    module sequence.__test__
    {
        import assert ;
        import sequence ;
        
        local rule is-even ( n )
        {
            if $(n) in 0 2 4 6 8
            {
                return true ;
            }
        }

        assert.result 4 6 4 2 8 : sequence.filter is-even : 1 4 6 3 4 7 2 3 8 ;

        # Test that argument binding works.
        local rule is-equal-test ( x y )
        {
            if $(x) = $(y)
            {
                return true ;
            }
        }

        assert.result 3 3 3 : sequence.filter is-equal-test 3 : 1 2 3 4 3 5 3 5 7 ;

        local rule append-x ( n )
        {
            return $(n)x ;
        }

        assert.result 1x 2x 3x : sequence.transform append-x : 1 2 3 ;

        local rule repeat2 ( x )
        {
            return $(x) $(x) ;
        }

        assert.result 1 1 2 2 3 3 : sequence.transform repeat2 : 1 2 3 ;

        local rule test-greater ( a b )
        {
            if $(a) > $(b)
            {
                return true ;
            }
        }
        assert.result 1 2 3 4 5 6 7 8 9 : sequence.insertion-sort 9 6 5 3 8 7 1 2 4 ;
        assert.result 9 8 7 6 5 4 3 2 1 : sequence.insertion-sort 9 6 5 3 8 7 1 2 4 : test-greater ;
        assert.result 1 2 3 4 5 6 :  sequence.merge 1 3 5 : 2 4 6 ;
        assert.result 6 5 4 3 2 1 :  sequence.merge 5 3 1 : 6 4 2 : test-greater ;
        assert.result 1 2 3 : sequence.merge 1 2 3 : ;
        assert.result 1 : sequence.merge 1 : 1 ;

        assert.result foo-bar-baz : sequence.join foo bar baz : - ;
        assert.result substandard : sequence.join sub stan dard ;
        assert.result 3.0.1 : sequence.join 3.0.1 : - ;

        assert.result 0 : sequence.length ;
        assert.result 3 : sequence.length a b c ;
        assert.result 17 : sequence.length 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 ;

        assert.result 1 : sequence.length a ;
        assert.result 10 : sequence.length a b c d e f g h i j ;
        assert.result 11 : sequence.length a b c d e f g h i j k ;
        assert.result 12 : sequence.length a b c d e f g h i j k l ;

        local p2 = x ;
        for local i in 1 2 3 4 5 6 7 8
        {
            p2 = $(p2) $(p2) ;
        }
        assert.result 256 : sequence.length $(p2) ;

        assert.result 1 2 3 4 5 : sequence.unique 1 2 3 2 4 3 3 5 5 5 ;
                
        assert.result 5 : sequence.max-element 1 3 5 0 4 ;
        
        assert.result e-3 h-3 : sequence.select-highest-ranked e-1 e-3 h-3 m-2 : 1 3 3 2 ;
        
        assert.result 7 6 5 4 3 2 1 : sequence.reverse 1 2 3 4 5 6 7 ;
    }
}