X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=blobdiff_plain;f=version2%2Fsrc%2FC%2FIoTString.h;h=1fb527f03a9977233377a1796c0c2c47e656993c;hp=bbd0c775df095b87fdb5a1748e3669bddc8220f6;hb=b2bc9b5c707bd7d932d60cd4e8c1cb580b36b5b4;hpb=0b9aca2b62c74f68652b170a92271a98d5b96666 diff --git a/version2/src/C/IoTString.h b/version2/src/C/IoTString.h index bbd0c77..1fb527f 100644 --- a/version2/src/C/IoTString.h +++ b/version2/src/C/IoTString.h @@ -2,26 +2,55 @@ #define IOTSTRING_H #include "array.h" - +#include /** * IoTString wraps the underlying char string. * @author Brian Demsky * @version 1.0 */ -public class IoTString { +inline unsigned int hashCharArray(Array *array) { + uint len = array->length(); + unsigned int hash = 0; + for (uint i = 0; i < len; i++) { + hash = 31 * hash + array->get(i); + } + return hash; +} + +class IoTString { private: Array *array; IoTString() {} - + 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. */ 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); + memcpy(array->internalArray(), _array, len); + 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 @@ -34,19 +63,38 @@ public: * Returns a copy of the underlying char string. */ - Array *getBytes() { return new Array(&array); } + Array *getBytes() { return new Array(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; + } + + unsigned 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 unsigned int hashString(IoTString *a) { + return a->hashValue(); +} + +inline bool StringEquals(IoTString *a, IoTString *b) { + return a->equals(b); +} #endif