Giter VIP home page Giter VIP logo

resizable_circular_buffer_c's Introduction

Circular buffer

Simple, resizable circular buffer, currently only holds ints but will be made more generic in future. Not thread-safe.

Tests

The tests use unity. To run the tests, run:

    make test

Overview

The interface is pretty much straightforward. It's listed in list.h

List *new_list();
void delete_list(List *l);
void list_push_to_front(List *l, int v);
void list_push_to_back(List *l, int v);
int list_pop_from_front(List *l);
int list_pop_from_back(List *l);
int list_get_head(List *l);

There are two additional 'functions' for iteration without mutation:

  • list_foreach
  • list_foreach_reverse

They aren't function's per se, rather they are macros which abstract away the nitty-gritties of iteration. Credits go to attractivechaos's khash from where I got the idea of using macros in this way rather than taking in a function pointer, or returning an OOP-like iterator object. For example, the list_foreach is defined as follows:

#define list_foreach(list, elem_var, code)      \
    {                                           \
        int pos = list->first;                  \
        while (1) {                             \
            elem_var = list->elements[pos];     \
            code;                               \
            if (pos == list->last) break;       \
            pos = MOD(pos + 1, list->capacity); \
        }                                       \
    }

The internals don't matter. list_foreach is then used as follows:

int main(){
    // setup list
    List *l = new_list();
    for (int i = 0; i < 100; i++) {
        list_push_to_back(l, i);
    }

    // iterate
    list_foreach(l, curr_list_elem, {
        printf("n = %d", curr_list_elem)
    });

    // cleanup list
    delete_list(l);
}

resizable_circular_buffer_c's People

Contributors

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