<
aspect xmlns:cpp="
http://www.sdml.info/srcML/cpp"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
xmlns:util="
http://www.pnp-software.com/util"
xmlns="
http://control.ee.ethz.ch/XWeaver/AspectX"
xmlns:src="
http://www.sdml.info/srcML/src"
xsi:schemaLocation="
http://control.ee.ethz.ch/XWeaver/AspectX ../../../../src/xsd/aspectX.xsd"
name="
Efficiency">
<
description>
This aspect counts the number of comparisons made by
the quick sort algorithm. This can give a measure of
efficiency of the algorithm.
<
author>
I. Birrer</
author>
</
description>
<
pointcut name="
allIfBlocks"
type="
src:block"
constraint="
parent::src:then">
<
description>
Points to all if blocks. The poinctut is defined as follows: In srcML <
i>
if</
i>
statements
are identified by the <
code>
src:if</
code>
element. The
<
i>
if</
i>
part is then identified by a <
code>
src:then</
code>
child element whereas the <
i>
else</
i>
block is identified as a <
code>
src:else</
code>
child element. The
<
i>
if</
i>
part further can have a <
code>
src:block</
code>
child element which contains the block.
Hence if we identify all <
code>
src:block</
code>
elements with a parent element
<
code>
src:then</
code>
then we get all blocks of all <
i>
if</
i>
statements.
Note that <
i>
if</
i>
statements without blocks are not affected by weaving!
</
description>
</
pointcut>
<
advice type="
begin"
name="
beginOfIfBlock">
<
description>
Increments the comparison counter for each comparison made.
Each if statement in the base code is considered as a comparison.
</
description>
<
pointcutRef ref="
allIfBlocks"
type="
src:block" />
<
codeModifier type="
codeFragment">
<
text>
comparisonCounter++;</
text>
</
codeModifier>
</
advice>
<
advice type="
add"
name="
addComparison definition">
<
description>
Add <
code>
comparisonCount</
code>
declaration. Since there is
no weaving rule defined for adding a <
i>
declaration</
i>
at the <
i>
begin</
i>
of a <
i>
unit</
i>
, the "add include"
weaving rule is used. This works just fine for now, as long
as the "inlude weaving rules" does not perform any tests
whether the added string is an include statement or not.
</
description>
<
pointcut type="
src:unit" />
<
codeModifier type="
declaration">
<
text>
int comparisonCounter = 0;</
text>
</
codeModifier>
</
advice>
<
advice name="
beforeTermination"
type="
before">
<
description>
Outputs the number of comparisons
made by the algorithm, before the program exits. The programs exits when the <
i>
main</
i>
function is left or more precisely when <
i>
return</
i>
is called in the main function. To add
a statement at the exit of a program therefore means to add a statement before any
return apperaing in the main function.
The pointcut that points to all return elements appearing within the main function is
expressed using a restriction of type <
code>
within</
code>
.
The instruction that is added to the code prints the value of the comparisonCounter variable.
</
description>
<
pointcut type="
src:return">
<
restriction type="
within">
<
pointcut type="
src:function"
constraint="
src:name='main'" />
</
restriction>
</
pointcut>
<
codeModifier type="
codeFragment">
<
text>
printf( "\nComparisons: %d", comparisonCounter ); </
text>
</
codeModifier>
</
advice>
</
aspect>
v