项目作者: AnthonyMikh

项目描述 :
General data collections in pure C
高级语言: C
项目地址: git://github.com/AnthonyMikh/SGDC.git
创建时间: 2017-04-03T09:52:11Z
项目社区:https://github.com/AnthonyMikh/SGDC

开源协议:MIT License

下载


What is SGDC

SGDC stands for “student’s general data collection”. SGDC is a collection of abstract data structures written in pure C. Unlike many other similar libraries, it doesn’t mess around casting from and to void*. SGDC also isn’t built upon typedefs so it lets you have several SGDC data structures in one program holding values of different types.

Usage

Suppose you want to have binary tree holding doubles. In order to do it, put sgdc directory along with your sources and write in the beginning of code file:

  1. #define T double
  2. #include "sgdc/bintree/bintree.h"
  3. ...
  4. /* code stuff */

Now you’re able to do things like this:

  1. SGDC_bintree_double* mytree = SGDC_bintree_create_double(0.5d);
  2. SGDC_bintree_insertr_double(mytree, 0.888d);

As you can see, T defines the type of data structure. All SGDC functions begin with SGDC_ and tailed by underscore followed by type name in order to escape name collisions.

More features

  • Of course, you can have more than one typed binary trees. Just define T before including header file:

    1. #define T char
    2. #include "sgdc/bintree/bintree.h"
    3. #define T float
    4. #include "sgdc/bintree/bintree.h"
    5. #define T long
    6. #include "sgdc/bintree/bintree.h"
    7. ...
    8. /* working with trees of chars, floats and longs*/
  • You can also not to define any T. In this case data structure will hold int values:

    1. #undef T
    2. #include "sgdc/bintree/bintree.h"
    3. ...
    4. /* works with "SGDC_bintree_create_int" etc. */
  • In case you find constant SGDC_ too noisy, you can change it:

    1. #define SGDC_PREFIX S
    2. #include "sgdc/bintree/bintree.h"
    3. ...
    4. /* now work with "S_bintree_create_int" */

    Or even remove it completely:

    1. #define SGDC_PREFIX
    2. /* now functions have no prefix*/
  • If you find typing bintree_char too tiring (especially if you have only one instance of structure), you can cut the corners:

    1. #define SGDC_SIMPLE_DATATYPE
    2. #define T char
    3. #include "sgdc/bintree/bintree.h"
    4. ...
    5. //now you're able to write stuff like this:
    6. data_node_t* tree = SGCD_bintree_create_char('☺');

    Note that if you try to use this option more than once, you will get an error during compilation.

  • You can ease typing long names using qualify macros:

    1. #define T char
    2. #include "sgdc/bintree/bintree.h"
    3. ...
    4. SGDC_QP(bintree)* tree = SGDC_QP(bintree_create)('A');

    You can type even less if you enable simple qualifiers:

    1. #define SGDC_SIMPLE_QUALIFIERS
    2. #include "sgdc/bintree/bintree.h"
    3. ...
    4. QP(bintree)* tree = QP(bintree_create)('A');

Known limitations

  • Since name qualifying is done via macros, you can’t use names containg * or spaces. Use typedef in order to solve this issue.
  • This library is not thread-safe and it will not be like this in near future.
  • This library uses macros with names containing only small letters. This can insult you.