Giter Site home page Giter Site logo

c_lists's Introduction

C Lists Library

Simple and leightwight library for lists datastructures. Supports modern build system MESON.
Generic values, no dependencies, C99 compliant, cross platform (Windows/Linux/MacOs).

  1. Getting Started
  2. Prerequisites
  3. Building
  4. Tests
  5. Generating Sources
  6. Why?
  7. Authors
  8. License

Getting Started

Clone project

git clone https://github.com/KubaTaba1uga/c_lists.git

Currently supported lists:

Variables to define:

  • ARL_VALUE_TYPE macro standing for type that You would like to use with arl_list.c

To confirm that everything is working we can go to examples/create_custom_types_gcc and compile the example.

gcc main.c ../../src/arl_list.c  -I../../include -o main -DARL_VALUE_TYPE=char && \
./main

Prerequisites

Install perequesites by scripts

./scripts/install_ruby.sh
./scripts/install_meson.sh

If installing perequesites by scripts has failed, You need to install them manually.

Building

Main tool used for building here is Meson.
However each list is composed of one src file and one header file, which should make the lib easy to compile with any other tool.

There are three building options available:

  • enable_tests flag indicating tests compilation
  • arl_prefix prefix for array list's public interface
  • arl_type type of array list's elements

Create build with some options configured

meson setup build -Denable_tests=false -Darl_prefix=my_prefix -Darl_type=float

Compile build

meson compile -C build

Src file and header file shoudl appear in build dir build/my_prefix_list.c build/my_prefix_list.h.
These files are ready to include into Your project. Meson is capable of generating shared library, static library and more. Check it out if You need more than just src files.

Tests

Create build with tests enabled

meson setup build -Denable_tests=true

Run all tests

meson test -C build

Generating Sources

Sources for particullar list can be generated to make things easier.

python3 scripts/generate_sources.py <source file> <new prefix> <new type> (<dest dir>)
  • source file is path to the particullar list, ex. src/arl_list.c.
  • new prefix is prefix which will be used in new src, ex. arl.
  • new type is type of list's elements, ex. void *,
  • dest dir is path to directory in which sources will appear, ex. .. This is optional argument.

Why

Library can be used as any other C data structure library, by compiling with default settings.

gcc -c src/arl_list.c -I include/ -o arl_list_lib.o

This is basic scenario where list's genericness is done by treating it's elements as of void * type.

As this may be fine for most use cases, this kind of implementation is creating unnecessery limits.
Let's imagine that You would like to use a list for char type, to create kind of dynamic string.
It would require allocating memory for each of the chars before appending the char as an element

  // Dynamic string
  char *my_string_0 = "nanananananana";
  char *my_sting_1 = "RTRTRTRTRTRTR";
  char *c;
  arl_ptr l;
  size_t i;

  // Creates list's instance
  arl_create(&l, 10);

  for (i = 0; i < strlen(my_string_0); i++) {
    c = malloc(sizeof(c));
    *c = my_string_0[i];
    // Appends my_string_0's values
    arl_append(l, c);
  }
  for (i = 0; i < strlen(my_string_1); i++) {
    c = malloc(sizeof(c));
    *c = my_string_0[i];
    // Appends my_string_1's values
    arl_append(l, c);
  }
  ...

pretty painfull ain't it?

So why can't we use the same mechanism as for void * but for char?

We can easilly imagine the same list but working on char type and this is exactly what this library is about. You can create the same list that You are usually using (with void * type) but with any type that You need.

gcc -c src/arl_list.c -I include/ -o arl_list_lib.o -DARL_VALUE_TYPE=char
  // Dynamic string
  char *my_string_0 = "nanananananana";
  char *my_sting_1 = "RTRTRTRTRTRTR";
  char c;
  arl_ptr l;
  size_t i;

  arl_create(&l, 10);

  for (i = 0; i < strlen(my_string_0); i++) {
    arl_append(l, my_string_0[i]);
  }
  for (i = 0; i < strlen(my_string_1); i++) {
     arl_append(l, my_string_1[i]);
  }
  ...

BTW What is nice about C Lists Library?

  • Only two files are required to use particullar list, source file and header file.
  • There is no macro overusage so the library is simple to understand and use.
  • Library can be generated for basic types (char, float, int etc.) and for void *.
    There are plans to allow custom structures and enums, if that's sth that You need, let me know ;)
  • There is a mechanism to delete almost all macro, look on Generating Sources section.
  • There is mechanism to delete names duplications, look on Generating Sources section.
  • Meson support.
  • Only lists, no unnecessary stuff.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

c_lists's People

Contributors

kubataba1uga 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.