Giter Club home page Giter Club logo

list's Introduction

list

type-safe, double-linked dynamic array for C

Usage

This is a guide for library. Full documentation is available below. At first you need to initialize list.

list_int_t lst;
list_init(&lst);

Initialization just fills structure with zeros so in some cases you can skip initialization.

You can put values at the end of list useing list_append. This is the only way to put first value into the list.

list_append(&lst, 42);

To get a value from the list use list_get. This function returns pointer to the value or NULL if index is invalid.

int *ptr0 = list_get(&lst, 0);

These pointers can be used to change the value. Same is done with list_set

list_set(&lst, 0, 43);

Negative indicies are also valid and can be used for indexing from the end of the list (-1 - last value; -2 - value before the last).

int *ptr1 = list_get(&lst, -1);

You also can insert value before any other using list_insert or remove value using list_remove.

// Here comments contains state of list

// 43
list_insert(&lst, 0, 55);
// 55 43
list_insert(&lst, -1, 4);
// 55 4 43
list_remove(&lst, -1);
// 43 4

To prevent memory leaks use list_destroy to remove all values from the list.

list_destroy(&lst);

Documentation

Functions

Some notes

  • All lists should be passed to function as pointers.
  • All functions are defined as macrosses and can't be passed as parameters to other functions.
  • Some finctions can't be called inline. Usually they returns nothing so inline calls are useless.

list_init

list_init(L)

Inializes the list. This function just fills the structure with zeros so ould be skipped. Returns same value as memset.

list_destroy

list_destroy(L)

Remove all values from the list. That function leaves trash in passed structure. Use list_init before using list again. Returns nothing.

list_append

list_append(L, value)

Puts value into the end of the list. Returns new size of the list.

list_insert

list_insert(L, index, value)

Puts value at given index, shifting all values after it to higher indicies. Does nothing if index isn't in the list. Returns new size of list.

list_get

list_get(L, index)

Reads value at given index. Returns pointer to the value or NULL if index isn't in the list.

list_set

list_set(L, index, value)

Sets new value at given index. Returns new value.

list_remove

list_remove(L, index)

Returns value at given index from the list, shifting all values after it to lower indicies. Does nothing if index isn't in the list. Returns new size of list.

list_copy

list_copy(source, destination)

Copies list source to destination. List destination should be empty or destroyed to prevent memory leak. Returns nothing.

list_apply

list_apply(L, function)

Calling function for each value in the list, passing pointers to value. Return value from function is ignored. Returns nothing.

Types

Each list has its own type to store. Usually list type looks like list_TYPE_t. Some predefined types are in the table below.

list type value type usage
list_int_t int
list_char_t char
list_float_t float
list_double_t double
list_str_t char* string pointers
list_ptr_t void* any pointers

You can also define your own type of list.

struct YOUR_TYPE;
typedef list_t(struct YOUR_TYPE) YOUR_LIST_TYPE;

Notes

  1. To get size of list use variable lst.base.size. This is used internally so do not change it
  2. list_append and list_remove returns new size of list

list's People

Contributors

abirvalarg avatar

Watchers

James Cloos avatar  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.