1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-02-03 03:30:06 +00:00

Removed the (rather pointless) granularity value from the array objects. Converted a few macros into functions and other misc code clean-ups.

This commit is contained in:
Julian Storer 2010-01-13 18:58:40 +00:00
parent c368805559
commit 97035bb3a1
69 changed files with 218 additions and 369 deletions

View file

@ -56,15 +56,9 @@ class Array
{
public:
//==============================================================================
/** Creates an empty array.
@param granularity this is the size of increment by which the internal storage
used by the array will grow. Only change it from the default if you know the
array is going to be very big and needs to be able to grow efficiently.
*/
Array (const int granularity = juceDefaultArrayGranularity) throw()
: data (granularity),
numUsed (0)
/** Creates an empty array. */
Array() throw()
: numUsed (0)
{
}
@ -72,7 +66,6 @@ public:
@param other the array to copy
*/
Array (const Array<ElementType, TypeOfCriticalSectionToUse>& other) throw()
: data (other.data.granularity)
{
other.lockArray();
numUsed = other.numUsed;
@ -86,8 +79,7 @@ public:
@param values the array to copy from
*/
Array (const ElementType* values) throw()
: data (juceDefaultArrayGranularity),
numUsed (0)
: numUsed (0)
{
while (*values != 0)
add (*values++);
@ -99,8 +91,7 @@ public:
@param numValues the number of values in the array
*/
Array (const ElementType* values, int numValues) throw()
: data (juceDefaultArrayGranularity),
numUsed (numValues)
: numUsed (numValues)
{
data.setAllocatedSize (numValues);
memcpy (data.elements, values, numValues * sizeof (ElementType));
@ -121,7 +112,6 @@ public:
other.lockArray();
lock.enter();
data.granularity = other.data.granularity;
data.ensureAllocatedSize (other.size());
numUsed = other.numUsed;
memcpy (data.elements, other.data.elements, numUsed * sizeof (ElementType));
@ -977,19 +967,7 @@ public:
void minimiseStorageOverheads() throw()
{
lock.enter();
if (numUsed == 0)
{
data.setAllocatedSize (0);
}
else
{
const int newAllocation = data.granularity * (numUsed / data.granularity + 1);
if (newAllocation < data.numAllocated)
data.setAllocatedSize (newAllocation);
}
data.shrinkToNoMoreThan (numUsed);
lock.exit();
}
@ -1001,7 +979,9 @@ public:
*/
void ensureStorageAllocated (const int minNumElements) throw()
{
lock.enter();
data.ensureAllocatedSize (minNumElements);
lock.exit();
}
//==============================================================================

View file

@ -27,14 +27,6 @@
#define __JUCE_ARRAYALLOCATIONBASE_JUCEHEADER__
//==============================================================================
/** The default size of chunk in which arrays increase their storage.
Used by ArrayAllocationBase and its subclasses.
*/
const int juceDefaultArrayGranularity = 8;
//==============================================================================
/**
Implements some basic array storage allocation functions.
@ -49,17 +41,11 @@ class ArrayAllocationBase
{
public:
//==============================================================================
/** Creates an empty array.
@param granularity_ this is the size of increment by which the internal storage
will be increased.
*/
ArrayAllocationBase (const int granularity_) throw()
/** Creates an empty array. */
ArrayAllocationBase() throw()
: elements (0),
numAllocated (0),
granularity (granularity_)
numAllocated (0)
{
jassert (granularity > 0);
}
/** Destructor. */
@ -114,25 +100,21 @@ public:
void ensureAllocatedSize (int minNumElements) throw()
{
if (minNumElements > numAllocated)
{
// for arrays with small granularity that get big, start
// increasing the size in bigger jumps
if (minNumElements > (granularity << 6))
{
minNumElements += (minNumElements / granularity);
if (minNumElements > (granularity << 8))
minNumElements += granularity << 6;
else
minNumElements += granularity << 5;
}
setAllocatedSize ((minNumElements + minNumElements / 2 + 8) & ~7);
}
setAllocatedSize (granularity * (minNumElements / granularity + 1));
}
/** Minimises the amount of storage allocated so that it's no more than
the given number of elements.
*/
void shrinkToNoMoreThan (int maxNumElements) throw()
{
if (maxNumElements < numAllocated)
setAllocatedSize (maxNumElements);
}
//==============================================================================
ElementType* elements;
int numAllocated, granularity;
int numAllocated;
private:
ArrayAllocationBase (const ArrayAllocationBase&);

View file

@ -145,6 +145,12 @@ void MemoryBlock::ensureSize (const int minimumSize,
setSize (minimumSize, initialiseToZero);
}
void MemoryBlock::swapWith (MemoryBlock& other) throw()
{
swapVariables (size, other.size);
data.swapWith (other.data);
}
//==============================================================================
void MemoryBlock::fillWith (const uint8 value) throw()
{

View file

@ -151,6 +151,11 @@ public:
void append (const void* const data,
const int numBytes) throw();
/** Exchanges the contents of this and another memory block.
No actual copying is required for this, so it's very fast.
*/
void swapWith (MemoryBlock& other) throw();
//==============================================================================
/** Copies data into this MemoryBlock from a memory address.

View file

@ -58,15 +58,9 @@ class OwnedArray
{
public:
//==============================================================================
/** Creates an empty array.
@param granularity this is the size of increment by which the internal storage
used by the array will grow. Only change it from the default if you know the
array is going to be very big and needs to be able to grow efficiently.
*/
OwnedArray (const int granularity = juceDefaultArrayGranularity) throw()
: data (granularity),
numUsed (0)
/** Creates an empty array. */
OwnedArray() throw()
: numUsed (0)
{
}
@ -674,19 +668,7 @@ public:
void minimiseStorageOverheads() throw()
{
lock.enter();
if (numUsed == 0)
{
data.setAllocatedSize (0);
}
else
{
const int newAllocation = data.granularity * (numUsed / data.granularity + 1);
if (newAllocation < data.numAllocated)
data.setAllocatedSize (newAllocation);
}
data.shrinkToNoMoreThan (numUsed);
lock.exit();
}
@ -698,7 +680,9 @@ public:
*/
void ensureStorageAllocated (const int minNumElements) throw()
{
lock.enter();
data.ensureAllocatedSize (minNumElements);
lock.exit();
}
//==============================================================================

View file

@ -51,22 +51,15 @@ class ReferenceCountedArray
public:
//==============================================================================
/** Creates an empty array.
@param granularity this is the size of increment by which the internal storage
used by the array will grow. Only change it from the default if you know the
array is going to be very big and needs to be able to grow efficiently.
@see ReferenceCountedObject, Array, OwnedArray
*/
ReferenceCountedArray (const int granularity = juceDefaultArrayGranularity) throw()
: data (granularity),
numUsed (0)
ReferenceCountedArray() throw()
: numUsed (0)
{
}
/** Creates a copy of another array */
ReferenceCountedArray (const ReferenceCountedArray<ObjectClass, TypeOfCriticalSectionToUse>& other) throw()
: data (other.data.granularity)
{
other.lockArray();
numUsed = other.numUsed;
@ -93,7 +86,6 @@ public:
clear();
data.granularity = other.granularity;
data.ensureAllocatedSize (other.numUsed);
numUsed = other.numUsed;
memcpy (data.elements, other.data.elements, numUsed * sizeof (ObjectClass*));
@ -738,19 +730,7 @@ public:
void minimiseStorageOverheads() throw()
{
lock.enter();
if (numUsed == 0)
{
data.setAllocatedSize (0);
}
else
{
const int newAllocation = data.granularity * (numUsed / data.granularity + 1);
if (newAllocation < data.numAllocated)
data.setAllocatedSize (newAllocation);
}
data.shrinkToNoMoreThan (numUsed);
lock.exit();
}

View file

@ -62,15 +62,9 @@ class SortedSet
{
public:
//==============================================================================
/** Creates an empty set.
@param granularity this is the size of increment by which the internal storage
used by the array will grow. Only change it from the default if you know the
array is going to be very big and needs to be able to grow efficiently.
*/
SortedSet (const int granularity = juceDefaultArrayGranularity) throw()
: data (granularity),
numUsed (0)
/** Creates an empty set. */
SortedSet() throw()
: numUsed (0)
{
}
@ -78,7 +72,6 @@ public:
@param other the set to copy
*/
SortedSet (const SortedSet<ElementType, TypeOfCriticalSectionToUse>& other) throw()
: data (other.data.granularity)
{
other.lockSet();
numUsed = other.numUsed;
@ -102,7 +95,6 @@ public:
other.lockSet();
lock.enter();
data.granularity = other.data.granularity;
data.ensureAllocatedSize (other.size());
numUsed = other.numUsed;
memcpy (data.elements, other.data.elements, numUsed * sizeof (ElementType));
@ -572,19 +564,7 @@ public:
void minimiseStorageOverheads() throw()
{
lock.enter();
if (numUsed == 0)
{
data.setAllocatedSize (0);
}
else
{
const int newAllocation = data.granularity * (numUsed / data.granularity + 1);
if (newAllocation < data.numAllocated)
data.setAllocatedSize (newAllocation);
}
data.shrinkToNoMoreThan (numUsed);
lock.exit();
}

View file

@ -145,6 +145,11 @@ void Value::setValue (const var& newValue)
value->setValue (newValue);
}
const String Value::toString() const
{
return value->getValue().toString();
}
const Value& Value::operator= (const var& newValue)
{
value->setValue (newValue);

View file

@ -76,6 +76,12 @@ public:
/** Returns the current value. */
operator const var() const;
/** Returns the value as a string.
This is alternative to writing things like "myValue.getValue().toString()".
*/
const String toString() const;
/** Sets the current value.
You can also use operator= to set the value.