0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ArrayListをC言語で作ったよ

Last updated at Posted at 2022-02-26

C言語で可変長配列が必要になったから作ってみたよ
作りはすごく粗いし、最低限の機能しかねえからあんま使い物にはならないかもだけど
もっと高性能なListを作るときのヒントになるかも

ソースコード

ArrayList.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ArrayList.h"

struct ArrayList{
    struct index** array;
    unsigned int size;
    unsigned int arrayLen;
}__attribute__ ((__packed__));


struct index{
    char* data;
    int size;
}__attribute__ ((__packed__));


typedef struct ArrayList LIST;


int init_List(LIST** list){
    *list = (LIST*)malloc(sizeof(LIST));
    memset(*list, 0, sizeof(LIST));
    (*list)->array = (struct index**)malloc(1024 * 8);
    (*list)->arrayLen = 1024;
    (*list)->size = 0;
    return 0;
}

int addList(LIST* list, char* buf, int size){
    char* index = (char*)malloc(size);
    memcpy(index, buf, size);
    struct index* index_buf= (struct index*)malloc(sizeof(struct index));
    index_buf->data = index;
    index_buf->size = size;
    list->array[list->size] = index_buf;
    list->size++;
    if(list->size >= list->arrayLen){
        list->arrayLen += 1024;
        list->array = (struct index **)realloc(list->array, list->arrayLen * 8);
    }
    return 0;
}

int getList(LIST* list, int num, char* buf, unsigned int size){
    if(num >= list->size){
        return -1;
    }
    struct index* index = list->array[num];
    if(size < index->size){
        return -1;
    }
    memcpy(buf, index->data, index->size);
    return 0;
}

int removeList(LIST* list, unsigned int num){
    if(num >= list->size){
        return -1;
    }
    free(list->array[num]->data);
    free(list->array[num]);
    char buf[128 * 8];
    int a = list->size - num;
    while (a > 0)
    {
        if(a >= 128){
            memcpy(buf, &(list->array[num + 1]), 128 * 8);
            memcpy(&(list->array[num]), buf, 128 * 8);
            num += 128;
        }else{
            memcpy(buf, &(list->array[num + 1]), a *8);
            memcpy(&(list->array[num]), buf, a * 8);
            num += a;
        }
        a = list->size - num;
    }
    list->size -= 1;
    return 0
}

int free_list(LIST* list){
    for(unsigned int i = 0; i < list->size; i++){
        free(list->array[i]->data);
        free(list->array[i]);
    }

    free(list);
    return 0;
}


FILE*みたいに使えるようにしてみました
initで構造体のポインタを取得、初期化
addで最後尾に新しい要素の保存
getで指定したところの要素を取得
removeで指定したところの要素を削除
freeで使わなくなったListの削除(malloc使ってるから仕方ないね)
__attribute__ ((__packed__)) 使ってる理由? 気分

参考にしてね

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?