<commit_before>/*
Copyright (C) 2001-2007, The Perl Foundation.
$Id$

=head1 NAME

objects.c - Class and object

=head1 DESCRIPTION

Handles class and object manipulation.

=head2 Functions

=over 4

=cut

*/

#define PARROT_IN_OBJECTS_C
#include "parrot/parrot.h"
#include <assert.h>

#include "objects.str"

static void parrot_class_register(Interp *interp, PMC *name,
        PMC *new_class, PMC *parent, PMC *mro);

/*

=item C<INTVAL Parrot_get_vtable_index(Interp *interp, STRING *name)>

Return index if C<name> is a valid vtable slot name.

=cut

*/

INTVAL
Parrot_get_vtable_index(Interp *interp, STRING *name)
{
    char * const name_c = string_to_cstring(interp, name);
    INTVAL low    = 0;
    INTVAL high   = NUM_VTABLE_FUNCTIONS - 1;

    /* some of the first "slots" don't have names. skip 'em. */
    while (!Parrot_vtable_slot_names[low]) {
        low++;
        high++;
    }

    while (low <= high) {
        const INTVAL i = (low + high) / 2;
        const char * const meth_c = Parrot_vtable_slot_names[i];

        /* XXX slot_names still have __ in front */
        const INTVAL cmp = strcmp(name_c, meth_c + 2);

        if (cmp == 0) {
            string_cstring_free(name_c);
            return i;
        }
        else if (cmp > 0)
            low = i + 1;
        else
            high = i - 1;
    }

    string_cstring_free(name_c);

    return -1;
}

/*

=item C<static PMC* find_vtable_meth_ns(Interp *interp, PMC *ns,
        INTVAL vtable_index)>

Return Sub PMC if a method with the vtable name exists in ns

=cut

*/

static PMC*
find_vtable_meth_ns(Interp *interp, PMC *ns, INTVAL vtable_index)
{
    const INTVAL k   = VTABLE_elements(interp, ns);
    PMC   * const key = VTABLE_nextkey_keyed(interp, key_new(interp), ns,
        ITERATE_FROM_START);

    const char * const meth     = Parrot_vtable_slot_names[vtable_index];<commit_msg>[Vtable] Fix the binary search algorithm for looking up vtable methods by name.<commit_after>/*
Copyright (C) 2001-2007, The Perl Foundation.
$Id$

=head1 NAME

objects.c - Class and object

=head1 DESCRIPTION

Handles class and object manipulation.

=head2 Functions

=over 4

=cut

*/

#define PARROT_IN_OBJECTS_C
#include "parrot/parrot.h"
#include <assert.h>

#include "objects.str"

static void parrot_class_register(Interp *interp, PMC *name,
        PMC *new_class, PMC *parent, PMC *mro);

/*

=item C<INTVAL Parrot_get_vtable_index(Interp *interp, STRING *name)>

Return index if C<name> is a valid vtable slot name.

=cut

*/

INTVAL
Parrot_get_vtable_index(Interp *interp, STRING *name)
{
    char * const name_c = string_to_cstring(interp, name);
    INTVAL low    = 0;
    INTVAL high   = NUM_VTABLE_FUNCTIONS - 1;

    /* some of the first "slots" don't have names. skip 'em. */
    while (!Parrot_vtable_slot_names[low]) {
        low++;
        high++;
    }

    while (low < high) {
        const INTVAL       i      = (low + high) / 2;
        const char * const meth_c = Parrot_vtable_slot_names[i];

        /* XXX slot_names still have __ in front */
        const INTVAL cmp = strcmp(name_c, meth_c + 2);

        if (cmp == 0) {
            string_cstring_free(name_c);
            return i;
        }
        else if (cmp > 0)
            low  = i + 1;
        else
            high = i;
    }

    string_cstring_free(name_c);

    return -1;
}

/*

=item C<static PMC* find_vtable_meth_ns(Interp *interp, PMC *ns,
        INTVAL vtable_index)>

Return Sub PMC if a method with the vtable name exists in ns

=cut

*/

static PMC*
find_vtable_meth_ns(Interp *interp, PMC *ns, INTVAL vtable_index)
{
    const INTVAL k   = VTABLE_elements(interp, ns);
    PMC   * const key = VTABLE_nextkey_keyed(interp, key_new(interp), ns,
        ITERATE_FROM_START);

    const char * const meth     = Parrot_vtable_slot_names[vtable_index];
{'fraction_non_alphanumeric': 0.10409600834855205, 'fraction_numerical': 0.010696582311505349, 'mean_word_length': 2.7187196896217265, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2302282', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1386, 'n_words': 386}