# Examples 7.2.3:
AVAL

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

include AVAL{BIT}

Note that there is no creation routine and that the writer of the including class must specify the constant asize as, say,

const asize : CARD := 128 ;

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.

#### aset

This assignment routine is one of those for which 'infix' array indexing syntax is provided. The statement -

[93] := setbit

which gives the value 'set' to the 94th (indexing begins at zero!) element of the CPU_BUS is an alternative syntactic form of

aset(93,setbit)

which has the identical meaning. Thus the loop

loop
[0.upto!(41)] := setbit
end

and

loop
aset(0.upto!(41),setbit)
end

both set all elements of the list array to the setbit value.

#### aget

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

loop
if [0.upto!(41)].clear then
...
end
end

or

loop
if aget(0.upto!(41)).clear then
...
end
end

#### aind!

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

loop
if aget(aind!).clear then
...
end
end

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.

#### aelt!

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 -

set_count : CARD is
count : CARD := 0 ;
loop
if aelt!.set then
count := count + 1
end
end ;
return count
end

#### aelt!

The routine could be re-written to sum the last cnt bits, say, as -

count_tail(
| cnt : CARD
| ) : CARD is

count : CARD := 0 ;
loop
if aelt!(asize - cnt).set then
count := count + 1
end
end ;
return count
end

in which the argument to aelt! is the bus bit at which the iter is to start.

#### aelt!

The routine could be re-written to count the first num elements, say, as -

sum_head(
| num : CARD
| ) : CARD is

count : CARD := 0 ;
loop
if aelt!(0,num).set then
count := count + 1
end
end ;
return count
end

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.

#### aelt!

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

count : CARD := 0 ;
loop
if aelt!(0,num,2.int).set then
count := count + 1
end
end ;
return res
end

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. |