X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=blobdiff_plain;f=version2%2Fsrc%2FC%2FIoTString.h;h=cb2d68b1af6dd13ddebf8ebcf2f95faef0bca912;hp=c90d46da44c84c582c83177b123cd978d32a3492;hb=97874483ce2547c8061cebaac2d1f14adfde86aa;hpb=a578eebaebcddd7f6b2a1b2edeed739bef657d5a diff --git a/version2/src/C/IoTString.h b/version2/src/C/IoTString.h index c90d46d..cb2d68b 100644 --- a/version2/src/C/IoTString.h +++ b/version2/src/C/IoTString.h @@ -2,27 +2,56 @@ #define IOTSTRING_H #include "array.h" - +#include /** * IoTString wraps the underlying char string. * @author Brian Demsky * @version 1.0 */ +inline int hashCharArray(Array *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 *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 *_array) : array(new Array(_array)) {} - ~IoTString() {} + IoTString(Array *_array) : + array(new Array(_array)), + hashvalue(hashCharArray(array)) { + } + + IoTString(const char *_array) { + int32_t len = strlen(_array); + array = new Array(len); + strcpy(array->internalArray(), _array); + hashvalue = hashCharArray(array); + } + + IoTString(IoTString *string) : + array(new Array(string->array)), + hashvalue(hashCharArray(array)) { + } + ~IoTString() { + delete array; + } + + char get(uint i) {return array->get(i);} + /** * Internal method to grab a reference to our char array. Caller * must not modify it. @@ -40,7 +69,7 @@ public: * Returns the length in chars of the IoTString. */ - bool equals(IoTString * str) { + bool equals(IoTString *str) { uint strlength = str->array->length(); uint thislength = array->length(); if (strlength != thislength) @@ -49,14 +78,23 @@ public: 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 *_array); }; -IoTString *IoTString_shallow(Array *_array) { +inline IoTString *IoTString_shallow(Array *_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