File: util.c

package info (click to toggle)
gltron 0.70final-9
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,208 kB
  • ctags: 5,051
  • sloc: ansic: 19,181; sh: 3,443; cpp: 973; makefile: 269
file content (41 lines) | stat: -rw-r--r-- 811 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* small utility functions */

#include <stdlib.h>
#include "base/nebu_random.h"
#include "base/nebu_types.h"

void randomPermutation( int N, int *nodes )
{
  int i;
  for(i = 0; i < N; i++)
    nodes[i] = i;

  for(i = 0; i < N - 1; i++) {
    int s, t;
    int tmp;
    t = N - 1 - i;
    // s = (int) ((float)( t + 1 ) * trand() / (RAND_MAX + 1.0f));
    s = trand() % (t + 1);
    tmp = nodes[t];
    nodes[t] = nodes[s];
    nodes[s] = tmp;
  }
}

void clamp( float *f, float min, float max )
{
  if(*f < min) *f = min;
  else if(*f > max) *f = max;
}

void addList(List **l, void* data) {
	List *p;
	if(*l == NULL) {
		*l = (List*) malloc(sizeof(List));
		(*l)->next = NULL;
	}
	for(p = *l; p->next != NULL; p = p->next);
	p->next = (List*) malloc(sizeof(List));
	p->next->next = NULL;
	p->data = data;
}