edits
[iotcloud.git] / version2 / src / C / IoTString.h
old mode 100644 (file)
new mode 100755 (executable)
index 2d7a14f..583a098
@@ -3,17 +3,18 @@
 
 #include "array.h"
 #include <string.h>
+#include <stdio.h>
 /**
  * IoTString wraps the underlying char string.
  * @author Brian Demsky <bdemsky@uci.edu>
  * @version 1.0
  */
 
-inline int hashCharArray(Array<char> *array) {
+inline unsigned int hashCharArray(Array<char> *array) {
        uint len = array->length();
-       int hash = 0;
+       unsigned int hash = 0;
        for (uint i = 0; i < len; i++) {
-               hash = 31 * hash + array->get(i);
+               hash = 31 * hash + ((unsigned int) array->get(i));
        }
        return hash;
 }
@@ -22,7 +23,7 @@ class IoTString {
 private:
        Array<char> *array;
        IoTString() {}
-       int hashvalue;
+       unsigned int hashvalue;
        /**
         * Builds an IoTString object around the char array.  This
         * constructor makes a copy, so the caller is free to modify the char array.
@@ -37,19 +38,26 @@ public:
        IoTString(const char *_array) {
                int32_t len = strlen(_array);
                array = new Array<char>(len);
-               strcpy(array->internalArray(), _array);
+               memcpy(array->internalArray(), _array, len);
                hashvalue = hashCharArray(array);
        }
 
        IoTString(IoTString *string) :
-               array(new Array<char>(string->array)),
-               hashvalue(hashCharArray(array)) {
+         array(new Array<char>(string->array)),
+               hashvalue(string->hashvalue) {
        }
 
        ~IoTString() {
                delete array;
        }
 
+       void print() {
+               for(uint i=0; i < array->length(); i++)
+                       printf("%c", array->get(i));
+       };
+       
+       char get(uint i) {return array->get(i);}
+
        /**
         * Internal method to grab a reference to our char array.  Caller
         * must not modify it.
@@ -67,6 +75,7 @@ public:
         * Returns the length in chars of the IoTString.
         */
 
+       
        bool equals(IoTString *str) {
                uint strlength = str->array->length();
                uint thislength = array->length();
@@ -77,7 +86,7 @@ public:
                return result == 0;
        }
 
-       int hashValue() { return hashvalue;}
+       unsigned int hashValue() { return hashvalue;}
        int length() { return array->length(); }
        friend IoTString *IoTString_shallow(Array<char> *_array);
 };
@@ -85,10 +94,11 @@ public:
 inline IoTString *IoTString_shallow(Array<char> *_array) {
        IoTString *str = new IoTString();
        str->array = _array;
+       str->hashvalue = hashCharArray(_array);
        return str;
 }
 
-inline int hashString(IoTString *a) {
+inline unsigned int hashString(IoTString *a) {
        return a->hashValue();
 }