Skip to content

A lightweight, C-based implementation of a generic dynamic array (Vector) featuring automatic memory management (in single-threading mode), multi-threading support via manual deletion, and the ability to create multi-dimensional vectors. It provides an interface similar to C++ STL containers.

License

Notifications You must be signed in to change notification settings

TinyProbe/c-vector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

34 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ C-Vector

C μ–Έμ–΄μ—μ„œ 동적 λ°°μ—΄(Vector) κΈ°λŠ₯을 μ œκ³΅ν•˜λŠ” κ²½λŸ‰ λΌμ΄λΈŒλŸ¬λ¦¬μž…λ‹ˆλ‹€. λ©€ν‹° μŠ€λ ˆλ”© 지원, μžλ™ λ©”λͺ¨λ¦¬ 관리, 닀차원 벑터 생성 λ“± λ‹€μ–‘ν•œ κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€.

✨ Features

  • 닀차원 벑터 지원: vt_new(type, dimension, ...) 맀크둜λ₯Ό μ‚¬μš©ν•˜μ—¬ 닀차원 동적 배열을 μ‰½κ²Œ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.

  • μžλ™ λ©”λͺ¨λ¦¬ 관리: μ‹±κΈ€ μŠ€λ ˆλ“œ ν™˜κ²½μ—μ„œ vt_in() / vt_out() λ˜λŠ” ν”„λ‘œκ·Έλž¨ μ’…λ£Œ μ‹œ μžλ™μœΌλ‘œ λͺ¨λ“  벑터 λ©”λͺ¨λ¦¬λ₯Ό ν•΄μ œν•©λ‹ˆλ‹€.

  • STL μœ μ‚¬ ν•¨μˆ˜: C++의 std::vector와 μœ μ‚¬ν•œ push, pop, insert, at, iterator λ“±μ˜ 맀크둜 ν•¨μˆ˜λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.

  • λ©€ν‹° μŠ€λ ˆλ”© 지원: 맀크둜 μ •μ˜λ₯Ό 톡해 λ©€ν‹° μŠ€λ ˆλ“œ ν™˜κ²½μ—μ„œλ„ μ•ˆμ „ν•˜κ²Œ μˆ˜λ™ λ©”λͺ¨λ¦¬ 관리가 κ°€λŠ₯ν•©λ‹ˆλ‹€.

πŸ› οΈ Usage

1. Single-Threading Mode

λ³„λ„μ˜ 맀크둜 μ •μ˜ 없이 기본적으둜 μ‹±κΈ€ μŠ€λ ˆλ“œ λͺ¨λ“œλ‘œ λ™μž‘ν•˜λ©°, μŠ€μ½”ν”„ 기반의 μžλ™ λ©”λͺ¨λ¦¬ ν•΄μ œ κΈ°λŠ₯을 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

#include "vector.h"

int main(void) {
    // ν•΄λ‹Ή μŠ€μ½”ν”„μ—μ„œ μƒμ„±λœ λͺ¨λ“  λ²‘ν„°μ˜ μžλ™ ν•΄μ œλ₯Ό μ‹œμž‘ν•©λ‹ˆλ‹€.
    vt_in();

    // μ •μˆ˜ν˜• 벑터 생성 및 κ°’ μΆ”κ°€
    vector v_int = vt_new(int, 1, 0);
    vt_push(v_int, 10);
    vt_add(v_int, (int)20);

    // 3차원 longν˜• 벑터 생성 (10 * 20 * 30)
    vector v_long = vt_new(long, 3, 10, 20, 30);

    // 첫 번째 μ›μ†Œ μ ‘κ·Ό
    printf("First element: %d\n", vt_front(v_int, int));

    // 이 μ§€μ μ—μ„œ vt_in() 이후 μƒμ„±λœ λͺ¨λ“  벑터 λ©”λͺ¨λ¦¬κ°€ μžλ™μœΌλ‘œ ν•΄μ œλ©λ‹ˆλ‹€.
    vt_out();
    return 0;
}

2. Multi-Threading Mode

λ©€ν‹° μŠ€λ ˆλ”© ν™˜κ²½μ—μ„œ μ‚¬μš©ν•˜λ €λ©΄ 헀더 파일 포함 전에 VECTOR_MULTITHREAD 맀크둜λ₯Ό μ •μ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€. 이 λͺ¨λ“œμ—μ„œλŠ” μžλ™ ν•΄μ œ(vt_in/vt_out)κ°€ λΉ„ν™œμ„±ν™”λ˜λ©°, vt_deleteλ₯Ό μ‚¬μš©ν•˜μ—¬ μˆ˜λ™μœΌλ‘œ λ©”λͺ¨λ¦¬λ₯Ό ν•΄μ œν•΄μ•Ό ν•©λ‹ˆλ‹€.

#define _VECTOR_MULTITHREAD_ // λ°˜λ“œμ‹œ μ •μ˜
#include "vector.h"

int main(void) {
    // vt_new ν•¨μˆ˜λŠ” μ—¬μ „νžˆ μ‚¬μš© κ°€λŠ₯ν•©λ‹ˆλ‹€.
    vector v = vt_new(double, 2, 5, 5);

    // λ©€ν‹°μŠ€λ ˆλ“œ λͺ¨λ“œμ—μ„œλŠ” λ°˜λ“œμ‹œ μˆ˜λ™μœΌλ‘œ λ©”λͺ¨λ¦¬ ν•΄μ œ
    vt_delete(v);

    // 닀차원 λ²‘ν„°μ˜ 경우 λ‚΄λΆ€μ˜ λͺ¨λ“  벑터도 μž¬κ·€μ μœΌλ‘œ ν•΄μ œλ©λ‹ˆλ‹€.
    // vector multi = vt_new(vector, 1, 10);
    // vt_delete(multi);

    return 0;
}

πŸ“š Macro Functions List

void    vt_in()
    // The 'vt_in' function is called at the beginning of the scope of the
    // function where the vector will be created. Then, when 'vt_out' is
    // called, it automatically clears all vectors created during that time.
    // This does not clear any memory that the user dynamically allocated.

void    vt_out()
    // 'vt_out' clears all vectors created after 'vt_in' was called and
    // before 'vt_out' is called. It does not free memory that the user
    // may have allocated arbitrarily. (Even if you do not call 'vt_in'
    // and 'vt_out', all vectors will be automatically cleared before
    // the program terminates. This automatic clearing feature is safe
    // to use only in single-threaded mode. In multithreading, you should
    // declare the _VECTOR_MULTITHREAD_ macro and use manual clearing.)

void    vt_delete(vector)
    // The 'vt_delete' function is a vector manual deletion function that
    // is only available in multithreading. It recursively deletes all
    // vectors, even if a multidimensional vector is input.

    // vector: Vector to be deleted.
    // 
    // int main(void) {
    //     vector v = vt_new(int, 2, 10, 20); // same as [10][20].
    //     vt_delete(v); // All vectors within the vector are also deleted.
    // }

vector  vt_new(type, dimension, ...)
    // The vt_new() function creates and returns a vector object.

    // type: The type of elements to be contained in the vector.
    // dimension: Indicates the depth of the dimension of the vector.
    // ...: Represents the number of elements in the vector in each
    //      dimension.
    //
    // int main(void) {
    //     vt_in();
    //     vector v = vt_new(long, 3, 10, 20, 30); // same as [10][20][30].
    //     vt_out();
    // }

size_t  vt_count(vector)
    // The 'vt_count' function returns the number of elements in a vector.

    // vector: The vector for which you want to get the number of elements.

size_t  vt_size(vector)
    // Same as 'vt_count'.

size_t  vt_len(vector)
    // Same as 'vt_count'.

size_t  vt_empty(vector)
    // The 'vt_empty' function returns true if the vector is empty,
    // otherwise it returns false.

    // vector: A vector to check if it is empty or not.

size_t  vt_type_size(vector)
    // The 'vt_type_size' function returns the size of the element type of
    // a vector in bytes.

    // vector: A vector to get the type size.

size_t  vt_type_count(vector)
    // The 'vt_type_count' function returns the number of allocated
    // elements.

    // vector: A vector to get the number of types.

size_t  vt_capacity(vector)
    // The 'vt_capacity' function returns the size of the space allocated
    // to a vector in bytes.

    // vector: A vector to get the allocation size.

void    vt_resize(vector, count)
    // The 'vt_resize' function changes the number of elements in a vector.

    // vector: A vector whose number of elements will be changed.
    // count: Number of new elements.

void    vt_clear(vector)
    // The 'vt_clear' function removes and deallocates all elements.

    // vector: A vector from which all elements will be removed.

void    vt_add(vector, item_variable)
    // The 'vt_add' function adds an element to the end of a vector.
    // (Only variables are accepted.)

    // vector: A vector to add elements to.
    // item_variable: Elements to add.

void    vt_push(vector, item_literal)
    // The 'vt_push' function adds an element to the end of a vector.
    // (Only literal values are allowed.)

    // vector: A vector to add elements to.
    // item_literal: Elements to add.

void    vt_pop(vector)
    // The 'vt_pop' function removes the last element of a vector.

    // vector: A vector from which to remove the last element.

