Skip to content

Commit

Permalink
added license; improved README; tidied passing_list/*
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Uemlianin committed Aug 13, 2011
1 parent 7f7c84f commit 01683fb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 11 deletions.
14 changes: 14 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2011, Ivan Uemlianin, <ivan@llaisdy.com>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

80 changes: 71 additions & 9 deletions README.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,81 @@
* simple_nif

A simple nif to pass a list of ints to a C function which returns a record of parameters.
.. contents::

Keywords:
** Overview

- erlang
- nif
- c
A simple nif to pass a list (or a tuple) of ints to a C function which returns a record of parameters.

More documentation forthcoming. For example:
I am releasing the code under the `ISC license`_.

- overview: erlang list or tuple to C array
- usage
- brief codewalk
** Setup

The two shell scripts compile.sh and compile_mac.sh are the scripts I've been using to compile smple_nif.c. n.b.: simple_main.c is just a little main() function to show the functions in simple.c in action.

There are two version of simple_nif.c: one accepts a tuple of ints, the other accepts a list of ints. Each has a corresponding test script simple_test.erl. Before you run compile.sh it is necessary to copy or symlink one of the versions from passing_{list,tuple}/ to the top-level directory.

** Usage

*** simple:get_params/1

The nif function get_params takes the ints and returns a record:

::

-record(params, {sum, mean, quartiles}).

For example:

::

1> simple:get_params([1,3,5,7,9,8,6,4,2]).
{params,45,5.0,{2.5,5.0,7.5}}
2>

*** simple_test:test/2

The test script simple_test.erl models more closely the usage I have in mind. The test function, simple_test:test(N, Max), generates an N long list of ints randomly generated in the range 1-Max. This input list is then split into windows, the size and offset of which are set in simple.hrl (defaults are 10 and 5). Finally, the window list is mapped using get_params. There are io:formats all along the way, e.g.:

::

1> simple_test:test(50,9).
Input: [2,1,5,1,2,9,4,6,6,6,
5,1,2,6,1,7,1,6,4,5,
1,5,8,3,8,4,9,3,3,7,
1,4,9,1,4,9,4,8,1,2,
6,9,5,1,1,3,7,8,6,4]

WindowList: [[2,1,5,1,2,9,4,6,6,6],
[9,4,6,6,6,5,1,2,6,1],
[5,1,2,6,1,7,1,6,4,5],
[7,1,6,4,5,1,5,8,3,8],
[1,5,8,3,8,4,9,3,3,7],
[4,9,3,3,7,1,4,9,1,4],
[1,4,9,1,4,9,4,8,1,2],
[9,4,8,1,2,6,9,5,1,1],
[6,9,5,1,1,3,7,8,6,4],
[3,7,8,6,4,0,0,0,0,0]]

{params,42,4.2,{2.0,4.5,6.0}}
{params,46,4.6,{2.0,5.5,6.0}}
{params,38,3.8,{1.0,4.5,6.0}}
{params,48,4.8,{3.0,5.0,7.0}}
{params,51,5.1,{3.0,4.5,8.0}}
{params,45,4.5,{3.0,4.0,7.0}}
{params,43,4.3,{1.0,4.0,8.0}}
{params,46,4.6,{1.0,4.5,8.0}}
{params,50,5.0,{3.0,5.5,7.0}}
{params,28,2.8,{0.0,1.5,6.0}}
ok
2>

** The code

# ... working here now ... #




.. ---------- ---------- ----------

.. _`ISC license`: http://en.wikipedia.org/wiki/ISC_license
2 changes: 1 addition & 1 deletion passing_list/simple_nif.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "erl_nif.h"
#include "simple.h"

static ERL_NIF_TERM get_params_nif(ErlNifEnv* env,
static ERL_NIF_TERM get_params_nif(ErlNifEnv* env,
int argc,
ERL_NIF_TERM argv[])
{
Expand Down
4 changes: 3 additions & 1 deletion passing_list/simple_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
-export([get_random_input/2]).

-record(params, {sum, mean, quartiles}).
-define(WINDOW_SIZE, 10).
-define(WINDOW_OFFSET, 5).

test(N, Max) ->
Input = get_random_input(N, Max),
io:format("Input: ~p~n~n", [Input]),
WindowList = get_window_list(Input, 10, 5),
WindowList = get_window_list(Input, ?WINDOW_SIZE, ?WINDOW_OFFSET),
io:format("WindowList: ~p~n~n", [WindowList]),
ParamsList = lists:map(fun(W) -> simple:get_params(W) end, WindowList),
lists:foreach(fun(P) ->
Expand Down

0 comments on commit 01683fb

Please sign in to comment.