Array reimplementation as single segment with support for missing values

This commit is contained in:
Abhijith Chatra
2016-12-23 19:49:15 -08:00
parent 1014d2f361
commit 3fcbdbdd82
3 changed files with 320 additions and 51 deletions

View File

@ -1,7 +1,7 @@
#ifndef __PXT_H
#define __PXT_H
// #define DEBUG_MEMLEAKS 1
//#define DEBUG_MEMLEAKS 1
#pragma GCC diagnostic ignored "-Wunused-parameter"
@ -38,6 +38,7 @@ namespace pxt {
ERR_OUT_OF_BOUNDS = 8,
ERR_REF_DELETED = 7,
ERR_SIZE = 9,
ERR_MISSING_VALUE = 10,
} ERROR;
extern const uint32_t functionsAndBytecode[];
@ -167,11 +168,50 @@ namespace pxt {
}
};
class Segment {
private:
uint32_t* data;
uint16_t length;
uint16_t size;
static const uint16_t MaxSize = 0xFFFF;
static const uint32_t MissingValue = 0x80000000;
static uint16_t growthFactor(uint16_t size);
void growByMin(uint16_t minSize);
void growBy(uint16_t newSize);
public:
Segment() : data (nullptr), length(0), size(0) {};
uint32_t get(uint32_t i);
void set(uint32_t i, uint32_t value);
uint32_t getLength() { return length;};
void push(uint32_t value);
uint32_t pop();
void remove(uint32_t i);
//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);
void destroy();
void print();
};
// A ref-counted collection of either primitive or ref-counted objects (String, Image,
// user-defined record, another collection)
class RefCollection
: public RefObject
{
private:
Segment head;
public:
// 1 - collection of refs (need decr)
// 2 - collection of strings (in fact we always have 3, never 2 alone)
@ -179,20 +219,14 @@ namespace pxt {
inline bool isRef() { return getFlags() & 1; }
inline bool isString() { return getFlags() & 2; }
std::vector<uint32_t> data;
RefCollection(uint16_t f);
inline bool in_range(int x) {
return (0 <= x && x < (int)data.size());
}
inline int length() { return data.size(); }
void destroy();
void print();
uint32_t length() { return head.getLength();}
void push(uint32_t x);
uint32_t pop();
uint32_t getAt(int x);
void removeAt(int x);
void setAt(int x, uint32_t y);