📄 charray.h
字号:
{
if (mLogicalLen == cpr.mLogicalLen) {
for (register int i = 0; i < mLogicalLen; i++)
if (mpArray[i] != cpr.mpArray[i])
return false;
return true;
}
return false;
}
// Sets all the elements within the logical-length of the array,
// (that is, elements 0..length()-1), to `value'.
//
template <class T> ChArray<T>&
ChArray<T>::setAll(const T& value)
{
for (register int i = 0; i < mLogicalLen; i++) {
mpArray[i] = value;
}
return *this;
}
// Appends the `otherArray' to the end of this array. The logical length of
// this array will increase by the length of the `otherArray'. If the
// physical length is not long enough it will increase by the amount
// necessary to fit the newly added elements (with the usual caveat about
// insufficient memory).
//
template <class T> ChArray<T>&
ChArray<T>::append(const ChArray<T>& otherArray)
{
int otherLen = otherArray.length();
if (otherLen == 0) {
return *this;
}
int newLen = mLogicalLen + otherLen;
if (newLen > mPhysicalLen) {
setPhysicalLength(newLen);
}
for (register int i=mLogicalLen; i<newLen; i++) {
mpArray[i] = otherArray.mpArray[i-mLogicalLen];
}
mLogicalLen = newLen;
return *this;
}
// Inserts `value' at `index'. The value formerly at `index'
// gets moved to `index+1', `index+1 gets moved to `index+2' and so on.
// Note that insertAt(length(), value) is equivalent to append(value).
// The logical length of the array will increase by one. If the physical
// length is not long enough it will increase by the grow length (with the
// usual caveat about insufficient memory).
//
template <class T> ChArray<T>&
ChArray<T>::insertAt(int index, const T& value)
{
T tmp(value);
assert(index >= 0 && index <= mLogicalLen);
if (mLogicalLen >= mPhysicalLen) {
int growth = (mLogicalLen * sizeof(T)) < ARRAY_GROWTH_THRESHOLD ?
mLogicalLen : ARRAY_GROWTH_THRESHOLD / sizeof(T);
setPhysicalLength(mLogicalLen + __max(growth, mGrowLen));
}
if (index != mLogicalLen) {
register T* p = mpArray + mLogicalLen;
register T* pStop = mpArray + index;
do {
*p = *(p-1);
} while (--p != pStop);
}
mpArray[index] = tmp;
mLogicalLen++;
return *this;
}
// Removes the element at `index'. The logical length will
// decrease by one. `index' MUST BE within bounds.
//
template <class T> ChArray<T>&
ChArray<T>::removeAt(int index)
{
assert(isValid(index));
// Shift array elements to the left if needed.
//
if (index < mLogicalLen - 1) {
register T* p = mpArray + index;
register T* pStop = mpArray + mLogicalLen - 1;
do {
*p = *(p+1);
} while (++p != pStop);
}
mLogicalLen--;
return *this;
}
// Removes all elements starting with 'startIndex' and ending with 'endIndex'
// The logical length will decrease by number of removed elements.
// Both `startIndex' and 'endIndex' MUST BE within bounds.
//
template <class T> ChArray<T>&
ChArray<T>::removeSubArray(int startIndex, int endIndex)
{
assert(isValid(startIndex));
assert(startIndex <= endIndex);
if ( endIndex >= mLogicalLen - 1) {
mLogicalLen = startIndex;
return *this;
}
// Shift array elements to the left if needed.
//
if (startIndex < mLogicalLen - 1) {
register T* p = mpArray + startIndex;
register T* q = mpArray + endIndex + 1;
register T* pStop = mpArray + mLogicalLen;
for (; q < pStop; p++, q++ ) {
*p = *q;
}
}
mLogicalLen -= endIndex - startIndex + 1;
return *this;
}
// Returns true if and only if the array contains `value' from
// index `start' onwards. Returns, in `index', the first location
// that contains `value'. The search begins at position `start'.
// `start' is supplied with a default value of `0', i.e., the
// beginning of the array.
//
template <class T> bool
ChArray<T>::find(const T& value, int& index, int start) const
{
for (register int i = start; i < mLogicalLen; i++) {
if (mpArray[i] == value) {
index = i;
return true;
}
}
return false;
}
// Allows you to set the logical length of the array.
// If you try to set the logical length to be greater than
// the physical length, then the array is grown to a
// reasonable size (thus increasing both the logical length
// AND the physical length).
// Also, the physical length will grow in growth length
// steps.
template <class T> ChArray<T>&
ChArray<T>::setLogicalLength(int n)
{
assert(n >= 0);
if (n > mPhysicalLen) {
int growth = (mPhysicalLen * sizeof(T)) < ARRAY_GROWTH_THRESHOLD ?
mPhysicalLen : ARRAY_GROWTH_THRESHOLD / sizeof(T);
int minSize = mPhysicalLen + __max(growth, mGrowLen);
if ( n > minSize)
minSize = n;
setPhysicalLength(minSize);
}
mLogicalLen = n;
return *this;
}
// Allows you to set the physical length of the array.
// If you set the physical length to be less than
// the logical length, then the logical length is reset
// to match the new physical length. A physical length
// of zero is valid.
//
template <class T> ChArray<T>&
ChArray<T>::setPhysicalLength(int n)
{
assert(n >= 0);
if (n == mPhysicalLen) return *this;
T* pOldArray = mpArray;
if (n == 0) { // Empty the array.
mpArray = NULL;
mPhysicalLen = 0;
// Get the required amount of space.
//
} else {
mpArray = new T[n];
if (mpArray == NULL) {
mPhysicalLen = 0;
} else {
for (register int i=0; i<mLogicalLen; i++) {
mpArray[i] = pOldArray[i];
}
mPhysicalLen = n;
}
}
if (pOldArray != NULL) { // Blow away the old array buffer.
delete[] pOldArray;
}
if (mPhysicalLen < mLogicalLen) {
mLogicalLen = mPhysicalLen;
}
return *this;
}
// Reverses the order of the array. That is if you have two
// arrays, `a' and `b', then if you assign `a = b' then call
// `a.reverse()' then a[0] == b[n], a[1] == b[n-1],... a[n] == b[0].
//
template <class T> ChArray<T>&
ChArray<T>::reverse()
{
T tmp;
for (register int i = 0; i < mLogicalLen/2; i++) {
tmp = mpArray[i];
mpArray[i] = mpArray[mLogicalLen - 1 - i];
mpArray[mLogicalLen - 1 - i] = tmp;
}
return *this;
}
// Swaps the elements in `i1' and `i2'.
//
template <class T> ChArray<T>&
ChArray<T>::swap(int i1, int i2)
{
assert(isValid(i1));
assert(isValid(i2));
if (i1 == i2) return *this;
T tmp = mpArray[i1];
mpArray[i1] = mpArray[i2];
mpArray[i2] = tmp;
return *this;
}
// Returns true if and only if `value' was removed from the array from
// position `start' onwards. Only the first occurrence of `value'
// is removed. Calling this function is equivalent to doing a "find(),
// then "removeAt()".
//
template <class T> bool
ChArray<T>::remove(const T& value, int start)
{
int i = 0;
if (find(value, i, start)) {
removeAt(i);
return true;
} else {
return false;
}
}
#endif // CH_ARRAY_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -