Skip to content
/ JPList Public

A dynamic array list implementation in C, usable for custom types

Notifications You must be signed in to change notification settings

juripan/JPList

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JPLIST

A dynamic array list implementation in C. Works for any type (even your custom structs)

Quick start:

  1. Include the jplist header file
#include "jplist.h"
  1. Make a print format macro for the types that you will use inside of the JPList struct (used for printing out the list)
#define format_Person(i) "(name: \"%s\", age: %d)", iget(list, Person, i).name, iget(list, Person, i).age
  1. Make a comparison function that gets called when comparing items in the JPList struct (it should act like strcmp from "string.h")
int int_cmp(int x, int y){return x - y;}
  1. Call the "ADD_BASE_FUNC" macro to initialize all of the functions that can be used for the JPList (pass in the type, print format macro you made earlier, and the comparison function)
ADD_BASE_FUNC(int, format_int, int_cmp)

IMPORTANT: you cannot use pointers as a type in the list UNLESS you typedef it
For example:

typedef int* int_ptr;
ADD_BASE_FUNC(int_ptr, format_int_ptr, int_ptr_cmp)

NOTE: you can also add specific features only by using the "ADD" macros defined in the jplist header file

Indexing:

Since the JPList struct stores the items in a void* so indexing the items directly is a mess, thats why I added a "IGET" macro
Example usage: \

int val = IGET(ints_list, int, 5);
IGET(ints_list, int, 4) = 21;