You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/07/31 15:31:48 UTC

svn commit: r561325 - in /activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang: Integer.cpp Integer.h

Author: tabish
Date: Tue Jul 31 06:31:47 2007
New Revision: 561325

URL: http://svn.apache.org/viewvc?view=rev&rev=561325
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103

Implementing the Primitive Wrappers fully

Modified:
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.h

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.cpp?view=diff&rev=561325&r1=561324&r2=561325
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.cpp Tue Jul 31 06:31:47 2007
@@ -87,8 +87,6 @@
 ////////////////////////////////////////////////////////////////////////////////
 std::string Integer::toString( int value, int radix ) {
 
-    std::string result = "";
-
     if( radix < Character::MIN_RADIX || radix > Character::MAX_RADIX ) {
         radix = 10;
     }
@@ -108,8 +106,10 @@
         count++;
     }
 
-    char* buffer = new char[count];
-    const int length = count;
+    // Save length and allocate a new buffer for the string, add one
+    // more for the null character.
+    int length = count;
+    char* buffer = new char[length + 1];
 
     do {
         int ch = 0 - ( j % radix );
@@ -125,8 +125,111 @@
         buffer[0] = '-';
     }
 
-    result.append( &buffer[0], length );
-    delete buffer;
+    // Ensure there's a null
+    buffer[length] = 0;
+    std::string result( &buffer[0] );
+    delete [] buffer;
+
+    return result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string toBinaryString( int value ) {
+
+    int count = 1;
+    int j = value;
+
+    if( value < 0 ) {
+        count = 32;
+    } else {
+        while ( (j >>= 1) != 0) {
+            count++;
+        }
+    }
+
+    // Save length and allocate a new buffer for the string, add one
+    // more for the null character.
+    int length = count;
+    char* buffer = new char[length + 1];
+
+    do {
+        buffer[--count] = (char)( (value & 1) + '0' );
+        value >>= 1;
+    } while( count > 0 );
+
+    // Ensure there's a null
+    buffer[length] = 0;
+    std::string result( &buffer[0] );
+    delete [] buffer;
+
+    return result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string toOctalString( int value ) {
+
+    int count = 1, j = value;
+
+    if( value < 0 ) {
+        count = 11;
+    } else {
+        while ( (j >>= 3) != 0 ) {
+            count++;
+        }
+    }
+
+    // Save length and allocate a new buffer for the string, add one
+    // more for the null character.
+    int length = count;
+    char* buffer = new char[length + 1];
+
+    do {
+        buffer[--count] = (char)( (value & 7) + '0' );
+        value >>= 3;
+    } while( count > 0 );
+
+    // Ensure there's a null
+    buffer[length] = 0;
+    std::string result( &buffer[0] );
+    delete [] buffer;
+
+    return result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string toHexString( int value ) {
+
+    int count = 1;
+    int j = value;
+
+    if( value < 0 ) {
+        count = 8;
+    } else {
+        while( (j >>= 4) != 0 ) {
+            count++;
+        }
+    }
+
+    // Save length and allocate a new buffer for the string, add one
+    // more for the null character.
+    int length = count;
+    char* buffer = new char[length + 1];
+
+    do {
+        int t = value & 15;
+        if( t > 9 ) {
+            t = t - 10 + 'a';
+        } else {
+            t += '0';
+        }
+        buffer[--count] = (char)t;
+        value >>= 4;
+    } while( count > 0 );
+
+    // Ensure there's a null
+    buffer[length] = 0;
+    std::string result( &buffer[0] );
+    delete [] buffer;
 
     return result;
 }
@@ -275,4 +378,51 @@
         }
     }
     return result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int Integer::highestOneBit( int value ) {
+
+    value |= (value >> 1);
+    value |= (value >> 2);
+    value |= (value >> 4);
+    value |= (value >> 8);
+    value |= (value >> 16);
+    return ( value & ~(value >> 1));
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int Integer::lowestOneBit( int value ) {
+    return ( value & (-value) );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int Integer::numberOfLeadingZeros( int value ) {
+
+    value |= value >> 1;
+    value |= value >> 2;
+    value |= value >> 4;
+    value |= value >> 8;
+    value |= value >> 16;
+    return Integer::bitCount( ~value );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int Integer::numberOfTrailingZeros( int value ) {
+    return bitCount( (value & -value) - 1 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int Integer::rotateLeft( int value ) {
+    return value; // TODO
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int Integer::rotateRight( int value ) {
+    return value; // TODO
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int Integer::signum( int value ) {
+    return ( value == 0 ? 0 : ( value < 0 ? -1 : 1 ) );
 }

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.h?view=diff&rev=561325&r1=561324&r2=561325
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Integer.h Tue Jul 31 06:31:47 2007
@@ -285,6 +285,162 @@
          */
         static std::string toString( int value, int radix );
 
+        /**
+         * Returns a string representation of the integer argument as an unsigned
+         * integer in base 16.
+         *
+         * The unsigned integer value is the argument plus 2^32 if the argument is
+         * negative; otherwise, it is equal to the argument. This value is converted
+         * to a string of ASCII digits in hexadecimal (base 16) with no extra leading
+         * 0s. If the unsigned magnitude is zero, it is represented by a single zero
+         * character '0'; otherwise, the first character of the representation of the
+         * unsigned magnitude will not be the zero character. The following characters
+         * are used as hexadecimal digits:
+         *
+         * 		0123456789abcdef
+         *
+         * If uppercase letters are desired, the toUpperCase() method may be called
+         * on the result:
+         * @param value - the int to be translated to an Octal string
+         * @returns the unsigned int value as a Octal string
+         */
+        static std::string toHexString( int value );
+
+        /**
+         * Returns a string representation of the integer argument as an unsigned
+         * integer in base 8.
+         *
+         * The unsigned integer value is the argument plus 2^32 if the argument is
+         * negative; otherwise, it is equal to the argument. This value is converted
+         * to a string of ASCII digits in octal (base 8) with no extra leading 0s.
+         *
+         * If the unsigned magnitude is zero, it is represented by a single zero
+         * character '0'; otherwise, the first character of the representation
+         * of the unsigned magnitude will not be the zero character. The following
+         * characters are used as octal digits:
+         *
+         *      01234567
+         *
+         * @param value - the int to be translated to an Octal string
+         * @returns the unsigned int value as a Octal string
+         */
+        static std::string toOctalString( int value );
+
+        /**
+         * Returns a string representation of the integer argument as an unsigned
+         * integer in base 2.
+         *
+         * The unsigned integer value is the argument plus 2^32 if the argument is
+         * negative; otherwise it is equal to the argument. This value is converted
+         * to a string of ASCII digits in binary (base 2) with no extra leading 0s.
+         * If the unsigned magnitude is zero, it is represented by a single zero
+         * character '0' ('\u0030'); otherwise, the first character of the
+         * representation of the unsigned magnitude will not be the zero character.
+         * The characters '0' ('\u0030') and '1' ('\u0031') are used as binary
+         * digits.
+         * @param value - the int to be translated to a binary string
+         * @returns the unsigned int value as a binary string
+         */
+        static std::string toBinaryString( int value );
+
+        /**
+         * Returns an int value with at most a single one-bit, in the position of
+         * the highest-order ("leftmost") one-bit in the specified int value.
+         * Returns zero if the specified value has no one-bits in its two's
+         * complement binary representation, that is, if it is equal to zero.
+         * @param value - the int to be inspected
+         * @return an int value with a single one-bit, in the position of the
+         * highest-order one-bit in the specified value, or zero if the specified
+         * value is itself equal to zero.
+         */
+        static int highestOneBit( int value );
+
+        /**
+         * Returns an int value with at most a single one-bit, in the position of
+         * the lowest-order ("rightmost") one-bit in the specified int value.
+         * Returns zero if the specified value has no one-bits in its two's
+         * complement binary representation, that is, if it is equal to zero.
+         * @param value - the int to be inspected
+         * @return an int value with a single one-bit, in the position of the
+         * lowest-order one-bit in the specified value, or zero if the specified
+         * value is itself equal to zero.
+         */
+        static int lowestOneBit( int value );
+
+        /**
+         * Returns the number of zero bits preceding the highest-order ("leftmost")
+         * one-bit in the two's complement binary representation of the specified
+         * int value. Returns 32 if the specified value has no one-bits in its two's
+         * complement representation, in other words if it is equal to zero.
+         *
+         * Note that this method is closely related to the logarithm base 2. For
+         * all positive int values x:
+         *
+         *     * floor( log2(x)) = 31 - numberOfLeadingZeros(x)
+         *     * ceil( log2(x)) = 32 - numberOfLeadingZeros(x - 1)
+         *
+         * @param value - the int to be inspected
+         * @return the number of zero bits preceding the highest-order ("leftmost")
+         * one-bit in the two's complement binary representation of the specified
+         * int value, or 32 if the value is equal to zero.
+         */
+        static int numberOfLeadingZeros( int value );
+
+        /**
+         * Returns the number of zero bits following the lowest-order ("rightmost")
+         * one-bit in the two's complement binary representation of the specified
+         * int value. Returns 32 if the specified value has no one-bits in its
+         * two's complement representation, in other words if it is equal to zero.
+         * @param value - the int to be inspected
+         * @return the number of zero bits following the lowest-order ("rightmost")
+         * one-bit in the two's complement binary representation of the specified
+         * int value, or 32 if the value is equal to zero.
+         */
+        static int numberOfTrailingZeros( int value );
+
+        /**
+         * Returns the value obtained by rotating the two's complement binary
+         * representation of the specified int value left by the specified number
+         * of bits. (Bits shifted out of the left hand, or high-order, side reenter
+         * on the right, or low-order.)
+         *
+         * Note that left rotation with a negative distance is equivalent to right
+         * rotation: rotateLeft(val, -distance) == rotateRight(val, distance). Note
+         * also that rotation by any multiple of 32 is a no-op, so all but the last
+         * five bits of the rotation distance can be ignored, even if the distance
+         * is negative: rotateLeft(val, distance) == rotateLeft(val, distance & 0x1F).
+         * @param value - the int to be inspected
+         * @return
+         */
+        static int rotateLeft( int value );
+
+        /**
+         * Returns the value obtained by rotating the two's complement binary
+         * representation of the specified int value right by the specified number
+         * of bits. (Bits shifted out of the right hand, or low-order, side reenter
+         * on the left, or high-order.)
+         *
+         * Note that right rotation with a negative distance is equivalent to left
+         * rotation: rotateRight(val, -distance) == rotateLeft(val, distance). Note
+         * also that rotation by any multiple of 32 is a no-op, so all but the last
+         * five bits of the rotation distance can be ignored, even if the distance is
+         * negative: rotateRight(val, distance) == rotateRight(val, distance & 0x1F).
+         * @param value - the int to be inspected
+         * @return the value obtained by rotating the two's complement binary
+         * representation of the specified int value right by the specified number
+         * of bits.
+         */
+        static int rotateRight( int value );
+
+        /**
+         * Returns the signum function of the specified int value. (The return value
+         * is -1 if the specified value is negative; 0 if the specified value is zero;
+         * and 1 if the specified value is positive.)
+         * @param value - the int to be inspected
+         * @return the signum function of the specified int value.
+         */
+        static int signum( int value );
+
     private:
 
         static int parse( const std::string& value, int offset,