[svn:parrot] r46378 - in trunk: include/parrot src src/pmc

plobsing at svn.parrot.org plobsing at svn.parrot.org
Fri May 7 02:52:07 UTC 2010


Author: plobsing
Date: Fri May  7 02:52:07 2010
New Revision: 46378
URL: https://trac.parrot.org/parrot/changeset/46378

Log:
consolidate packfile constant reverse lookup in PackFile_ConstTable_rlookup function

Modified:
   trunk/include/parrot/packfile.h
   trunk/src/packout.c
   trunk/src/pmc/imageio.pmc
   trunk/src/pmc/imageiosize.pmc

Modified: trunk/include/parrot/packfile.h
==============================================================================
--- trunk/include/parrot/packfile.h	Fri May  7 02:38:23 2010	(r46377)
+++ trunk/include/parrot/packfile.h	Fri May  7 02:52:07 2010	(r46378)
@@ -388,6 +388,15 @@
         __attribute__nonnull__(2);
 
 PARROT_EXPORT
+int PackFile_ConstTable_rlookup(PARROT_INTERP,
+    ARGIN(const PackFile_ConstTable *ct),
+    ARGIN(PMC *key),
+    int type)
+        __attribute__nonnull__(1)
+        __attribute__nonnull__(2)
+        __attribute__nonnull__(3);
+
+PARROT_EXPORT
 int PackFile_find_in_const(PARROT_INTERP,
     ARGIN(const PackFile_ConstTable *ct),
     ARGIN(PMC *key),
@@ -424,6 +433,10 @@
 #define ASSERT_ARGS_PackFile_ConstTable_pack_size __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp) \
     , PARROT_ASSERT_ARG(seg))
+#define ASSERT_ARGS_PackFile_ConstTable_rlookup __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+       PARROT_ASSERT_ARG(interp) \
+    , PARROT_ASSERT_ARG(ct) \
+    , PARROT_ASSERT_ARG(key))
 #define ASSERT_ARGS_PackFile_find_in_const __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
        PARROT_ASSERT_ARG(interp) \
     , PARROT_ASSERT_ARG(ct) \

Modified: trunk/src/packout.c
==============================================================================
--- trunk/src/packout.c	Fri May  7 02:38:23 2010	(r46377)
+++ trunk/src/packout.c	Fri May  7 02:52:07 2010	(r46378)
@@ -218,24 +218,76 @@
 PARROT_EXPORT
 int
 PackFile_find_in_const(PARROT_INTERP,
-        ARGIN(const PackFile_ConstTable *ct), ARGIN(PMC *key), int type)
+    ARGIN(const PackFile_ConstTable *ct), ARGIN(PMC *key), int type)
 {
     ASSERT_ARGS(PackFile_find_in_const)
+    int i = PackFile_ConstTable_rlookup(interp, ct, key, type);
+
+    if (i < 0) {
+        Parrot_io_eprintf(NULL, "find_in_const: couldn't find const for key\n");
+        Parrot_exit(interp, 1);
+    }
+
+    return i;
+}
+
+/*
+
+=item C<int PackFile_ConstTable_rlookup(PARROT_INTERP, const PackFile_ConstTable
+*ct, PMC *key, int type)>
+
+Reverse lookup a constant in the constant table.
+
+TODO: use a hash to make these O(1) for strings
+
+=cut
+
+*/
+
+PARROT_EXPORT
+int
+PackFile_ConstTable_rlookup(PARROT_INTERP,
+    ARGIN(const PackFile_ConstTable *ct), ARGIN(PMC *key), int type)
+{
+    ASSERT_ARGS(PackFile_ConstTable_rlookup)
     int      i;
     FLOATVAL key_num;
     STRING  *key_str;
 
+    PARROT_ASSERT(type == PFC_STRING || type == PFC_NUMBER);
+
     GETATTR_Key_str_key(interp, key, key_str);
     GETATTR_Key_num_key(interp, key, key_num);
 
     for (i = 0; i < ct->const_count; ++i) {
-        if (type == PFC_STRING && ct->constants[i]->u.string == key_str)
-            return i;
-        if (type == PFC_NUMBER && ct->constants[i]->u.number == key_num)
-            return i;
+        PackFile_Constant *constant = ct->constants[i];
+
+        switch (type) {
+          case PFC_STRING:
+            if (constant->type == PFC_STRING) {
+                STRING * const sc = constant->u.string;
+                if (Parrot_str_equal(interp, key_str, sc)
+                &&  Parrot_charset_number_of_str(interp, key_str)
+                ==  Parrot_charset_number_of_str(interp, sc)
+                &&  Parrot_encoding_number_of_str(interp, key_str)
+                ==  Parrot_encoding_number_of_str(interp, sc))
+                    return i;
+            }
+            break;
+
+          case PFC_NUMBER:
+            if (constant->type == PFC_NUMBER)
+                if (constant->u.number == key_num)
+                    return i;
+            break;
+
+          default:
+            PANIC(interp, "Universe imploded. Did you divide by zero?");
+        }
     }
-    Parrot_io_eprintf(NULL, "find_in_const: couldn't find const for key\n");
-    Parrot_exit(interp, 1);
+
+    /* not found */
+    return -1;
 }
 
 /*

Modified: trunk/src/pmc/imageio.pmc
==============================================================================
--- trunk/src/pmc/imageio.pmc	Fri May  7 02:38:23 2010	(r46377)
+++ trunk/src/pmc/imageio.pmc	Fri May  7 02:52:07 2010	(r46378)
@@ -536,23 +536,15 @@
     VTABLE void push_string(STRING *v) {
         if (PObj_flag_TEST(private1, SELF)) {
             /* store a reference to constant table entry of string */
