![]() |
Examples 7.2.3:
|
The features of the pervasive class AVAL are used in individual examples. None of the examples given is a complete class nor necessarily a 'complete' method.
In all of the examples it will be assumed that the code shown is in a class called, say, CPU_BUS which has included AVAL as
Note that there is no creation routine and that the writer of the including class must specify the constant asize as, say,
NOTE | If it were not necessary to access every individual bit of the CPU bus then a more relaxed approach could be taken by specifying the bus as an AVAL{OCTET} with an asize of 16 since an octet is itself an AVAL{{BIT} with an asize of 8. |
This assignment routine is one of those for which 'infix' array indexing syntax is provided. The statement -
which gives the value 'set' to the 94th (indexing begins at zero!) element of the CPU_BUS is an alternative syntactic form of
which has the identical meaning. Thus the loop
and
both set all elements of the list array to the setbit value.
This routine may be thought of as the reader routine corresponding to aset above. After the assignment loop above, therefore, it would be possible to check that each bit was set by either
or
This iter, which yields every index value for the array in turn could therefore be substituted for the upto!expression in the above loop if all bus bits were to be tested, giving
This has the advantage that it is independent of the actual size of the array and, should that change, no corresponding editing of the loop source text is needed. As a matter of programming style, the expression "0.upto!(41)" should have been written as "0.upto!(Low_Bits)", say, where Low_Bits had been defined as a constant.
The first version of the iter named aelt! yields in turn the value of each bit on the CPU bus. A simple routine to count all set bits could therefore be written as -
The routine could be re-written to sum the last cnt bits, say, as -
count_tail( | |
cnt : CARD | |
) : CARD is |
in which the argument to aelt! is the bus bit at which the iter is to start.
The routine could be re-written to count the first num elements, say, as -
sum_head( | |
num : CARD | |
) : CARD is |
in which the first argument to aelt! is the element at which the iter is to start, the second is the number of values to yield.
The summation routine could be re-written to count those num elements indexed by an odd number, say, as -
sum_odd | |
num : CARD | |
) : CARD is |
in which the last argument is the stepping interval for the iteration. Note that this argument is an INT type, not a CARD.
![]() |
Specification Index | ![]() |
Language Index | ![]() |
Section 7 Index |
Comments
or enquiries should be made to Keith Hopper. Page last modified: Sunday, 21 May 2000. |
![]() |