DynamicArray최근에 플랫폼 특성 때문에 순수 C만 해야 하는데 STL따위가 없어서 너무 짜증난다는 얘기를 듣게 되어서 만들어보게 되었다. 오랜만에 하니까 재미도 있긴 하지만 짜증이 너무 많이 나서 C는 진짜 못쓰겠다는 생각이 들었다.
StructName_FunctionName(StructName) 따위를 치고, 또 보다 보면 짜증나서 죽을거 같으니 C++처럼 보이게 만들자./************************************************************************************************* main *************************************************************************************************/ typedef struct { int a; int b; int c; } Data; int main() { DynamicArray *pDa = New_DynamicArray(sizeof(Data)); { size_t i = 0; for(; i < 50; i++) { Data data = { i, i*2, i*3 }; pDa->Add(pDa, &data); } } { size_t i = 0; for(; i < pDa->_iCount; i++) { if(i % 5 == 0) printf("\n"); Data *pData = (Data*)pDa->Get(pDa, i); printf("(%d,%d,%d)\t", pData->a, pData->b, pData->c); } } Delete_DynamicArray(pDa); return EXIT_SUCCESS; }
(0,0,0) (1,2,3) (2,4,6) (3,6,9) (4,8,12) (5,10,15) (6,12,18) (7,14,21) (8,16,24) (9,18,27) (10,20,30) (11,22,33) (12,24,36) (13,26,39) (14,28,42) (15,30,45) (16,32,48) (17,34,51) (18,36,54) (19,38,57) (20,40,60) (21,42,63) (22,44,66) (23,46,69) (24,48,72) (25,50,75) (26,52,78) (27,54,81) (28,56,84) (29,58,87) (30,60,90) (31,62,93) (32,64,96) (33,66,99) (34,68,102) (35,70,105) (36,72,108) (37,74,111) (38,76,114) (39,78,117) (40,80,120) (41,82,123) (42,84,126) (43,86,129) (44,88,132) (45,90,135) (46,92,138) (47,94,141) (48,96,144) (49,98,147)
사실 별거 없고 스트럭처에 펑션포인터만 넣어주고 눈속임 한 것이 전부다.
물론 이렇게 하면 손과 눈이 조금 편해질 것 같기도 하지만, 호출시마다 this를 통해서, 또 펑션포인터를 통해 함수에 액세스해야 하므로 시간상의 손해, DynamicArray마다 동일한 멤버펑션포인터를 가지고 있으므로 공간상의 손해가 있어 피해가 막심하다.
선언 정의 분리 및 Add, Insert, Remove 따위도 등도 하려 했으나 흥미를 잃어서 중지. 언제까지나 놀기 위해 만든것. 더 이상의 구현은 생략한다. 오류처리도 하나도 안되어있고 개판이다 ㅋㅋ 메모리도 새려나?
전체 소스. gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5에서 컴파일된다.
#include <stdlib.h> #include <stdio.h> #include <string.h> /************************************************************************************************* miscellaneous *************************************************************************************************/ size_t Min(size_t i, size_t j) { return i < j ? i : j; } size_t Max(size_t i, size_t j) { return j < i ? i : j; } /************************************************************************************************* DynamicArray *************************************************************************************************/ typedef struct DynamicArray { void (*SetCapacity)(struct DynamicArray *this, const size_t iCapacity); void (*Add)(struct DynamicArray *this, const void *pData); void *(*Get)(const struct DynamicArray *this, const size_t iIndex); size_t _iDataSize; void** _array; size_t _iCapacity; size_t _iCount; } DynamicArray; void DynamicArray_SetCapacity(DynamicArray *this, const size_t iCapacity) { void** newArray = (void**)malloc(sizeof(void*) * iCapacity); if(NULL != this->_array) { memcpy(newArray, this->_array, sizeof(void*) * Min(iCapacity, this->_iCapacity)); free(this->_array); } this->_array = newArray; this->_iCapacity = iCapacity; } void DynamicArray_Add(DynamicArray *this, const void *pData) { if(this->_iCapacity <= this->_iCount) this->SetCapacity(this, Min((this->_iCapacity+1) * 2, this->_iCapacity + 16)); void* pNew = malloc(this->_iDataSize); memcpy(pNew, pData, this->_iDataSize); this->_array[this->_iCount++] = pNew; } void *DynamicArray_Get(const struct DynamicArray *this, const size_t iIndex) { return this->_array[iIndex]; } DynamicArray *New_DynamicArray(size_t iDataSize) { DynamicArray *this = (DynamicArray *)malloc(sizeof(DynamicArray)); this->Add = DynamicArray_Add; this->SetCapacity = DynamicArray_SetCapacity; this->Get = DynamicArray_Get; this->_iDataSize = iDataSize; this->_array = NULL; this->_iCapacity = 0; this->_iCount = 0; return this; } void Delete_DynamicArray(DynamicArray *this) { size_t i; for(i = 0; i < this->_iCount; i++) { free(this->_array[i]); } free(this); } /************************************************************************************************* main *************************************************************************************************/ typedef struct { int a; int b; int c; } Data; int main() { DynamicArray *pDa = New_DynamicArray(sizeof(Data)); { size_t i = 0; for(; i < 50; i++) { Data data = { i, i*2, i*3 }; pDa->Add(pDa, &data); } } { size_t i = 0; for(; i < pDa->_iCount; i++) { if(i % 5 == 0) printf("\n"); Data *pData = (Data*)pDa->Get(pDa, i); printf("(%d,%d,%d)\t", pData->a, pData->b, pData->c); } } Delete_DynamicArray(pDa); return EXIT_SUCCESS; }
-- 2011-01-10
see also 한국해양대학교CProgramming2CDynamicArray
4.2. Similar PagesSimilar pages by cosine similarity. Words after page name are term frequency.
|