Giter Site home page Giter Site logo

c_collections's Introduction

Collections

A collection of Java style collections written in C So far, the collections included are:

  • LinkedList
  • Heap

These collections include functions such as foreach, map, filter and fold that can be used to use a functional approach. Both collections can hold any type, as they take void* as value for each element

Examples:

  • LinkedList:
#include <linked_list.h>

// foreach will take any void function 
// that takes a void* as an argument
void print(void* elem) {
	int* i = (int*)elem;
	printf("%d\n", *i);
}

int main()
{
	list_t l = init_list();
  list_t* list = &l;
  // Adds a value at the end of the list
  add_end(list, &value);
  // as the name implies, this one adds at the start
  add_start(list, &value);
  // will print every value contained in the list
  foreach_list(list, &print);  
}
  • Heap:
#include "heap.h"
// Anything is valid as a comparator as long as it returns an int and takes 2 void*
inline int min_int(void* elem1, void* elem2) {
	return memcmp(&elem1, &elem2, sizeof(elem1)) < 0;
}

// Exactly same as the print for lists
void print(void* elem) {
	int* i = (int*)elem;
	printf("%d\n", *i);
}

int main() {
  // Initialise a new heap that uses min_int as a comparator
  // This allows us to have a single collection but have various implementations
  // such as max heap, min heap, min heap with strings
  heap_t h = init_heap(256, &min_int);
	heap_t* heap = &h;
	// inserts a value inside the heap
	insert_heap(heap, &value);
	// prints every value contained in the heap
	foreach_heap(heap, &print);
	// Frees the memory used by the heap
	destroy_heap(heap);
}

TODO

  • Make this a shared library
  • Better commenting in code

c_collections's People

Contributors

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