File: scramble.c

package info (click to toggle)
staden 2.0.0%2Bb11-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,584 kB
  • sloc: ansic: 240,605; tcl: 65,360; cpp: 12,854; makefile: 11,203; sh: 3,023; fortran: 2,033; perl: 63; awk: 46
file content (54 lines) | stat: -rw-r--r-- 1,220 bytes parent folder | download | duplicates (5)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdlib.h>

#include "sequence_formats.h"
/* routines for scrambling a sequence */
/*
   send seq of length seq_len
   put in a scramble structure
   generate seq_len random numbers and 
   put them into the scramble structure
   sort the scramble structure
   copy the scrambled sequence to seq
*/
struct char_int_pair {
    char character;
    int  number;
};

int compare_pair(const void *vp1, const void *vp2) {
    struct char_int_pair *p1 = (struct char_int_pair *)vp1;
    struct char_int_pair *p2 = (struct char_int_pair *)vp2;

    if (p1->number < p2->number)
	return -1;
    else if (p1->number == p2->number)
	return 0;
    else
	return 1;
}
 
int scramble_seq ( char seq[], int seq_len, int seed ) {

    int i;
    struct char_int_pair *scrambler;

    if ( ! ( scrambler = (struct char_int_pair *) malloc ( sizeof(struct char_int_pair)*(seq_len) ))) {
        return -1;
    }

    srand(seed);
    for(i=0;i<seq_len;i++) {
	scrambler[i].character = seq[i];
	scrambler[i].number = rand();
    }

    qsort ((void *) scrambler, seq_len, sizeof(struct char_int_pair),
	   compare_pair);

    for(i=0;i<seq_len;i++) {
	seq[i] = scrambler[i].character;
    }

    free ( scrambler );
    return 0;
}