X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=version2%2Fsrc%2FC%2FIoTString.h;h=99e45bb2f287be3ce0e4e549ca812819b8b54ea1;hb=be9ebe0770dcfee9a6a48623c542ea53b75dd83b;hp=bd838bb3ce8626634998ce586d8f55a3ff99cbbc;hpb=34ca5d4113dbbd0b5342abd280e2f9e11e7cd3b7;p=iotcloud.git diff --git a/version2/src/C/IoTString.h b/version2/src/C/IoTString.h index bd838bb..99e45bb 100644 --- a/version2/src/C/IoTString.h +++ b/version2/src/C/IoTString.h @@ -3,17 +3,18 @@ #include "array.h" #include +#include /** * IoTString wraps the underlying char string. * @author Brian Demsky * @version 1.0 */ -inline int hashCharArray(Array *array) { +inline unsigned int hashCharArray(Array *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; } @@ -21,8 +22,10 @@ inline int hashCharArray(Array *array) { class IoTString { private: Array *array; - IoTString() {} - int hashvalue; + unsigned int hashvalue; + unsigned int refCount; + IoTString() : refCount (1) {} + /** * Builds an IoTString object around the char array. This * constructor makes a copy, so the caller is free to modify the char array. @@ -31,25 +34,45 @@ private: public: IoTString(Array *_array) : array(new Array(_array)), - hashvalue(hashCharArray(array)) { + hashvalue(hashCharArray(array)), + refCount(1) { } IoTString(const char *_array) { int32_t len = strlen(_array); array = new Array(len); - strcpy(array->internalArray(), _array); + memcpy(array->internalArray(), _array, len); hashvalue = hashCharArray(array); + refCount = 1; } IoTString(IoTString *string) : - array(new Array(string->array)), - hashvalue(hashCharArray(array)) { + array(new Array(string->array)), + hashvalue(string->hashvalue), + refCount(1) { + } + + IoTString * acquireRef() { + refCount++; + return this; } + void releaseRef() { + if ((--refCount) == 0) + delete this; + } + ~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 +90,7 @@ public: * Returns the length in chars of the IoTString. */ + bool equals(IoTString *str) { uint strlength = str->array->length(); uint thislength = array->length(); @@ -77,18 +101,19 @@ public: return result == 0; } - int hashValue() { return hashvalue;} + 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; + str->hashvalue = hashCharArray(_array); return str; } -inline int hashString(IoTString *a) { +inline unsigned int hashString(IoTString *a) { return a->hashValue(); }