Compiles w/o warnings
[iotcloud.git] / version2 / src / C / IoTString.h
index bbd0c775df095b87fdb5a1748e3669bddc8220f6..2d7a14f9ef476b967ac79baef03a0464f199d7dd 100644 (file)
@@ -2,26 +2,53 @@
 #define IOTSTRING_H
 
 #include "array.h"
-
+#include <string.h>
 /**
  * IoTString wraps the underlying char string.
  * @author Brian Demsky <bdemsky@uci.edu>
  * @version 1.0
  */
 
-public class IoTString {
+inline int hashCharArray(Array<char> *array) {
+       uint len = array->length();
+       int hash = 0;
+       for (uint i = 0; i < len; i++) {
+               hash = 31 * hash + array->get(i);
+       }
+       return hash;
+}
+
+class IoTString {
 private:
        Array<char> *array;
        IoTString() {}
-
+       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.
         */
 
 public:
-       IoTString(Array<char> *_array) : array(new Array<char>(_array)) {}
-       ~IoTString() {}
+       IoTString(Array<char> *_array) :
+               array(new Array<char>(_array)),
+               hashvalue(hashCharArray(array)) {
+       }
+
+       IoTString(const char *_array) {
+               int32_t len = strlen(_array);
+               array = new Array<char>(len);
+               strcpy(array->internalArray(), _array);
+               hashvalue = hashCharArray(array);
+       }
+
+       IoTString(IoTString *string) :
+               array(new Array<char>(string->array)),
+               hashvalue(hashCharArray(array)) {
+       }
+
+       ~IoTString() {
+               delete array;
+       }
 
        /**
         * Internal method to grab a reference to our char array.  Caller
@@ -34,19 +61,38 @@ public:
         * Returns a copy of the underlying char string.
         */
 
-       Array<char> *getBytes() { return new Array<Char>(&array); }
+       Array<char> *getBytes() { return new Array<char>(array); }
 
        /**
         * Returns the length in chars of the IoTString.
         */
 
+       bool equals(IoTString *str) {
+               uint strlength = str->array->length();
+               uint thislength = array->length();
+               if (strlength != thislength)
+                       return false;
+
+               int result = memcmp(str->array->internalArray(), array->internalArray(), strlength);
+               return result == 0;
+       }
+
+       int hashValue() { return hashvalue;}
        int length() { return array->length(); }
        friend IoTString *IoTString_shallow(Array<char> *_array);
 };
 
-IoTString *IoTString_shallow(Array<char> *_array) {
+inline IoTString *IoTString_shallow(Array<char> *_array) {
        IoTString *str = new IoTString();
        str->array = _array;
        return str;
 }
+
+inline int hashString(IoTString *a) {
+       return a->hashValue();
+}
+
+inline bool StringEquals(IoTString *a, IoTString *b) {
+       return a->equals(b);
+}
 #endif