File: vector.c

package info (click to toggle)
r-cran-eco 3.1-7-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 672 kB
  • ctags: 163
  • sloc: ansic: 4,183; makefile: 7
file content (120 lines) | stat: -rw-r--r-- 2,632 bytes parent folder | download | duplicates (7)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/******************************************************************
  This file is a part of eco: R Package for Fitting Bayesian Models
  of Ecological Inference for 2x2 Tables
  by Kosuke Imai and Ying Lu
  Copyright: GPL version 2 or later.
*******************************************************************/

#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <R_ext/Utils.h>
#include <R_ext/PrtUtil.h>
#include <R.h>

int* intArray(int num) {
  int *iArray = (int *)malloc(num * sizeof(int));
  if (iArray)
    return iArray;
  else {
    error("Out of memory error in intArray\n");
    return NULL;
  }
}

int** intMatrix(int row, int col) {
  int i;
  int **iMatrix = (int **)malloc(row * sizeof(int *));
  if (iMatrix) {
    for (i = 0; i < row; i++) {
      iMatrix[i] = (int *)malloc(col *  sizeof(int));
      if (!iMatrix[i])
	error("Out of memory error in intMatrix\n");
    }
    return iMatrix;
  }
  else {
    error("Out of memory error in intMatrix\n");
    return NULL;
  }
}

double* doubleArray(int num) {
  //double *dArray = (double *)malloc(num * sizeof(double));
  double *dArray = Calloc(num,double);
  if (dArray)
    return dArray;
  else {
    error("Out of memory error in doubleArray\n");
    return NULL;
  }
}

double** doubleMatrix(int row, int col) {
  int i;
  //double **dMatrix = (double **)malloc((size_t)(row * sizeof(double *)));
  double **dMatrix = Calloc(row,double*);
  if (dMatrix) {
    for (i = 0; i < row; i++) {
      dMatrix[i] = Calloc(col,double);
      if (!dMatrix[i]) {
        error("Out of memory error in doubleMatrix\n");
        return NULL;
      }
    }
    return dMatrix;
  }
  else {
    error("Out of memory error in doubleMatrix\n");
    return NULL;
  }
}

double*** doubleMatrix3D(int x, int y, int z) {
  int i;
  double ***dM3 = (double ***)malloc(x * sizeof(double **));
  if (dM3) {
    for (i = 0; i < x; i++)
      dM3[i] = doubleMatrix(y, z);
    return dM3;
  }
  else {
    error("Out of memory error in doubleMatrix3D\n");
    return NULL;
  }
}

long* longArray(int num) {
  long *lArray = (long *)malloc(num * sizeof(long));
  if (lArray)
    return lArray;
  else {
    error("Out of memory error in longArray\n");
    return NULL;
  }
}

void FreeMatrix(double **Matrix, int row) {
  int i;
  for (i = 0; i < row; i++)
    Free(Matrix[i]);
  Free(Matrix);
}

void FreeintMatrix(int **Matrix, int row) {
  int i;
  for (i = 0; i < row; i++)
    free(Matrix[i]);
  free(Matrix);
}

void Free3DMatrix(double ***Matrix, int index, int row) {
  int i;
  for (i = 0; i < index; i++)
    FreeMatrix(Matrix[i], row);
  free(Matrix);
}