Changeset 151 in libcf for trunk/src/cf_list.c


Ignore:
Timestamp:
10/31/13 10:17:24 (11 years ago)
Author:
cheese
Message:

#1 fix interface and add util module

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cf_list.c

    r149 r151  
    2424        return CF_ERROR_DS_INVALID_ARGS
    2525
    26 /** 리스트 노드 (CF_Traverser의 구현) */
     26/** 리스트 노드 (cf_traverser의 구현) */
    2727typedef struct __cf_node__
    2828{
     
    3232} CF_NODE;
    3333
    34 /** 리스트 컨텍스트 (CF_List_Ctx의 구현) */
     34/** 리스트 컨텍스트 (cf_ctx의 구현) */
    3535typedef struct __cf_list__
    3636{
     
    4848 */
    4949int
    50 CF_List_CreateCtx (CF_List_Ctx * ctx)
     50CF_List_Create (cf_ctx * ctx)
    5151{
    5252    CF_LIST_CONTEXT * context = NULL;
    5353
    54     ASSERT_ARGS (ctx == NULL);
    55 
    56     context = (CF_LIST_CONTEXT *) calloc (sizeof (CF_LIST_CONTEXT), 1);
     54    ASSERT_CTX (ctx);
     55
     56    context = NEWCTX (CF_LIST_CONTEXT);
    5757    if (context == NULL)
    5858        return CF_ERROR_DS_CREATE_CTX;
    5959
    60     *ctx = (CF_List_Ctx) context;
     60    *ctx = (cf_ctx) context;
    6161
    6262    return CF_OK;
     
    7171 */
    7272int
    73 CF_List_DestroyCtx (CF_List_Ctx ctx)
     73CF_List_Destroy (cf_ctx ctx)
    7474{
    7575    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     
    8585
    8686/**
    87  * 리스트 탐색자(Traverser)를 Front 위치로 설정
     87 * 리스트 탐색자(traverser)를 Front 위치로 설정
    8888 *
    8989 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     
    9393 */
    9494int
    95 CF_List_Front (CF_List_Ctx  ctx,
    96                CF_Traverser * traverser)
     95CF_List_Front (cf_ctx       ctx,
     96               cf_traverser * traverser)
    9797{
    9898    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     
    101101    ASSERT_TRAVERSER (traverser);
    102102
    103     *traverser = (CF_Traverser *) context->front;
    104 
    105     return CF_OK;
    106 }
    107 
    108 /**
    109  * 리스트 탐색자(Traverser)를 Rear 위치로 설정
     103    *traverser = (cf_traverser *) context->front;
     104
     105    return CF_OK;
     106}
     107
     108/**
     109 * 리스트 탐색자(traverser)를 Rear 위치로 설정
    110110 *
    111111 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     
    115115 */
    116116int
    117 CF_List_Rear (CF_List_Ctx   ctx,
    118               CF_Traverser  * traverser)
     117CF_List_Rear (cf_ctx        ctx,
     118              cf_traverser  * traverser)
    119119{
    120120    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     
    123123    ASSERT_TRAVERSER (traverser);
    124124
    125     *traverser = (CF_Traverser *) context->rear;
    126 
    127     return CF_OK;
    128 }
    129 
    130 /**
    131  * 리스트에 새 데이터 삽입
     125    *traverser = (cf_traverser *) context->rear;
     126
     127    return CF_OK;
     128}
     129
     130/**
     131 * 리스트 처음에 새 데이터 삽입
     132 *
     133 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     134 *
     135 * \param ctx       리스트 컨텍스트
     136 * \param element   삽입할 데이터 주소
     137 */
     138int
     139CF_List_AddFront (cf_ctx        ctx,
     140                  const void    * element)
     141{
     142    int             result = 0;
     143    cf_traverser    trav = NULL;
     144
     145    ASSERT_CTX (ctx);
     146
     147    result = CF_List_Front (ctx, &trav);
     148    if (result < 0)
     149        return result;
     150
     151    return CF_List_InsertBefore (ctx, trav, element);
     152}
     153
     154/**
     155 * 리스트 끝에 새 데이터 삽입
     156 *
     157 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     158 *
     159 * \param ctx       리스트 컨텍스트
     160 * \param element   삽입할 데이터 주소
     161 */
     162int
     163CF_List_AddRear (cf_ctx     ctx,
     164                 const void * element)
     165{
     166    int             result = 0;
     167    cf_traverser    trav = NULL;
     168
     169    ASSERT_CTX (ctx);
     170
     171    result = CF_List_Rear (ctx, &trav);
     172    if (result < 0)
     173        return result;
     174
     175    return CF_List_InsertAfter (ctx, trav, element);
     176}
     177
     178/**
     179 * 리스트 탐색자(traverser)의 앞에 새 데이터 삽입
    132180 *
    133181 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     
    135183 * \param ctx       리스트 컨텍스트
    136184 * \param traverser 리스트 탐색자
    137  * \param direction 탐색자의 전/후를 지정
    138185 * \param element   삽입할 데이터 주소
    139  *
    140  * \remarks
    141  * traverser가 NULL일 때,
    142  * direction이 CF_DIRECTION_BEFORE라면 Front 위치이고
    143  * direction이 CF_DIRECTION_AFTER라면 Rear 위치에 삽입
    144  */
    145 int
    146 CF_List_Insert (CF_List_Ctx         ctx,
    147                 const CF_Traverser  traverser,
    148                 const CF_DIRECTION  direction,
    149                 const void          * element)
     186 */
     187int
     188CF_List_InsertBefore (cf_ctx                ctx,
     189                      const cf_traverser    traverser,
     190                      const void            * element)
    150191{
    151192    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
    152     CF_NODE     * node = NULL;
    153     CF_NODE     * newnode = NULL;
    154 
    155     ASSERT_CTX (ctx);
    156 
    157     newnode = (CF_NODE *) calloc (sizeof (CF_NODE), 1);
     193    CF_NODE         * node = NULL;
     194    CF_NODE         * newnode = NULL;
     195
     196    ASSERT_CTX (ctx);
     197
     198    newnode = NEWCTX (CF_NODE);
    158199    if (newnode == NULL)
    159200        return CF_ERROR_DS_CREATE_NODE;
     
    169210        }
    170211
    171         if (direction == CF_DIRECTION_BEFORE)
     212        node = traverser ? (CF_NODE *) traverser : context->front;
     213
     214        newnode->next = node;
     215        if (node)
    172216        {
    173             node = traverser ? (CF_NODE *) traverser : context->front;
    174 
    175             newnode->next = node;
    176             if (node)
    177             {
    178                 newnode->prev = node->prev;
    179                 node->prev  = newnode;
    180                 if (newnode->prev)
    181                     newnode->prev->next = newnode;
    182             }
    183 
    184             if (node == context->front)
    185                 context->front = newnode;
     217            newnode->prev = node->prev;
     218            node->prev  = newnode;
     219            if (newnode->prev)
     220                newnode->prev->next = newnode;
    186221        }
    187         else /* if (direction == CF_DIRECTION_AFTER) */
     222
     223        if (node == context->front)
     224            context->front = newnode;
     225    } NO_CATCH;
     226
     227    context->size++;
     228
     229    return CF_OK;
     230}
     231
     232/**
     233 * 리스트 탐색자(traverser)의 뒤에 새 데이터 삽입
     234 *
     235 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     236 *
     237 * \param ctx       리스트 컨텍스트
     238 * \param traverser 리스트 탐색자
     239 * \param element   삽입할 데이터 주소
     240 */
     241int
     242CF_List_InsertAfter (cf_ctx             ctx,
     243                     const cf_traverser traverser,
     244                     const void         * element)
     245{
     246    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     247    CF_NODE         * node = NULL;
     248    CF_NODE         * newnode = NULL;
     249
     250    ASSERT_CTX (ctx);
     251
     252    newnode = NEWCTX (CF_NODE);
     253    if (newnode == NULL)
     254        return CF_ERROR_DS_CREATE_NODE;
     255
     256    newnode->element = (void *) element;
     257
     258    TRY
     259    {
     260        if (CF_List_GetSize (ctx) == 0)
    188261        {
    189             node = traverser ? (CF_NODE *) traverser : context->rear;
    190 
    191             newnode->prev = node;
    192             if (node)
    193             {
    194                 newnode->next = node->next;
    195                 node->next = newnode;
    196                 if (newnode->next)
    197                     newnode->next->prev = newnode;
    198             }
    199 
    200             if (node == context->rear)
    201                 context->rear = newnode;
     262            context->front = context->rear = newnode;
     263            TRY_BREAK;
    202264        }
     265
     266        node = traverser ? (CF_NODE *) traverser : context->rear;
     267
     268        newnode->prev = node;
     269        if (node)
     270        {
     271            newnode->next = node->next;
     272            node->next = newnode;
     273            if (newnode->next)
     274                newnode->next->prev = newnode;
     275        }
     276
     277        if (node == context->rear)
     278            context->rear = newnode;
    203279    } NO_CATCH;
    204280
     
    209285
    210286/**
     287 * 리스트 탐색자(traverser)의 자리에 새 데이터 삽입
     288 *
     289 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     290 *
     291 * \param traverser 리스트 탐색자
     292 * \param element   할당할 데이터 주소
     293 */
     294int
     295CF_List_Set (const cf_traverser traverser,
     296             const void         * element)
     297{
     298    CF_NODE * node = (CF_NODE *) traverser;
     299
     300    ASSERT_TRAVERSER (traverser);
     301
     302    node->element = (void *) element;
     303
     304    return CF_OK;
     305}
     306
     307/**
     308 * 탐색자 위치의 데이터를 가져옴
     309 *
     310 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     311 *
     312 * \param traverser 리스트 탐색자
     313 * \param element   데이터 주소
     314 */
     315int
     316CF_List_Get (const cf_traverser traverser,
     317             void               ** element)
     318{
     319    CF_NODE * node = (CF_NODE *) traverser;
     320
     321    ASSERT_TRAVERSER (traverser);
     322    ASSERT_ARGS (element == NULL);
     323
     324    *element = node->element;
     325
     326    return CF_OK;
     327}
     328
     329/**
    211330 * 리스트에서 탐색자 위치의 항목을 삭제
    212331 *
     
    217336 */
    218337int
    219 CF_List_Remove (CF_List_Ctx     ctx,
    220                 CF_Traverser    * traverser)
     338CF_List_Remove (cf_ctx          ctx,
     339                cf_traverser    * traverser)
    221340{
    222341    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     
    263382 */
    264383int
    265 CF_List_RemoveAll (CF_List_Ctx ctx)
    266 {
    267     CF_Traverser traverser = NULL;
     384CF_List_RemoveAll (cf_ctx ctx)
     385{
     386    cf_traverser traverser = NULL;
    268387
    269388    ASSERT_CTX (ctx);
     
    279398
    280399/**
    281  * 탐색자 위치의 데이터를 가져옴
    282  *
    283  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    284  *
    285  * \param traverser 리스트 탐색자
    286  * \param element   데이터 주소
    287  */
    288 int
    289 CF_List_GetElement (const CF_Traverser  traverser,
    290                     void                ** element)
    291 {
    292     CF_NODE * node = (CF_NODE *) traverser;
    293 
    294     ASSERT_TRAVERSER (traverser);
    295     ASSERT_ARGS (element == NULL);
    296 
    297     *element = node->element;
    298 
    299     return CF_OK;
    300 }
    301 
    302 /**
    303400 * 탐색자 위치를 이전으로 이동
    304401 *
     
    308405 */
    309406int
    310 CF_List_Prev (CF_Traverser * traverser)
     407CF_List_Prev (cf_traverser * traverser)
    311408{
    312409    CF_NODE * node = (CF_NODE *) *traverser;
     
    316413
    317414    node = node->prev;
    318     *traverser = (CF_Traverser *) node;
     415    *traverser = (cf_traverser *) node;
    319416    if (node == NULL)
    320417        return CF_ERROR_DS_NO_MORE;
     
    331428 */
    332429int
    333 CF_List_Next (CF_Traverser * traverser)
     430CF_List_Next (cf_traverser * traverser)
    334431{
    335432    CF_NODE * node = (CF_NODE *) *traverser;
     
    339436
    340437    node = node->next;
    341     *traverser = (CF_Traverser *) node;
     438    *traverser = (cf_traverser *) node;
    342439    if (node == NULL)
    343440        return CF_ERROR_DS_NO_MORE;
     
    354451 */
    355452int
    356 CF_List_GetSize (CF_List_Ctx ctx)
     453CF_List_GetSize (cf_ctx ctx)
    357454{
    358455    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
Note: See TracChangeset for help on using the changeset viewer.