void    vt_insert(vector, index, item_variable)
    // The 'vt_insert' function adds an element to a vector at a specific
    // position. (Only variables are accepted.)

    // vector: A vector to add elements to.
    // index: Index at which to insert the element.
    // item_variable: Elements to add.

void    vt_input(vector, index, item_literal)
    // The 'vt_input' function adds an element to an arbitrary position
    // in a vector. (Only literal values are allowed.)

    // vector: A vector to add elements to.
    // index: Index at which to insert the element.
    // item_literal: Elements to add.

void    vt_erase(vector, index)
    // The 'vt_erase' function removes the last element of a vector.

    // vector: A vector from which to remove the last element.
    // index: The index of the element to remove.

type    vt_at(vector, index, type)
    // The 'vt_at' function dereferences a specific element of a vector.
    // (For performance reasons, vector index range checking is not
    // performed.)

    // vector: A vector to dereference elements.
    // index: Index of the element to dereference.
    // type: The type of element to dereference.

type    vt_front(vector, type)
    // The 'vt_front' function dereferences the first element of a vector.

    // vector: A vector that dereferences the first element.
    // type: The type of element to dereference.

type    vt_back(vector, type)
    // The 'vt_back' function dereferences the last element of the vector.

    // vector: A vector that dereferences the last element.
    // type: The type of element to dereference.

void    vt_swap(left, right)
    // The 'vt_swap' function swaps two vectors.

    // left: The left-hand vector to be exchanged.
    // right: The right-hand vector to be exchanged.

vector  vt_move(source)
    // The 'vt_move' function returns ownership of the vector.

    // source: The vector to return ownership to.

vector  vt_clone(source)
    // The 'vt_clone' function creates and returns a clone object of a
    // vector. (Deep replication is performed between vectors, but memory
    // arbitrarily allocated by the user is not replicated.)

    // source: The vector to be cloned.

void    vt_reverse(vector)
    // The 'vt_reverse' function reverses the order of elements in a vector.

    // vector: A vector whose elements are to be reversed in order.

vt_itr  vt_itr(vector, index)
    // The 'vt_itr' function returns an iterator pointing to a specific
    // index.

    // vector: A vector from which to create iterators.
    // index: The index to which the iterator will point.

vt_itr  vt_begin(vector)
    // The 'vt_begin' function returns an iterator pointing to the first
    // element.

    // vector: A vector from which to create iterators.

vt_itr  vt_end(vector)
    // The 'vt_end' function returns an iterator pointing to the position
    // after the last element.

    // vector: A vector from which to create iterators.

void    vt_next(iterator)
    // The 'vt_next' function moves the position pointed to by the
    // iterator to the next element.

    // iterator: An iterator that moves the specified element to the
    //           next one.

void    vt_prev(iterator)
    // The 'vt_prev' function moves the position pointed to by the
    // iterator to the previous element.

    // iterator: An iterator that moves the specified element to the
    //           previous element.

type    vt_ref(iterator, type)
    // The 'vt_ref' function returns a dereference to the element
    // specified by the iterator.

    // iterator: An iterator to get the dereference.
    // type: The type of element to dereference.

βš™οΈ Configuration

  • 헀더 νŒŒμΌμ—μ„œ λ‹€μŒ μƒμˆ˜λ₯Ό μˆ˜μ •ν•˜μ—¬ λ²‘ν„°μ˜ μ΅œλŒ€ 크기 등을 μ‘°μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
#define MAX_VECTOR_COUNT        (1ull << 20) // μ΅œλŒ€ 생성 κ°€λŠ₯ν•œ 벑터 μΈμŠ€ν„΄μŠ€ 수
#define MAX_STACK_HEIGHT        (1ull << 20) // λ‚΄λΆ€ μŠ€νƒμ˜ μ΅œλŒ€ 높이 (vt_in/vt_out κ΄€λ ¨)
#define MAX_VECTOR_DIMENSION    (1ull << 5)  // 벑터가 κ°€μ§ˆ 수 μžˆλŠ” μ΅œλŒ€ 차원

πŸ”— References

  • vector.h 파일 내에 λͺ¨λ“  ν•¨μˆ˜/λ§€ν¬λ‘œμ— λŒ€ν•œ 상세 λ¬Έμ„œκ°€ ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

πŸ“„ License

이 ν”„λ‘œμ νŠΈλŠ” MIT λΌμ΄μ„ μŠ€λ₯Ό λ”°λ¦…λ‹ˆλ‹€.

About

A lightweight, C-based implementation of a generic dynamic array (Vector) featuring automatic memory management (in single-threading mode), multi-threading support via manual deletion, and the ability to create multi-dimensional vectors. It provides an interface similar to C++ STL containers.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published