Merge pull request #335 from Microsoft/default
Default values for array
This commit is contained in:
		@@ -150,15 +150,9 @@ namespace pxt {
 | 
			
		||||
      
 | 
			
		||||
      if (i < length)
 | 
			
		||||
      {
 | 
			
		||||
        if (data[i] != Segment::MissingValue)
 | 
			
		||||
        {
 | 
			
		||||
          return data[i];          
 | 
			
		||||
        }
 | 
			
		||||
        error(ERR_MISSING_VALUE);
 | 
			
		||||
        return 0;
 | 
			
		||||
      }
 | 
			
		||||
      error(ERR_OUT_OF_BOUNDS);
 | 
			
		||||
      return 0;
 | 
			
		||||
      return Segment::DefaultValue;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void Segment::set(uint32_t i, uint32_t value) 
 | 
			
		||||
@@ -223,12 +217,8 @@ namespace pxt {
 | 
			
		||||
         {
 | 
			
		||||
           memcpy(tmp, data, size * sizeof(uint32_t));
 | 
			
		||||
         }
 | 
			
		||||
 | 
			
		||||
         //fill the rest with missing values;
 | 
			
		||||
         for(uint16_t i = size; i < newSize; i++)
 | 
			
		||||
         {
 | 
			
		||||
           tmp[i] = Segment::MissingValue;
 | 
			
		||||
         }
 | 
			
		||||
        //fill the rest with default value
 | 
			
		||||
         memset(tmp + size, Segment::DefaultValue, (newSize - size) * sizeof(uint32_t));
 | 
			
		||||
 | 
			
		||||
         //free older segment;
 | 
			
		||||
         ::operator delete(data); 
 | 
			
		||||
@@ -280,12 +270,11 @@ namespace pxt {
 | 
			
		||||
      if (length > 0)
 | 
			
		||||
      {
 | 
			
		||||
        uint32_t value = data[length];
 | 
			
		||||
        data[length] = Segment::MissingValue;
 | 
			
		||||
        data[length] = Segment::DefaultValue;
 | 
			
		||||
        --length;
 | 
			
		||||
        return value;
 | 
			
		||||
      }
 | 
			
		||||
      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
 | 
			
		||||
@@ -306,15 +295,14 @@ namespace pxt {
 | 
			
		||||
          memmove(data + i, data + i + 1, (length - i - 1) * sizeof(uint32_t));
 | 
			
		||||
        }
 | 
			
		||||
        length--;        
 | 
			
		||||
        data[length] = Segment::MissingValue;        
 | 
			
		||||
        data[length] = Segment::DefaultValue;        
 | 
			
		||||
#ifdef DEBUG_BUILD
 | 
			
		||||
        printf("After Segment::remove index:%u\n", i);
 | 
			
		||||
        this->print();
 | 
			
		||||
#endif  
 | 
			
		||||
        return ret;
 | 
			
		||||
      }
 | 
			
		||||
      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.     
 | 
			
		||||
@@ -360,32 +348,13 @@ namespace pxt {
 | 
			
		||||
 | 
			
		||||
    bool Segment::isValidIndex(uint32_t i)
 | 
			
		||||
    {
 | 
			
		||||
      if (i > length || data[i] == Segment::MissingValue)
 | 
			
		||||
      if (i > length)
 | 
			
		||||
      {
 | 
			
		||||
        return false;
 | 
			
		||||
      }
 | 
			
		||||
      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()
 | 
			
		||||
    {
 | 
			
		||||
#ifdef DEBUG_BUILD
 | 
			
		||||
@@ -415,29 +384,16 @@ namespace pxt {
 | 
			
		||||
 | 
			
		||||
    uint32_t RefCollection::getAt(int i) 
 | 
			
		||||
    {
 | 
			
		||||
      if (head.isValidIndex(i)) 
 | 
			
		||||
      uint32_t tmp = head.get(i);
 | 
			
		||||
      if (isRef())
 | 
			
		||||
      {
 | 
			
		||||
        uint32_t tmp = head.get(i);
 | 
			
		||||
        if (isRef())
 | 
			
		||||
        {
 | 
			
		||||
          incr(tmp);
 | 
			
		||||
        }
 | 
			
		||||
        return tmp;
 | 
			
		||||
      }
 | 
			
		||||
      else 
 | 
			
		||||
      {
 | 
			
		||||
        error(ERR_OUT_OF_BOUNDS);
 | 
			
		||||
        return 0;
 | 
			
		||||
        incr(tmp);
 | 
			
		||||
      }
 | 
			
		||||
      return tmp;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    uint32_t RefCollection::removeAt(int i) 
 | 
			
		||||
    {
 | 
			
		||||
      if (!head.isValidIndex((uint32_t)i))
 | 
			
		||||
      {
 | 
			
		||||
        error(ERR_OUT_OF_BOUNDS);
 | 
			
		||||
        return 0;
 | 
			
		||||
      }
 | 
			
		||||
      if (isRef())
 | 
			
		||||
      {
 | 
			
		||||
        decr(head.get(i));
 | 
			
		||||
@@ -447,18 +403,11 @@ namespace pxt {
 | 
			
		||||
 | 
			
		||||
    void RefCollection::insertAt(int i, uint32_t value) 
 | 
			
		||||
    {
 | 
			
		||||
      if (i < length())
 | 
			
		||||
      head.insert(i, value);
 | 
			
		||||
      if (isRef())
 | 
			
		||||
      {
 | 
			
		||||
        head.insert(i, value);
 | 
			
		||||
        if (isRef())
 | 
			
		||||
        {
 | 
			
		||||
          incr(value);
 | 
			
		||||
        } 
 | 
			
		||||
      }
 | 
			
		||||
      else
 | 
			
		||||
      {
 | 
			
		||||
        error(ERR_OUT_OF_BOUNDS);
 | 
			
		||||
      }
 | 
			
		||||
        incr(value);
 | 
			
		||||
      } 
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void RefCollection::setAt(int i, uint32_t value) 
 | 
			
		||||
@@ -480,26 +429,31 @@ namespace pxt {
 | 
			
		||||
      {
 | 
			
		||||
        StringData *xx = (StringData*)x;
 | 
			
		||||
        uint32_t i = start;
 | 
			
		||||
        while(head.getNextValidIndex(start, &i))
 | 
			
		||||
        while(head.isValidIndex(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;
 | 
			
		||||
          }
 | 
			
		||||
          start = i;
 | 
			
		||||
          i++;
 | 
			
		||||
        }
 | 
			
		||||
      } 
 | 
			
		||||
      else 
 | 
			
		||||
      {
 | 
			
		||||
        uint32_t i = start;
 | 
			
		||||
        while(head.getNextValidIndex(start, &i))
 | 
			
		||||
        while(head.isValidIndex(i))
 | 
			
		||||
        {
 | 
			
		||||
          if (head.get(i) == x)
 | 
			
		||||
          {
 | 
			
		||||
            return (int)i;
 | 
			
		||||
          }
 | 
			
		||||
          start = i;
 | 
			
		||||
          i++;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@@ -550,12 +504,9 @@ namespace pxt {
 | 
			
		||||
    {
 | 
			
		||||
      if (this->isRef())
 | 
			
		||||
      {
 | 
			
		||||
        uint32_t start = 0;
 | 
			
		||||
        uint32_t i = 0;
 | 
			
		||||
        while(head.getNextValidIndex(start, &i))
 | 
			
		||||
        for(uint32_t i = 0; i < this->head.getLength(); i++)
 | 
			
		||||
        {
 | 
			
		||||
          decr(this->head.get(i));
 | 
			
		||||
          start = i;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      this->head.destroy();
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,6 @@ namespace pxt {
 | 
			
		||||
    ERR_OUT_OF_BOUNDS = 8,
 | 
			
		||||
    ERR_REF_DELETED = 7,
 | 
			
		||||
    ERR_SIZE = 9,
 | 
			
		||||
    ERR_MISSING_VALUE = 10,
 | 
			
		||||
  } ERROR;
 | 
			
		||||
 | 
			
		||||
  extern const uint32_t functionsAndBytecode[];
 | 
			
		||||
@@ -175,7 +174,7 @@ namespace pxt {
 | 
			
		||||
      uint16_t size;
 | 
			
		||||
 | 
			
		||||
      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);      
 | 
			
		||||
      void growByMin(uint16_t minSize);
 | 
			
		||||
@@ -197,10 +196,6 @@ namespace pxt {
 | 
			
		||||
      uint32_t remove(uint32_t i);
 | 
			
		||||
      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);
 | 
			
		||||
 | 
			
		||||
      void destroy();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user