Coder Social home page Coder Social logo

vec's Introduction

vec

A simple vector implementation in C.
Interface (thoroughly documented) can be found in include/vec/vec.h
Core methods resemble the ones of https://en.cppreference.com/w/cpp/container/vector

I made this to see CMake and C in action.


INSTALL

To use the library I recommend you not to use find_package.
Instead use git submodules to checkout the repository and then add it to the project as a directory

For example,
suppose you have the following directory structure:
├── CMakeLists.txt
├── readme.txt
└── src
    └── main.c

- Checkout the repository as a submodule
mkdir external && cd external && git submodule add [email protected]:thxi/vec.git && cd ..

- Make a CMakeLists.txt in the external directory
echo "add_subdirectory(vec)" > external/CmakeLists.txt

- add the following lines to your main CMakeLists (which resides in the project root)
add_subdirectory(vec) 
target_link_libraries(${YOUR_TARGET} vec) # do not forget to set the target

- now you can use the lib


USE

After installing as described above:

#include <stdio.h>
#include <assert.h>
#include "vec/vec.h"
int main() {
  vec_type(int) v = vec_new();
  for (int i = 0; i < 10; i++) {
    bool ret = vec_push(v, i + 1);
    if (ret != true) {
        return 1; // error allocating memory
    }
  }
  for_each(v) { printf("%d ", v.buf[i]); }
  printf("\n");
  for (int i = 0; i < 5; i++) {
    vec_pop(v);
  }
  vec_shrink_to_fit(v);
  assert(v.size == 5);
  assert(v.cap == 5);
  for (int i = 0; i < 5; i++) {
    vec_pop(v);
  }
  assert(vec_empty(v) == true);

  vec_free(v); // do not forget to free
}


TODO:

[X] implement core methods as in https://en.cppreference.com/w/cpp/container/vector
[X] add sanitizer to test binary
[X] add tests
[X] add install
[X] structure tests
[ ] add example to a different directory

vec's People

Contributors

thxi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.