Last active
January 31, 2018 07:18
-
-
Save ChunMinChang/4f271097345f13fd2aafdbe5f78e123e to your computer and use it in GitHub Desktop.
Resizable array implementation #data_structure #array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> // for assert. | |
#include <cstdlib> // for calloc, free. | |
#include <cstring> // for memcpy. | |
template<class T> | |
class AutoArray | |
{ | |
public: | |
AutoArray(unsigned int aCapacity = 0) | |
: mCapacity(aCapacity) | |
, mSize(0) | |
, mData(nullptr) | |
{ | |
if (mCapacity) { | |
Resize(mCapacity); | |
} | |
} | |
~AutoArray() | |
{ | |
} | |
unsigned int Size() { return mSize; } | |
void Push(T aData) | |
{ | |
if (mSize == mCapacity) { | |
// Resize(mSize ? 2 * mSize : 1); | |
Resize(Max(2 * mSize, 1)); | |
} | |
// mData[mSize] = aData; | |
// ++mSize; | |
mData[mSize++] = aData; | |
} | |
T Pop() | |
{ | |
assert(mSize); | |
// --mSize; | |
// T last = mData[mSize]; | |
T last = mData[--mSize]; | |
return last; | |
} | |
T& operator[] (unsigned int aIndex) | |
{ | |
assert(aIndex < mSize); | |
return mData[aIndex]; | |
} | |
private: | |
void Resize(unsigned int aCapacity) | |
{ | |
assert(!mData || aCapacity > mCapacity); | |
T* space = static_cast<T*>(calloc(1, sizeof(T) * aCapacity)); | |
if (mData) { | |
memcpy(space, mData, sizeof(T) * mCapacity); | |
free(mData); | |
} | |
mData = space; | |
mCapacity = aCapacity; | |
} | |
void Clear() | |
{ | |
free(mData); | |
mData = nullptr; | |
mSize = 0; | |
mCapacity = 0; | |
} | |
unsigned int Max(unsigned int a, unsigned int b) | |
{ | |
return a > b ? a : b; | |
} | |
unsigned int mSize; | |
unsigned int mCapacity; | |
T* mData; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "AutoArray.h" | |
int main() | |
{ | |
AutoArray<char> A; | |
for (unsigned int i = 0 ; i < 10 ; ++i) { | |
A.Push(i + 'a'); | |
} | |
for (unsigned int i = 0 ; i < A.Size() ; ++i) { | |
std::cout << A[i] << std::endl; | |
} | |
A[3] = 'x'; | |
A[4] = 'y'; | |
A[5] = 'z'; | |
while (A.Size()) { | |
std::cout << A.Pop() << std::endl; | |
} | |
AutoArray<int> B(5); | |
for (unsigned int i = 0 ; i < 10 ; ++i) { | |
B.Push(i); | |
} | |
for (unsigned int i = 0 ; i < B.Size() ; ++i) { | |
std::cout << B[i] << std::endl; | |
} | |
// std::cout << B[20] << std::endl; // It should fail the assertion! | |
while (B.Size() > 3) { | |
std::cout << B.Pop() << std::endl; | |
} | |
// B[3] = 27; // It should fail the assertion! | |
B[2] = 26; | |
B[1] = 25; | |
for (unsigned int i = 0 ; i < B.Size() ; ++i) { | |
std::cout << B[i] << std::endl; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> // for assert. | |
#include <cstdlib> // for calloc, free. | |
#include <cstring> // for memcpy. | |
template<class T> | |
class FlexibleArray | |
{ | |
public: | |
FlexibleArray(unsigned int aCapacity = 0) | |
: mCapacity(aCapacity) | |
, mSize(0) | |
, mData(nullptr) | |
{ | |
if (mCapacity) { | |
Resize(mCapacity); | |
} | |
} | |
~FlexibleArray() | |
{ | |
Clear(); | |
} | |
unsigned int Size() { return mSize; } | |
T& operator[] (unsigned int aIndex) | |
{ | |
if (aIndex >= mCapacity) { | |
// Resize(mCapacity && 2 * mCapacity > aIndex ? 2 * mCapacity : aIndex + 1); | |
Resize(Max(2 * mCapacity, aIndex + 1)); | |
} | |
if (aIndex >= mSize) { | |
mSize = aIndex + 1; | |
} | |
return mData[aIndex]; | |
} | |
private: | |
void Resize(unsigned int aCapacity) { | |
assert(!mData || aCapacity > mCapacity); | |
T* space = static_cast<T*>(calloc(1, sizeof(T) * aCapacity)); | |
assert(space); | |
if (mData) { | |
memcpy(space, mData, sizeof(T) * mCapacity); | |
free(mData); | |
} | |
mData = space; | |
mCapacity = aCapacity; | |
} | |
void Clear() | |
{ | |
free(mData); | |
mData = nullptr; | |
mSize = 0; | |
mCapacity = 0; | |
} | |
unsigned int Max(unsigned int a, unsigned int b) | |
{ | |
return a > b ? a : b; | |
} | |
unsigned int mCapacity; | |
unsigned int mSize; | |
T* mData; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "FlexibleArray.h" | |
int main() | |
{ | |
FlexibleArray<char> A; | |
for (unsigned int i = 0 ; i < 10 ; ++i) { | |
A[i] = i + 'a'; | |
} | |
for (unsigned int i = 0 ; i < A.Size() ; ++i) { | |
std::cout << A[i] << std::endl; | |
} | |
FlexibleArray<int> B(10); | |
for (unsigned int i = 0 ; i < 20 ; ++i) { | |
B[i] = i + i; | |
} | |
for (unsigned int i = 0 ; i < B.Size() ; ++i) { | |
std::cout << B[i] << std::endl; | |
} | |
FlexibleArray<int> C; | |
for (unsigned int i = 5; i < 10 ; ++i) { | |
C[i] = i * i; | |
} | |
for (unsigned int i = 0 ; i < C.Size() ; ++i) { | |
std::cout << C[i] << std::endl; | |
} | |
FlexibleArray<int> D(5); | |
D[10] = 67; | |
D[20] = 97; | |
for (unsigned int i = 0 ; i < D.Size() ; ++i) { | |
std::cout << D[i] << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment