Giter Site home page Giter Site logo

itertools-c's Introduction

Itertools library for C

This is my effort to implement python's itertools library for C.

Methods supported so far -

For creating iterator
  • newArrayIter(array, array_length, size_of_element)
  • new_list_iter(list)
Itertools library functions
  • count(init_val, step)
  • cycle(array, array_length, size_of_element)
  • repeat(value)
  • accumulate(iterator, function, init_val)
  • compress(iterator1, iterator2)
  • islice(iterator, start, stop, step)
Some other functions
  • permutations(array, n, r, size_of_element)
  • combinations(array, n, r, size_of_element)
  • combinations_replacement(array, n, r, size_of_element)

Example

How to create array iterator -
iter* i = newArrayIter(a, n, sizeof(*a)); // Creates Array iterator.
while(has_next(i))
  printf("%d ", *(int*) next(i));
free_iter(i);
Accumulation example -
int fun(const void* a, const void* b)
{
	return *(int*) a + *(int*) b;
}

...

iter* i = accumulate(iter, fun, 0);   // iter is iterator.
while(has_next(i))
  printf("%d ", *(int*) next(i));   // prints cumulative sum.
free_iter(i);
Permutations -
iter* i = permutations(a, n, r, sizeof(*a));  // a is array
int* c;
printf("Permutations are \n");
while (has_next(i))
{
  c = (int*) next(i);
  for (int j = 0; j < r; j++)
    printf("%d ", c[j]);
  printf("\n");
}
free_iter(i);

How to test

File main.c contains examples of how use this library.

make test           # Make files
./test

To test permutations

make permtest       # Make permutations test
./permtest          # Run it

To test combinations

make combtest       # Make combinations test
./combtest          # Run it

I hope u like it. ;)

itertools-c's People

Contributors

shreyash14s avatar

Stargazers

 avatar  avatar  avatar

Watchers

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