global agg_array

probe begin
{
    #
    # Array section
    #
    printf("Arrays of aggregates:\n");

    # Add elements to the array of aggregates, but in an unsorted order
    agg_array[9] <<< 18
    agg_array[1] <<< 40
    agg_array[8] <<< 16
    agg_array[2] <<< 4
    agg_array[7] <<< 14
    agg_array[3] <<< 36
    agg_array[6] <<< 12
    agg_array[5] <<< 10
    agg_array[4] <<< 8
    agg_array[10] <<< 20

    agg_array[9] <<< 18 / 2
    agg_array[1] <<< 40 / 2
    agg_array[8] <<< 16 / 2
    agg_array[2] <<< 4 / 2
    agg_array[7] <<< 14 / 2
    agg_array[3] <<< 36 / 2
    agg_array[6] <<< 12 / 2
    agg_array[5] <<< 10 / 2
    agg_array[4] <<< 8

    agg_array[9] <<< 18 * 3
    agg_array[1] <<< 40 * 3
    agg_array[8] <<< 16 * 3
    agg_array[2] <<< 4 * 3
    agg_array[7] <<< 14 * 3
    agg_array[3] <<< 36
    agg_array[6] <<< 12
    agg_array[5] <<< 10

    agg_array[9] <<< 1
    agg_array[1] <<< 2
    agg_array[8] <<< 3
    agg_array[2] <<< 4
    agg_array[7] <<< 14 * 3
    agg_array[3] <<< 36 * 3
    agg_array[6] <<< 12

    agg_array[9] <<< 2
    agg_array[1] <<< 3
    agg_array[8] <<< 16
    agg_array[2] <<< 4
    agg_array[7] <<< 14
    agg_array[3] <<< 36

    agg_array[9] <<< 18
    agg_array[1] <<< 40
    agg_array[8] <<< 16
    agg_array[2] <<< 4
    agg_array[7] <<< 14

    agg_array[9] <<< 18
    agg_array[1] <<< 40
    agg_array[8] <<< 16
    agg_array[2] <<< 4

    agg_array[9] <<< 18
    agg_array[1] <<< 40
    agg_array[8] <<< 16

    agg_array[9] <<< 18
    agg_array[1] <<< 40

    agg_array[9] <<< 18

    # Print the sorted array (by value, decreasing)
    printf("sorted (by values, decreasing):\n");
    foreach (key in agg_array-)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
    }

    # Print the sorted array (by value, increasing)
    printf("\nsorted (by values), increasing:\n");
    foreach (key in agg_array+)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
    }

    # Print the first 5 sorted array items (by value, decreasing)
    printf("\nsorted (by values, decreasing) limit 5:\n");
    i = 0
    foreach (key in agg_array- limit 5)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
	i++;
    }
    printf("loop had %d iterations\n", i);

    # Print the first 5 sorted array items (by value, increasing)
    printf("\nsorted (by values, increasing) limit 5:\n");
    i = 0
    foreach (key in agg_array+ limit 5)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
	i++;
    }
    printf("loop had %d iterations\n", i);

    # Print the first 5 sorted array items (by key)
    printf("\nsorted (by keys) limit 5:\n");
    i = 0
    foreach (key+ in agg_array limit 5)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
	i++;
    }
    printf("loop had %d iterations\n", i);

    # Use a variable reference to limit the loop iteration
    x = 3
    printf("\nsorted (by values) limit x (%d):\n", x);
    i = 0
    foreach (key in agg_array+ limit x)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
	i++;
    }
    printf("loop had %d iterations\n", i);

    # Use a expression containing a variable reference to limit the
    # loop iteration
    printf("\nsorted (by values) limit x * 2 (%d):\n", (x * 2));
    i = 0
    foreach (key in agg_array+ limit x * 2)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
	i++;
    }
    printf("loop had %d iterations\n", i);

    # Ensure the expression is only evaluated once
    printf("\nsorted (by values) limit ++x:\n");
    i = 0
    foreach (key in agg_array+ limit ++x)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
	i++;
    }
    printf("loop had %d iterations\n", i);
    printf("x ended up as %d\n", x);

    # Ensure the expression is only evaluated once
    printf("\nsorted (by values) limit x++:\n");
    i = 0
    foreach (key in agg_array+ limit x++)
    {
	printf("agg_array[%d]: count:%d  sum:%d  avg:%d  min:%d  max:%d\n", 
	       key, @count(agg_array[key]), @sum(agg_array[key]),
	       @avg(agg_array[key]), @min(agg_array[key]),
	       @max(agg_array[key]))
	i++;
    }
    printf("loop had %d iterations\n", i);
    printf("x ended up as %d\n", x);

    printf("\nDone.\n");
    exit()
}
