Exemple #1
0
//----------------------------------------------------------------
int SoArrayUID::FillAt(int nUID, const void* pElement)
{
	if (m_pElementBuff == 0)
	{
		return -1;
	}
	if (pElement == 0)
	{
		return -1;
	}
	if (nUID == -1)
	{
		nUID = FindFirstEmptyElement();
		if (nUID == -1)
		{
			if (ResizeArray(m_nCapacity + m_nAddCount) == false)
			{
				return -1;
			}
			nUID = m_nCapacity - m_nAddCount;
		}
	}
	if (nUID < 0 || nUID >= m_nCapacity)
	{
		return -1;
	}
	//
	SoTinyMemCpy(m_pElementBuff + nUID * m_nElementSize, pElement, m_nElementSize);
	m_pStatusBuff[nUID] = Status_Using;
	++m_nUsingElementCount;
	return nUID;
}
Exemple #2
0
bool VectorSet(vector_t v, size_t index, element_t e, element_t *prev) {
  assert(v != NULL);

  if (index >= v->length) {
    // need to increase array size.
    // attempt to allocate new array and return false if failure.
    size_t newLength = index+1;
    element_t *newArry = ResizeArray(v->arry, v->length, newLength);
    if (newArry == NULL)
      return false;

    // allocation successfull - free old array and replace with new one
    free(v->arry);
    v->arry = newArry;
    v->length = newLength;
  }

  // copy previous value at location index to caller, then store new
  // value and return success.
  // (note: if array was expanded, "previous value" will be NULL)
  *prev = v->arry[index];
  v->arry[index] = e;

  return true;
}
Exemple #3
0
//----------------------------------------------------------------
int SoArrayUID::TakeNew(void** ppElement)
{
	if (m_pElementBuff == 0)
	{
		return -1;
	}
	if (ppElement == 0)
	{
		return -1;
	}
	//
	int nUID = FindFirstEmptyElement();
	if (nUID == -1)
	{
		if (ResizeArray(m_nCapacity + m_nAddCount) == false)
		{
			return -1;
		}
		nUID = m_nCapacity - m_nAddCount;
	}
	if (nUID < 0 || nUID >= m_nCapacity)
	{
		return -1;
	}
	//
	*ppElement = m_pElementBuff + nUID * m_nElementSize;
	m_pStatusBuff[nUID] = Status_Using;
	++m_nUsingElementCount;
	return nUID;
}
Exemple #4
0
//----------------------------------------------------------------
bool SoArrayUID::InitArray(int nElementSize, int nInitCapacity, int nAddCount)
{
	m_nUsingElementCount = 0;
	m_nElementSize = nElementSize;
	m_nAddCount = nAddCount;
	return ResizeArray(nInitCapacity);
}
Exemple #5
0
	// Removes all chromosomes from the group
	void GaChromosomeGroup::Clear(bool dontRecycle/* = false*/)
	{
		// already empty?
		if( !_count )
			return;

		_hasShuffleBackup = false;

		// need shrinking?
		bool shrink = _sizable && ( 2 * _count <= _array.GetSize() );

		if( _recycleObjects && !dontRecycle && _population )
		{
			// recycle all chromosome objects using provided pool
			while( _count > 0 )
				_population->ReleaseStorageObject( _chromosomes[ --_count ] );
		}
		else if( _membershipFlag )
		{
			// chromosome objects are not recycled - just clear mebership flags
			while( _count > 0 )
				_chromosomes[ --_count ]->GetFlags().ClearFlags( _membershipFlag );
		}
		else
			_count = 0;

		// shrink size of array that will store chromosomes
		if( shrink )
			ResizeArray( _array.GetSize() / 2 );
	}
