/*
Copyright (C) 2001-2003, The Perl Foundation.
$Id: enumerate.pmc 16275 2006-12-28 01:20:44Z particle $

=head1 NAME

src/pmc/enumerate.pmc - enumerate PMC

=head1 DESCRIPTION

These is an Iterator return [idx, item] tuples.

=head2 Methods

=over 4

=cut

*/

#include "parrot/parrot.h"

pmclass Enumerate extends Iterator no_ro {

    void init_pmc(PMC* iterable) {
        SUPER(iterable);
        PMC_struct_val(SELF) = key_new_integer(INTERP, 0);
        PMC_pmc_val(SELF) = VTABLE_get_iter(INTERP, iterable);
    }

/*

=item C<PMC* shift_pmc()>

Returns [idx, element] for the current idx/key and sets the idx/key to
the next one.

=cut

*/

    INTVAL get_bool() {
        PMC *key = PMC_struct_val(SELF);
        PMC *iter = PMC_pmc_val(SELF);
        INTVAL ok = PMC_int_val(key) >= 0 && VTABLE_get_bool(INTERP, iter);
        if (!ok)
            PMC_int_val(key) = -1;
        return ok;
    }

    PMC* shift_pmc() {
        PMC *key = PMC_struct_val(SELF);
        PMC *iter = PMC_pmc_val(SELF);
        PMC *item, *tupl, *val;
        INTVAL i = PMC_int_val(key);

        if (i == -1)
            real_exception(INTERP, NULL, E_StopIteration, "StopIteration");
        item = VTABLE_shift_pmc(INTERP, iter);
        tupl = pmc_new(INTERP, enum_class_FixedPMCArray);
        ++PMC_int_val(key);
        VTABLE_set_integer_native(INTERP, tupl, 2);
        val = pmc_new(INTERP, enum_class_Integer);
        PMC_int_val(val) = i;
        VTABLE_set_pmc_keyed_int(INTERP, tupl, 0, val);
        VTABLE_set_pmc_keyed_int(INTERP, tupl, 1, item);
        return tupl;
    }

    PMC* get_iter() {
        return SELF;
    }

}

/*

=back

=cut

*/


/*
 * Local variables:
 *   c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
