GithubHelp home page GithubHelp logo

julstrat / sh_libs Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 1.76 MB

Single Header ANSI C mini libraries

License: ISC License

Makefile 1.04% C 98.70% Shell 0.26%
karatsuba-algorithm union-find-algorithm merge-algorithms bisection isc-license single-header-library

sh_libs's Introduction

sh_libs

Single Header ANSI C mini libraries

sh_karatsuba.h

Polynomial multiplication using Karatsuba algorithm.

#include "sh_karatsuba.h"
#include <assert.h>

KARATSUBA(int)
KARATSUBA(double)

#define BENCH_SZ 1024*16
#define TCALLOC(type, x) (type *)(calloc(x, sizeof (type)))

int main(void) {
    int i, j;
    int *ai, *bi, *ci, *expi;
    double *ad, *bd, *cd, *expd;
    
    /* int type */
    ai = TCALLOC(int, BENCH_SZ);
    bi = TCALLOC(int, BENCH_SZ);
    ci = TCALLOC(int, 2*BENCH_SZ-1);
    expi = TCALLOC(int, 2*BENCH_SZ-1);
    for (i = 0; i < BENCH_SZ; i++) {
        ai[i] = rand(); bi[i] = rand();
    }
    for (i = 0; i < BENCH_SZ; i++) {
        for (j = 0; j < BENCH_SZ; j++) {
            expi[i+j] += ai[i] * bi[j];
        }
    }

    karatsuba_int(ai, bi, BENCH_SZ, ci);
    for (i = 0; i < 2*BENCH_SZ-1; i++) {
        assert(expi[i] = ci[i]);
    }

    /* double type */    
    ad = TCALLOC(double, BENCH_SZ);
    bd = TCALLOC(double, BENCH_SZ);
    cd = TCALLOC(double, 2*BENCH_SZ-1);
    expd = TCALLOC(double, 2*BENCH_SZ-1);
    for (i = 0; i < BENCH_SZ; i++) {
        ad[i] = rand()*1.0; bd[i] = rand()*1.0;
    }
    for (i = 0; i < BENCH_SZ; i++) {
        for (j = 0; j < BENCH_SZ; j++) {
            expd[i+j] += ad[i] * bd[j];
        }
    }

    karatsuba_double(ad, bd, BENCH_SZ, cd);
    for (i = 0; i < 2*BENCH_SZ-1; i++) {
        assert(expd[i] = cd[i]);
    }

    return 0;
}

sh_bisect.h

Ordered array bisection algorithms.

void *bisect_left(const void *key, const void *arr,
                  size_t el_num, size_t el_size,
                  int (*compare) (const void *, const void *));

void *bisect_right(const void *key, const void *arr,
                   size_t el_num, size_t el_size,
                   int (*compare) (const void *, const void *));

sh_merge.h

Ordered array merge algorithms.

void *merge(const void *arr_a, size_t num_el_a,
            const void *arr_b, size_t num_el_b,
            size_t el_size, int (*compare) (const void *,
                                            const void *));

void *merge_into(void *dst, size_t num_el_dst,
                 const void *src, size_t num_el_src,
                 size_t el_size, int (*compare) (const void *,
                                                 const void *));

sh_uf.h

Union Find data structure - Rem's algorithm.

UNION_FIND uf_create(size_t size);

int uf_union(UNION_FIND uf, size_t x, size_t y);

int uf_connected(UNION_FIND uf, size_t x, size_t y);

size_t uf_find(UNION_FIND uf, size_t x);

void uf_free(UNION_FIND uf);

sh_libs's People

Contributors

julstrat avatar

Watchers

 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.