Support for default values in array.

This commit is contained in:
Abhijith 2017-01-08 18:43:14 -08:00
parent 7a98b06424
commit 91321dab83
2 changed files with 21 additions and 52 deletions

View File

@ -150,15 +150,10 @@ namespace pxt {
if (i < length) if (i < length)
{ {
if (data[i] != Segment::MissingValue)
{
return data[i]; return data[i];
}
error(ERR_MISSING_VALUE);
return 0;
} }
error(ERR_OUT_OF_BOUNDS); error(ERR_OUT_OF_BOUNDS);
return 0; return Segment::DefaultValue;
} }
void Segment::set(uint32_t i, uint32_t value) void Segment::set(uint32_t i, uint32_t value)
@ -223,12 +218,8 @@ namespace pxt {
{ {
memcpy(tmp, data, size * sizeof(uint32_t)); memcpy(tmp, data, size * sizeof(uint32_t));
} }
//fill the rest with default value
//fill the rest with missing values; memset(tmp + size, Segment::DefaultValue, (newSize - size) * sizeof(uint32_t));
for(uint16_t i = size; i < newSize; i++)
{
tmp[i] = Segment::MissingValue;
}
//free older segment; //free older segment;
::operator delete(data); ::operator delete(data);
@ -280,12 +271,12 @@ namespace pxt {
if (length > 0) if (length > 0)
{ {
uint32_t value = data[length]; uint32_t value = data[length];
data[length] = Segment::MissingValue; data[length] = Segment::DefaultValue;
--length; --length;
return value; return value;
} }
error(ERR_OUT_OF_BOUNDS); error(ERR_OUT_OF_BOUNDS);
return 0; return Segment::DefaultValue;
} }
//this function removes an element at index i and shifts the rest of the elements to //this function removes an element at index i and shifts the rest of the elements to
@ -306,7 +297,7 @@ namespace pxt {
memmove(data + i, data + i + 1, (length - i - 1) * sizeof(uint32_t)); memmove(data + i, data + i + 1, (length - i - 1) * sizeof(uint32_t));
} }
length--; length--;
data[length] = Segment::MissingValue; data[length] = Segment::DefaultValue;
#ifdef DEBUG_BUILD #ifdef DEBUG_BUILD
printf("After Segment::remove index:%u\n", i); printf("After Segment::remove index:%u\n", i);
this->print(); this->print();
@ -314,7 +305,7 @@ namespace pxt {
return ret; return ret;
} }
error(ERR_OUT_OF_BOUNDS); error(ERR_OUT_OF_BOUNDS);
return 0; return Segment::DefaultValue;
} }
//this function inserts element value at index i by shifting the rest of the elements right. //this function inserts element value at index i by shifting the rest of the elements right.
@ -360,32 +351,13 @@ namespace pxt {
bool Segment::isValidIndex(uint32_t i) bool Segment::isValidIndex(uint32_t i)
{ {
if (i > length || data[i] == Segment::MissingValue) if (i > length)
{ {
return false; return false;
} }
return true; return true;
} }
bool Segment::getNextValidIndex(uint32_t i, uint32_t *result)
{
while (i < length)
{
if (data[i] != Segment::MissingValue)
{
*result = i;
#ifdef DEBUG_BUILD
printf("In Segment::getNextValidIndex result=%u\n",i);
this->print();
#endif
return true;
}
i++;
}
return false;
}
void Segment::destroy() void Segment::destroy()
{ {
#ifdef DEBUG_BUILD #ifdef DEBUG_BUILD
@ -447,7 +419,7 @@ namespace pxt {
void RefCollection::insertAt(int i, uint32_t value) void RefCollection::insertAt(int i, uint32_t value)
{ {
if (i < length()) if (((uint32_t)i) < length())
{ {
head.insert(i, value); head.insert(i, value);
if (isRef()) if (isRef())
@ -480,26 +452,31 @@ namespace pxt {
{ {
StringData *xx = (StringData*)x; StringData *xx = (StringData*)x;
uint32_t i = start; uint32_t i = start;
while(head.getNextValidIndex(start, &i)) while(head.isValidIndex(i))
{ {
StringData *ee = (StringData*)head.get(i); StringData *ee = (StringData*)head.get(i);
if (xx->len == ee->len && memcmp(xx->data, ee->data, xx->len) == 0) if (ee == xx)
{
//handles ee being null
return (int) i;
}
if (ee && xx->len == ee->len && memcmp(xx->data, ee->data, xx->len) == 0)
{ {
return (int)i; return (int)i;
} }
start = i; i++;
} }
} }
else else
{ {
uint32_t i = start; uint32_t i = start;
while(head.getNextValidIndex(start, &i)) while(head.isValidIndex(i))
{ {
if (head.get(i) == x) if (head.get(i) == x)
{ {
return (int)i; return (int)i;
} }
start = i; i++;
} }
} }
@ -550,12 +527,9 @@ namespace pxt {
{ {
if (this->isRef()) if (this->isRef())
{ {
uint32_t start = 0; for(uint32_t i = 0; i < this->head.getLength(); i++)
uint32_t i = 0;
while(head.getNextValidIndex(start, &i))
{ {
decr(this->head.get(i)); decr(this->head.get(i));
start = i;
} }
} }
this->head.destroy(); this->head.destroy();

View File

@ -38,7 +38,6 @@ namespace pxt {
ERR_OUT_OF_BOUNDS = 8, ERR_OUT_OF_BOUNDS = 8,
ERR_REF_DELETED = 7, ERR_REF_DELETED = 7,
ERR_SIZE = 9, ERR_SIZE = 9,
ERR_MISSING_VALUE = 10,
} ERROR; } ERROR;
extern const uint32_t functionsAndBytecode[]; extern const uint32_t functionsAndBytecode[];
@ -175,7 +174,7 @@ namespace pxt {
uint16_t size; uint16_t size;
static const uint16_t MaxSize = 0xFFFF; static const uint16_t MaxSize = 0xFFFF;
static const uint32_t MissingValue = 0x80000000; static const uint32_t DefaultValue = 0x0;
static uint16_t growthFactor(uint16_t size); static uint16_t growthFactor(uint16_t size);
void growByMin(uint16_t minSize); void growByMin(uint16_t minSize);
@ -197,10 +196,6 @@ namespace pxt {
uint32_t remove(uint32_t i); uint32_t remove(uint32_t i);
void insert(uint32_t i, uint32_t value); void insert(uint32_t i, uint32_t value);
//Returns true if there is a valid index greater than or equal to 'i', returns false otherwise
//If 'i' is valid returns it in 'result', if not tries to find the next valid
//index < length which is valid.
bool getNextValidIndex(uint32_t i, uint32_t *result);
bool isValidIndex(uint32_t i); bool isValidIndex(uint32_t i);
void destroy(); void destroy();