void CPatternContainer::OnModTypeChanged(const MODTYPE /*oldtype*/)
//-----------------------------------------------------------------
{
	const CModSpecifications specs = m_rSndFile.GetModSpecifications();
	if(specs.patternsMax < Size())
		ResizeArray(MAX(MAX_PATTERNS, specs.patternsMax));
	else if(Size() < MAX_PATTERNS)
		ResizeArray(MAX_PATTERNS);

	// remove pattern time signatures
	if(!specs.hasPatternSignatures)
	{
		for(PATTERNINDEX nPat = 0; nPat < m_Patterns.size(); nPat++)
		{
			m_Patterns[nPat].RemoveSignature();
		}
	}
}
void CPatternContainer::Init()
//----------------------------
{
	for(PATTERNINDEX i = 0; i < Size(); i++)
	{
		Remove(i);
	}

	ResizeArray(MAX_PATTERNS);
}
Exemple #8
0
void CPatternContainer::Init()
//----------------------------
{
    for(modplug::tracker::patternindex_t i = 0; i < Size(); i++)
    {
            Remove(i);
    }

    ResizeArray(MAX_PATTERNS);
}
Exemple #9
0
	// Shrinks size of array that stores chromosomes if it is possible and reasonable
	bool GaChromosomeGroup::Shrink()
	{
		// cannot shrink chromosome group with fixed size
		if( !_sizable )
			return false;

		// is it reasonable to shrink size?
		int newSize = _count * 2;
		if( newSize > _array.GetSize() )
		{
			ResizeArray( newSize );
			return true;
		}

		return false;
	}
Exemple #10
0
bool VectorSet(vector_t v, uint32_t index, element_t e, element_t *prev) {
  assert(v != NULL);

  if (index >= v->length) {
    size_t newLength = index+1;

    v->arry = ResizeArray(v->arry, v->length, newLength);
    v->length = newLength;
  } else {
    prev = v->arry[index];
  }
  
  v->arry[index] = e;

  return true;
}
Exemple #11
0
int main()
{
    char menu = ' ';
    int locationsLength = 0;
    int locationIndex = 0;
    printf("This program will a store the name, description, and long/lat coordinates of locations.\n");
    printf("Enter the amount of locations.\n");
    scanf("%d", &locationsLength);
    location *LocationArray = malloc(locationsLength * sizeof(location));
    //test to make sure malloc worked
    if(LocationArray == NULL){
        printf("Heap memory is exhausted.");
        exit(1);
    }

    while(1){
    printf("\nType A to add an additional location.\n");
    printf("Type P to print the current list of locations.\n");
    printf("Type Q to quit the program.\n");
    scanf(" %c", &menu);
    switch (menu) {
        case 'A':
        case 'a':
            if(locationIndex == locationsLength)
                ResizeArray(LocationArray, &locationsLength);
            addLoc(LocationArray, locationIndex);
            locationIndex++;
            break;
        case 'P':
        case 'p':
            printLoc(LocationArray, locationIndex);
            break;
        case 'Q':
        case 'q':
            printf("Exiting Program.\n");
            exit(1);
            break;
        default:
            printf("Choice is invalid. Try Again.\n");
            break;
            }
        }
    return 0;
}
Exemple #12
0
	// Sets new size of array that stores chromosomes
	void GaChromosomeGroup::SetSize(int size)
	{
		if( size != _array.GetSize() )
		{
			GA_ASSERT( Common::Exceptions::GaInvalidOperationException, !_sizable, "This chromosome group manage its size automatically.", "Population" );
			GA_ARG_ASSERT( Common::Exceptions::GaArgumentOutOfRangeException, size >= 0, "size", "Group size cannot be negative value.", "Population" );

			// with new size of group cannot store chromosmes?
			if( !size )
				// removes all chromosomes from the group
				Clear();
			else
			{
				int limit = 0;
				if( !_array.IsEmpty() )
				{
					limit = _array.GetSize() < _count ? _array.GetSize() : _count;

					if( _recycleObjects && _population )
					{
						// recycle chromosome objects using provided pool
						for( int i = limit; i < _count; i++ )
							_population->ReleaseStorageObject( _chromosomes[ i ] );
					}
					else if( _membershipFlag )
					{
						// chromosome objects are not recycled - just clear mebership flags
						for( int i = limit; i < _count; i++ )
							_chromosomes[ i ]->GetFlags().SetFlags( _membershipFlag );
					}
				}

				_count = limit;
				ResizeArray( size );
			}
		}
	}
Exemple #13
0
	// Increases size of array that stores chromosomes
	void GaChromosomeGroup::IncreaseSize()
	{
		// increase size of array
		int size = _array.GetSize() * 2;
		ResizeArray( size ? size : 4 );
	}