-            int i;
+            PMC *v_pmc = key_new_string(interp, v);
             PackFile_ConstTable *table = PARROT_IMAGEIO(SELF)->pf_ct;
-            for (i = 0; i < table->const_count; i++) {
-                PackFile_Constant *constant = table->constants[i];
-                if (constant->type == PFC_STRING) {
-                    STRING * const sc = constant->u.string;
-                    if (Parrot_str_equal(INTERP, v, sc)
-                    && Parrot_charset_number_of_str(INTERP, v)
-                    ==  Parrot_charset_number_of_str(INTERP, sc)
-                    &&  Parrot_encoding_number_of_str(INTERP, v)
-                    ==  Parrot_encoding_number_of_str(INTERP, sc))
-                    {
-                        STATICSELF.push_integer(i);
-                        return;
-                    }
-                }
+            int idx = PackFile_ConstTable_rlookup(INTERP, table, v_pmc, PFC_STRING);
+
+            if (idx >= 0) {
+                STATICSELF.push_integer(idx);
+                return;
             }
+
             /* XXX
              * handle cases where the PMC has changed after Parrot_freeze_strings was called
              * eg: :immediate subs

Modified: trunk/src/pmc/imageiosize.pmc
==============================================================================
--- trunk/src/pmc/imageiosize.pmc	Fri May  7 02:38:23 2010	(r46377)
+++ trunk/src/pmc/imageiosize.pmc	Fri May  7 02:52:07 2010	(r46378)
@@ -246,23 +246,16 @@
 
     VTABLE void push_string(STRING *v) {
         if (PObj_flag_TEST(private1, SELF)) {
-            /* look for a reference to constant table entry of string */
-            int i;
+            /* store a reference to constant table entry of string */
+            PMC *v_pmc = key_new_string(interp, v);
             PackFile_ConstTable *table = PARROT_IMAGEIOSIZE(SELF)->pf_ct;
-            for (i = 0; i < table->const_count; i++) {
-                PackFile_Constant *constant = table->constants[i];
-                if (constant->type == PFC_STRING) {
-                    STRING * const sc = constant->u.string;
-                    if (Parrot_str_equal(INTERP, v, sc)
-                    &&  Parrot_charset_number_of_str(INTERP, v)
-                    ==  Parrot_charset_number_of_str(INTERP, sc)
-                    &&  Parrot_encoding_number_of_str(INTERP, v)
-                    ==  Parrot_encoding_number_of_str(INTERP, sc)) {
-                        STATICSELF.push_integer(i);
-                        return;
-                    }
-                }
+            int idx = PackFile_ConstTable_rlookup(INTERP, table, v_pmc, PFC_STRING);
+
+            if (idx >= 0) {
+                STATICSELF.push_integer(idx);
+                return;
             }
+
             /* XXX
              * handle cases where the PMC has changed after Parrot_freeze_strings was called
              * eg: :immediate subs


More information about the parrot-commits mailing list