cvsuser 05/03/27 04:35:25
Modified: classes boolean.pmc fixedbooleanarray.pmc
resizablebooleanarray.pmc
include/parrot pobj.h
t/pmc resizablebooleanarray.t
Log:
Mention that the Boolean PMC extends the Integer PMC.
---
The boolean values in the *BooleanArrays are now bits in an array
of Parrot_UInt1, aka char, values. The allocated size is now stored
in PMC_int_val2(SELF). FixedBooleanArray and ResizableBooleanArray now use
the same data layout, so it doesn't really matter who is extending whom.
There are no new tests nor methods.
Revision Changes Path
1.21 +3 -3 parrot/classes/boolean.pmc
Index: boolean.pmc
===================================================================
RCS file: /cvs/public/parrot/classes/boolean.pmc,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- boolean.pmc 12 Jan 2005 11:42:06 -0000 1.20
+++ boolean.pmc 27 Mar 2005 12:35:21 -0000 1.21
@@ -1,6 +1,6 @@
/*
Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
-$Id: boolean.pmc,v 1.20 2005/01/12 11:42:06 leo Exp $
+$Id: boolean.pmc,v 1.21 2005/03/27 12:35:21 bernhard Exp $
=head1 NAME
@@ -10,8 +10,8 @@
This class implements a boolean value variable.
-Albeit C<Boolean> is derived from C<PerlInt>, it doesn't morph to other
-types, its value is changed only.
+Albeit the C<Boolean PMC> is derived from the C<Integer PMC>,
+it doesn't morph to other types. Only it's value is changed.
=head2 Methods
1.6 +80 -38 parrot/classes/fixedbooleanarray.pmc
Index: fixedbooleanarray.pmc
===================================================================
RCS file: /cvs/public/parrot/classes/fixedbooleanarray.pmc,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- fixedbooleanarray.pmc 20 Mar 2005 12:47:01 -0000 1.5
+++ fixedbooleanarray.pmc 27 Mar 2005 12:35:21 -0000 1.6
@@ -1,6 +1,6 @@
/*
-Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
-$Id: fixedbooleanarray.pmc,v 1.5 2005/03/20 12:47:01 bernhard Exp $
+Copyright: 2001-2005 The Perl Foundation. All Rights Reserved.
+$Id: fixedbooleanarray.pmc,v 1.6 2005/03/27 12:35:21 bernhard Exp $
=head1 NAME
@@ -8,8 +8,9 @@
=head1 DESCRIPTION
-This class, FixedBooleanArray, implements an array of fixed size, which
stores booleans,
-it uses Boolean PMCs for all of the conversions
+The C<FixedBooleanArray PMC> implements an array of fixed size, which stores
booleans.
+It uses the C<Boolean PMC> for all conversions.
+The C<FixedBooleanArray PMC> is extended by the C<ResizableBooleanArray PMC>.
=head2 Functions
@@ -21,6 +22,34 @@
#include "parrot/parrot.h"
+
+/*
+ * TODO merge this with functions from BigInt PMC
+ */
+
+#define BITS_PER_CHAR 8
+
+static void
+booleanarray_init(Interp *interpreter, PMC *self) {
+ PMC_int_val2(self) = 0;
+ PMC_data(self) = NULL;
+}
+
+static void
+booleanarray_clone(Interp *interpreter, PMC *self, PMC *dest) {
+ if (PMC_data(self)) {
+ Parrot_UInt1 *sd = PMC_data(self);
+ PMC_int_val2(dest) = PMC_int_val2(self);
+ PMC_data(dest) = malloc(PMC_int_val2(self) / BITS_PER_CHAR);
+ mem_sys_memcopy(PMC_data(dest), PMC_data(self), PMC_int_val2(self) /
BITS_PER_CHAR);
+ }
+}
+
+static void
+booleanarray_clear(Interp *interpreter, PMC *self) {
+ PMC_int_val2(self) = 0;
+}
+
pmclass FixedBooleanArray need_ext does array {
/*
@@ -40,10 +69,12 @@
*/
void init () {
- PMC_int_val(SELF) = 0;
- PMC_data(SELF) = NULL;
+ PMC_int_val(SELF) = 0; /* size of array */
+ booleanarray_init(INTERP, SELF);
+ PObj_active_destroy_SET(SELF);
}
+
/*
=item C<void morph(INTVAL type)>
@@ -70,6 +101,7 @@
*/
void destroy () {
+ booleanarray_clear(INTERP, SELF);
if (PMC_data(SELF))
mem_sys_free(PMC_data(SELF));
PMC_data(SELF) = NULL;
@@ -89,14 +121,9 @@
PMC* clone () {
INTVAL size;
PMC * dest = pmc_new(INTERP, SELF->vtable->base_type);
+ PMC_int_val(dest) = PMC_int_val(SELF);
- if (!PMC_data(SELF))
- return dest;
- size = PMC_int_val(SELF);
- PMC_int_val(dest) = size;
-
- PMC_data(dest) = mem_sys_allocate(size * sizeof(INTVAL));
- mem_sys_memcopy(PMC_data(dest), PMC_data(SELF), size*sizeof(INTVAL));
+ booleanarray_clone(INTERP, SELF, dest);
PObj_active_destroy_SET(dest);
return dest;
}
@@ -112,8 +139,7 @@
*/
INTVAL get_bool () {
- INTVAL size = SELF.elements();
- return (INTVAL)(size != 0);
+ return SELF.elements() ? 1 : 0;
}
/*
@@ -154,13 +180,13 @@
*/
INTVAL get_integer_keyed_int (INTVAL key) {
- INTVAL *data;
+ Parrot_UInt1 *sd = PMC_data(SELF);
+
if (key < 0 || key >= PMC_int_val(SELF))
internal_exception(OUT_OF_BOUNDS,
"FixedBooleanArray: index out of bounds!");
- data = (INTVAL *)PMC_data(SELF);
- return data[key];
+ return ( sd[key/BITS_PER_CHAR] & ( 1 << ( key % BITS_PER_CHAR ) )) ?
1 : 0;
}
/*
@@ -176,7 +202,7 @@
INTVAL get_integer_keyed (PMC* key) {
/* simple int keys only */
INTVAL k = key_integer(INTERP, key);
- return DYNSELF.get_integer_keyed_int(k);
+ return SELF.get_integer_keyed_int(k);
}
@@ -220,9 +246,8 @@
*/
STRING* get_string_keyed_int (INTVAL key) {
- PMC *temp;
- temp = DYNSELF.get_pmc_keyed_int(key);
- return VTABLE_get_string(INTERP, temp);
+ PMC *val = DYNSELF.get_pmc_keyed_int(key);
+ return VTABLE_get_string(INTERP, val);
}
/*
@@ -252,11 +277,8 @@
*/
PMC* get_pmc_keyed_int (INTVAL key) {
- PMC *ret;
- INTVAL val;
-
- ret = pmc_new(INTERP, enum_class_Boolean);
- val = DYNSELF.get_integer_keyed_int(key);
+ PMC *ret = pmc_new(INTERP, enum_class_Boolean);
+ INTVAL val = DYNSELF.get_integer_keyed_int(key);
VTABLE_set_integer_native(INTERP, ret, val);
return ret;
}
@@ -287,10 +309,13 @@
*/
void set_integer_native (INTVAL size) {
+ Parrot_UInt1 *sd;
if (PMC_int_val(SELF) || size < 1)
internal_exception(OUT_OF_BOUNDS, "FixedBooleanArray: Can't
resize!");
+
PMC_int_val(SELF) = size;
- PMC_data(SELF) = mem_sys_allocate(size * sizeof(INTVAL));
+ PMC_int_val2(SELF) = (size / BITS_PER_CHAR + 1) * BITS_PER_CHAR;
+ PMC_data(SELF) = mem_sys_allocate(PMC_int_val2(SELF) /
BITS_PER_CHAR);
PObj_active_destroy_SET(SELF);
}
@@ -305,13 +330,17 @@
*/
void set_integer_keyed_int (INTVAL key, INTVAL value) {
- INTVAL *data;
+ Parrot_UInt1 *sd = PMC_data(SELF);
+
if (key < 0 || key >= PMC_int_val(SELF))
internal_exception(OUT_OF_BOUNDS,
"FixedBooleanArray: index out of bounds!");
- data = (INTVAL*)PMC_data(SELF);
- data[key] = (value != 0);
+ if ( value ) {
+ sd[key/BITS_PER_CHAR] |= ( 1 << ( key % BITS_PER_CHAR ) );
+ } else {
+ sd[key/BITS_PER_CHAR] &= ( ~ ( 1 << ( key % BITS_PER_CHAR ) ) );
+ }
}
/*
@@ -325,8 +354,7 @@
*/
void set_integer_keyed (PMC *key, INTVAL value) {
- INTVAL k;
- k = key_integer(INTERP, key);
+ INTVAL k = key_integer(INTERP, key);
DYNSELF.set_integer_keyed_int(k, value);
}
@@ -342,7 +370,7 @@
*/
void set_number_keyed_int (INTVAL key, FLOATVAL value) {
- DYNSELF.set_integer_keyed_int(key, (INTVAL)(value != 0.0));
+ DYNSELF.set_integer_keyed_int(key, ( value ? 1 : 0 ));
}
/*
@@ -357,8 +385,7 @@
*/
void set_number_keyed(PMC *key, FLOATVAL value) {
- INTVAL k;
- k = key_integer(INTERP, key);
+ INTVAL k = key_integer(INTERP, key);
DYNSELF.set_number_keyed_int(k, value);
}
@@ -394,8 +421,7 @@
*/
void set_string_keyed(PMC *key, STRING* value) {
- INTVAL k;
- k = key_integer(INTERP, key);
+ INTVAL k = key_integer(INTERP, key);
DYNSELF.set_string_keyed_int(k, value);
}
@@ -433,6 +459,22 @@
DYNSELF.set_pmc_keyed_int(k, value);
}
+/*
+
+=item C<void push_integer (INTVAL value)>
+
+Extends the array by adding an element of value C<value> to the end of
+the array.
+
+=cut
+
+*/
+
+ void push_integer (INTVAL value) {
+ INTVAL nextix = DYNSELF.elements();
+ DYNSELF.set_integer_keyed_int(nextix, value);
+ }
+
}
/*
1.6 +32 -67 parrot/classes/resizablebooleanarray.pmc
Index: resizablebooleanarray.pmc
===================================================================
RCS file: /cvs/public/parrot/classes/resizablebooleanarray.pmc,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- resizablebooleanarray.pmc 3 Jan 2005 22:04:30 -0000 1.5
+++ resizablebooleanarray.pmc 27 Mar 2005 12:35:21 -0000 1.6
@@ -1,6 +1,6 @@
/*
-Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
-$Id: resizablebooleanarray.pmc,v 1.5 2005/01/03 22:04:30 scog Exp $
+Copyright: 2001-2005 The Perl Foundation. All Rights Reserved.
+$Id: resizablebooleanarray.pmc,v 1.6 2005/03/27 12:35:21 bernhard Exp $
=head1 NAME
@@ -8,8 +8,10 @@
=head1 DESCRIPTION
-This class, ResizableBooleanArray, implements an array of resizable size,
which stores booleans.
-It uses Boolean PMCs for all of the conversions,
+The C<ResizableBooleanArray PMC> implements an array of resizable size,
+which stores booleans.
+It uses the C<Boolean PMC> for all conversions.
+The C<ResizableBooleanArray PMC> extends the C<FixedBooleanArray PMC>.
=head2 Functions
@@ -21,14 +23,21 @@
#include "parrot/parrot.h"
-typedef struct _SizeBooleanData {
- INTVAL size;
- INTVAL data[1];
-} SizeBooleanData;
-#define NEEDED_SIZE(n) ((n-1)*sizeof(INTVAL) + sizeof(SizeBooleanData))
+
+#define BITS_PER_CHAR 8
pmclass ResizableBooleanArray extends FixedBooleanArray need_ext does array {
+
+/*
+
+=back
+
+=head2 Methods
+
+=over 4
+
+
/*
=item C<INTVAL get_integer_keyed_int(INTVAL key)>
@@ -40,15 +49,14 @@
*/
INTVAL get_integer_keyed_int (INTVAL key) {
- SizeBooleanData *sd;
+ Parrot_UInt1 *sd;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableBooleanArray: index out of bounds!");
if(key >= PMC_int_val(SELF))
DYNSELF.set_integer_native(key+1);
-
- sd = (SizeBooleanData *)PMC_data(SELF);
- return sd->data[key];
+
+ return SUPER(key);
}
/*
@@ -62,31 +70,13 @@
*/
void set_integer_keyed_int (INTVAL key, INTVAL value) {
- SizeBooleanData *sd;
if (key < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableBooleanArray: index out of bounds!");
if(key >= PMC_int_val(SELF))
DYNSELF.set_integer_native(key+1);
- sd = (SizeBooleanData *)PMC_data(SELF);
- sd->data[key] = (value != 0);
- }
-
-/*
-
-=item C<void push_integer (INTVAL value)>
-
-Extends the array by adding an element of value C<value> to the end of
-the array.
-
-=cut
-
-*/
-
- void push_integer (INTVAL value) {
- INTVAL nextix = DYNSELF.elements();
- DYNSELF.set_integer_keyed_int(nextix, value);
+ SUPER(key, value);
}
/*
@@ -100,52 +90,27 @@
*/
void set_integer_native (INTVAL size) {
- SizeBooleanData *sd;
+ Parrot_UInt1 *sd;
if (size < 0)
internal_exception(OUT_OF_BOUNDS,
"ResizableBooleanArray: Can't resize!");
- sd = PMC_data(SELF);
- PMC_int_val(SELF) = size;
- if(sd == NULL) {
- sd = mem_sys_allocate(NEEDED_SIZE(size));
- sd->size = size;
- } else if(size >= sd->size) {
- sd->size = size < 2*sd->size ? sd->size*2 : size;
- sd = mem_sys_realloc(sd, NEEDED_SIZE(sd->size));
+ if ( ! PMC_data(SELF) ) {
+ SUPER(size);
+ } else if ( size > PMC_int_val2(SELF) ) {
+ INTVAL old_size = PMC_int_val2(SELF);
+ PMC_int_val2(SELF) = size < 2*old_size ? 2*old_size :
(size/BITS_PER_CHAR+1)*BITS_PER_CHAR;
+ Parrot_UInt1 *sd = PMC_data(SELF);
+ PMC_data(SELF) = mem_sys_realloc(sd,
PMC_int_val2(SELF)/BITS_PER_CHAR);
+ PMC_int_val(SELF) = size;
} else {
+ PMC_int_val(SELF) = size;
return;
}
- PMC_data(SELF) = sd;
PObj_active_destroy_SET(SELF);
}
-/*
-
-=item C<PMC *clone()>
-
-Creates and returns a copy of the array.
-
-=cut
-
-*/
-
- PMC* clone () {
- SizeBooleanData *sd;
- PMC * dest = pmc_new(INTERP, SELF->vtable->base_type);
-
- if (!PMC_data(SELF))
- return dest;
- PMC_int_val(dest) = PMC_int_val(SELF);
- sd = PMC_data(SELF);
-
- PMC_data(dest) = mem_sys_allocate(NEEDED_SIZE(sd->size));
- mem_sys_memcopy(PMC_data(dest), PMC_data(SELF),
NEEDED_SIZE(sd->size));
- PObj_active_destroy_SET(dest);
- return dest;
- }
-
}
/*
1.52 +15 -15 parrot/include/parrot/pobj.h
Index: pobj.h
===================================================================
RCS file: /cvs/public/parrot/include/parrot/pobj.h,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -r1.51 -r1.52
--- pobj.h 18 Mar 2005 13:40:13 -0000 1.51
+++ pobj.h 27 Mar 2005 12:35:23 -0000 1.52
@@ -1,7 +1,7 @@
/* pobj.h
- * Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
+ * Copyright: 2001-2005 The Perl Foundation. All Rights Reserved.
* CVS Info
- * $Id: pobj.h,v 1.51 2005/03/18 13:40:13 jrieks Exp $
+ * $Id: pobj.h,v 1.52 2005/03/27 12:35:23 bernhard Exp $
* Overview:
* Parrot Object data members and flags enum
* Data Structure and Algorithms:
@@ -41,26 +41,26 @@
typedef union UnionVal {
- struct { /* Buffers structure */
- void * _bufstart;
- size_t _buflen;
+ struct { /* One Buffer structure */
+ void * _bufstart;
+ size_t _buflen;
} _b;
- struct { /* PMC unionval members */
- DPOINTER* _struct_val; /* two ptrs, both are defines */
- PMC* _pmc_val;
+ struct { /* or two pointers, both are
defines */
+ DPOINTER * _struct_val;
+ PMC * _pmc_val;
} _ptrs;
struct {
- INTVAL _int_val; /* or 2 intvals */
+ INTVAL _int_val; /* or 2 intvals */
INTVAL _int_val2;
} _i;
- FLOATVAL _num_val;
- struct parrot_string_t * _string_val;
+ FLOATVAL _num_val; /* or one float */
+ struct parrot_string_t * _string_val; /* or a pointer to a string */
} UnionVal;
#define UVal_ptr(u) (u)._ptrs._struct_val
#define UVal_pmc(u) (u)._ptrs._pmc_val
#define UVal_int(u) (u)._i._int_val
-#define UVal_int2(u) (u)._i._int_val2
+#define UVal_int2(u) (u)._i._int_val2
#define UVal_num(u) (u)._num_val
#define UVal_str(u) (u)._string_val
#define UVal_bufstart(u) (u)._b._bufstart
@@ -118,9 +118,9 @@
typedef enum {
enum_stringrep_unknown = 0,
- enum_stringrep_one = 1,
- enum_stringrep_two = 2,
- enum_stringrep_four = 4
+ enum_stringrep_one = 1,
+ enum_stringrep_two = 2,
+ enum_stringrep_four = 4
} parrot_string_representation_t;
struct parrot_string_t {
1.8 +11 -12 parrot/t/pmc/resizablebooleanarray.t
Index: resizablebooleanarray.t
===================================================================
RCS file: /cvs/public/parrot/t/pmc/resizablebooleanarray.t,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- resizablebooleanarray.t 4 Mar 2005 17:49:11 -0000 1.7
+++ resizablebooleanarray.t 27 Mar 2005 12:35:24 -0000 1.8
@@ -1,14 +1,13 @@
-#! perl -w
-# Copyright: 2001-2003 The Perl Foundation. All Rights Reserved.
-# $Id: resizablebooleanarray.t,v 1.7 2005/03/04 17:49:11 bernhard Exp $
+# Copyright: 2001-2005 The Perl Foundation. All Rights Reserved.
+# $Id: resizablebooleanarray.t,v 1.8 2005/03/27 12:35:24 bernhard Exp $
=head1 NAME
-t/pmc/resizablebooleanarray.t - ResizableBooleanArray PMC
+t/pmc/resizablebooleanarray.t - testing the ResizableBooleanArray PMC
=head1 SYNOPSIS
- % perl -Ilib t/pmc/ResizableBooleanArray.t
+ % perl -Ilib t/pmc/resizablebooleanarray.t
=head1 DESCRIPTION
@@ -63,7 +62,7 @@
.endm
ENDOFMACRO
-output_is(<<'CODE', <<'OUTPUT', "Setting array size");
+pasm_output_is(<<'CODE', <<'OUTPUT', "Setting array size");
new P0, .ResizableBooleanArray
set I0,P0
@@ -103,7 +102,7 @@
ok 5
OUTPUT
-output_is(<<'CODE', <<'OUTPUT', "Setting first element");
+pasm_output_is(<<'CODE', <<'OUTPUT', "Setting first element");
new P0, .ResizableBooleanArray
set P0, 1
@@ -132,7 +131,7 @@
ok 3
OUTPUT
-output_is(<<'CODE', <<'OUTPUT', "Setting second element");
+pasm_output_is(<<'CODE', <<'OUTPUT', "Setting second element");
new P0, .ResizableBooleanArray
set P0, 2
@@ -163,7 +162,7 @@
# TODO: Rewrite these properly when we have exceptions
-output_is(<<'CODE', <<'OUTPUT', "Setting out-of-bounds elements");
+pasm_output_is(<<'CODE', <<'OUTPUT', "Setting out-of-bounds elements");
new P0, .ResizableBooleanArray
set P0[1], -7
@@ -191,7 +190,7 @@
ok 3
OUTPUT
-output_is(<<'CODE', <<'OUTPUT', "Getting out-of-bounds elements");
+pasm_output_is(<<'CODE', <<'OUTPUT', "Getting out-of-bounds elements");
new P0, .ResizableBooleanArray
set P0, 1
@@ -203,7 +202,7 @@
OUTPUT
-output_is(<<"CODE", <<'OUTPUT', "Set via PMC keys, access via INTs");
+pasm_output_is(<<"CODE", <<'OUTPUT', "Set via PMC keys, access via INTs");
@{[ $fp_equality_macro ]}
new P0, .ResizableBooleanArray
new P1, .Key
@@ -239,7 +238,7 @@
ok 3
OUTPUT
-output_is(<<"CODE", <<'OUTPUT', "Set via INTs, access via PMC Keys");
+pasm_output_is(<<"CODE", <<'OUTPUT', "Set via INTs, access via PMC Keys");
@{[ $fp_equality_macro ]}
new P0, .ResizableBooleanArray
set P0, 1