summaryrefslogtreecommitdiff
path: root/tools/build/v2/util/container.jam
blob: dd4963938e2b7fd96e5cc8e6b4a8d3d68f07706f (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
336
337
338
339
# Copyright 2003 Dave Abrahams
# Copyright 2002, 2003 Rene Rivera
# Copyright 2002, 2003, 2004 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)

# Various container classes.

# Base for container objects. This lets us construct recursive structures. That
# is containers with containers in them, specifically so we can tell literal
# values from node values.
#
class node
{
    rule __init__ (
        value ?  # Optional value to set node to initially.
    )
    {
        self.value = $(value) ;
    }

    # Set the value of this node, passing nothing will clear it.
    #
    rule set ( value * )
    {
        self.value = $(value) ;
    }

    # Get the value of this node.
    #
    rule get ( )
    {
        return $(self.value) ;
    }
}


# A simple vector. Interface mimics the C++ std::vector and std::list, with the
# exception that indices are one (1) based to follow Jam standard.
#
# TODO: Possibly add assertion checks.
#
class vector : node
{
    import numbers ;
    import utility ;
    import sequence ;

    rule __init__ (
        values *  # Initial contents of vector.
    )
    {
        node.__init__ ;
        self.value = $(values) ;
    }

    # Get the value of the first element.
    #
    rule front ( )
    {
        return $(self.value[1]) ;
    }

    # Get the value of the last element.
    #
    rule back ( )
    {
        return $(self.value[-1]) ;
    }

    # Get the value of the element at the given index, one based. Access to
    # elements of recursive structures is supported directly. Specifying
    # additional index values recursively accesses the elements as containers.
    # For example: [ $(v).at 1 : 2 ] would retrieve the second element of our
    # first element, assuming the first element is a container.
    #
    rule at (
        index  # The element index, one based.
        : *  # Additional indices to access recursively.
    )
    {
        local r = $(self.value[$(index)]) ;
        if $(2)
        {
            r = [ $(r).at $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ] ;
        }
        return $(r) ;
    }

    # Get the value contained in the given element. This has the same
    # functionality and interface as "at" but in addition gets the value of the
    # referenced element, assuming it is a "node".
    #
    rule get-at (
        index  # The element index, one based.
        : *  # Additional indices to access recursively.
    )
    {
        local r = $(self.value[$(index)]) ;
        if $(2)
        {
            r = [ $(r).at $(2) : $(3) : $(4) : $(5) : $(6) : $(7) : $(8) : $(9) ] ;
        }
        return [ $(r).get ] ;
    }

    # Insert the given value into the front of the vector pushing the rest of
    # the elements back.
    #
    rule push-front (
        value  # Value to become first element.
    )
    {
        self.value = $(value) $(self.value) ;
    }

    # Remove the front element from the vector. Does not return the value. No
    # effect if vector is empty.
    #
    rule pop-front ( )
    {
        self.value = $(self.value[2-]) ;
    }

    # Add the given value at the end of the vector.
    #
    rule push-back (
        value  # Value to become back element.
    )
    {
        self.value += $(value) ;
    }

    # Remove the back element from the vector. Does not return the value. No
    # effect if vector is empty.
    #
    rule pop-back ( )
    {
        self.value = $(self.value[1--2]) ;
    }

    # Insert the given value at the given index, one based. The values at and to
    # the right of the index are pushed back to make room for the new value.
    # If the index is passed the end of the vector the element is added to the
    # end.
    #
    rule insert (
        index  # The index to insert at, one based.
        : value  # The value to insert.
    )
    {
        local left = $(self.value[1-$(index)]) ;
        local right = $(self.value[$(index)-]) ;
        if $(right)-is-not-empty
        {
            left = $(left[1--2]) ;
        }
        self.value = $(left) $(value) $(right) ;
    }

    # Remove one or more elements from the vector. The range is inclusive, and
    # not specifying an end is equivalent to the [start, start] range.
    #
    rule erase (
        start  # Index of first element to remove.
        end ?  # Optional, index of last element to remove.
    )
    {
        end ?= $(start) ;
        local left = $(self.value[1-$(start)]) ;
        left = $(left[1--2]) ;
        local right = $(self.value[$(end)-]) ;
        right = $(right[2-]) ;
        self.value = $(left) $(right) ;
    }

    # Remove all elements from the vector.
    #
    rule clear ( )
    {
        self.value = ;
    }

    # The number of elements in the vector.
    #
    rule size ( )
    {
        return [ sequence.length $(self.value) ] ;
    }

    # Returns "true" if there are NO elements in the vector, empty otherwise.
    #
    rule empty ( )
    {
        if ! $(self.value)-is-not-empty
        {
            return true ;
        }
    }

    # Returns the textual representation of content.
    #
    rule str ( )
    {
        return "[" [ sequence.transform utility.str : $(self.value) ] "]" ;
    }

    # Sorts the vector inplace, calling 'utility.less' for comparisons.
    #
    rule sort ( )
    {
        self.value = [ sequence.insertion-sort $(self.value) : utility.less ] ;
    }

    # Returns true if content is equal to the content of other vector. Uses
    # 'utility.equal' for comparison.
    #
    rule equal ( another )
    {
        local mismatch ;
        local size = [ size ] ;
        if $(size) = [ $(another).size ]
        {
            for local i in [ numbers.range 1 $(size) ]
            {
                if ! [ utility.equal [ at $(i) ] [ $(another).at $(i) ] ]
                {
                    mismatch = true ;
                }
            }
        }
        else
        {
            mismatch = true ;
        }

        if ! $(mismatch)
        {
            return true ;
        }
    }
}


rule __test__ ( )
{
    import assert ;
    import "class" : new ;

    local v1 = [ new vector ] ;
    assert.true $(v1).equal $(v1) ;
    assert.true $(v1).empty ;
    assert.result 0 : $(v1).size ;
    assert.result "[" "]" : $(v1).str ;
    $(v1).push-back b ;
    $(v1).push-front a ;
    assert.result "[" a b "]" : $(v1).str ;
    assert.result a : $(v1).front ;
    assert.result b : $(v1).back ;
    $(v1).insert 2 : d ;
    $(v1).insert 2 : c ;
    $(v1).insert 4 : f ;
    $(v1).insert 4 : e ;
    $(v1).pop-back ;
    assert.result 5 : $(v1).size ;
    assert.result d : $(v1).at 3 ;
    $(v1).pop-front ;
    assert.result c : $(v1).front ;
    assert.false $(v1).empty ;
    $(v1).erase 3 4 ;
    assert.result 2 : $(v1).size ;

    local v2 = [ new vector q w e r t y ] ;
    assert.result 6 : $(v2).size ;
    $(v1).push-back $(v2) ;
    assert.result 3 : $(v1).size ;
    local v2-alias = [ $(v1).back ] ;
    assert.result e : $(v2-alias).at 3 ;
    $(v1).clear ;
    assert.true $(v1).empty ;
    assert.false $(v2-alias).empty ;
    $(v2).pop-back ;
    assert.result t : $(v2-alias).back ;

    local v3 = [ new vector ] ;
    $(v3).push-back [ new vector 1 2 3 4 5 ] ;
    $(v3).push-back [ new vector a b c ] ;
    assert.result "[" "[" 1 2 3 4 5 "]" "[" a b c "]" "]" : $(v3).str ;
    $(v3).push-back [ new vector [ new vector x y z ] [ new vector 7 8 9 ] ] ;
    assert.result 1 : $(v3).at 1 : 1 ;
    assert.result b : $(v3).at 2 : 2 ;
    assert.result a b c : $(v3).get-at 2 ;
    assert.result 7 8 9 : $(v3).get-at 3 : 2 ;

    local v4 = [ new vector 4 3 6 ] ;
    $(v4).sort ;
    assert.result 3 4 6 : $(v4).get ;
    assert.false $(v4).equal $(v3) ;

    local v5 = [ new vector 3 4 6 ] ;
    assert.true $(v4).equal $(v5) ;
    # Check that vectors of different sizes are considered non-equal.
    $(v5).pop-back ;
    assert.false $(v4).equal $(v5) ;

    local v6 = [ new vector [ new vector 1 2 3 ] ] ;
    assert.true $(v6).equal [ new vector [ new vector 1 2 3 ] ] ;

    local v7 = [ new vector 111 222 333 ] ;
    assert.true $(v7).equal $(v7) ;
    $(v7).insert 4 : 444 ;
    assert.result 111 222 333 444 : $(v7).get ;
    $(v7).insert 999 : xxx ;
    assert.result 111 222 333 444 xxx : $(v7).get ;

    local v8 = [ new vector "" "" "" ] ;
    assert.true $(v8).equal $(v8) ;
    assert.false $(v8).empty ;
    assert.result 3  : $(v8).size ;
    assert.result "" : $(v8).at 1 ;
    assert.result "" : $(v8).at 2 ;
    assert.result "" : $(v8).at 3 ;
    assert.result    : $(v8).at 4 ;
    $(v8).insert 2 : 222 ;
    assert.result 4 : $(v8).size ;
    assert.result "" 222 "" "" : $(v8).get ;
    $(v8).insert 999 : "" ;
    assert.result 5 : $(v8).size ;
    assert.result "" 222 "" "" "" : $(v8).get ;
    $(v8).insert 999 : xxx ;
    assert.result 6 : $(v8).size ;
    assert.result "" 222 "" "" "" xxx : $(v8).get ;

    # Regression test for a bug causing vector.equal to compare only the first
    # and the last element in the given vectors.
    local v9 = [ new vector 111 xxx 222 ] ;
    local v10 = [ new vector 111 yyy 222 ] ;
    assert.false $(v9).equal $(v10) ;
}