FE_misc_lib.cpp
上传用户:italyroyal
上传日期:2013-05-06
资源大小:473k
文件大小:3k
- ///////////////////////////////////////////////////////////////////////////////
- // This is a part of the Feature program.
- // Version: 1.0
- // Date: February 22, 2003
- // Programmer: Oh-Wook Kwon
- // Copyright(c) 2003 Oh-Wook Kwon. All rights reserved. owkwon@ucsd.edu
- ///////////////////////////////////////////////////////////////////////////////
- #include "StdAfx.h"
#include <malloc.h>
- #include <ctime>
- #include "FE_feature.h"
- #ifdef _WIN32
- long Fe::lrand48()
- {
- return (long)rand();
- }
- void Fe::srand48(long seedvar)
- {
- srand((int)seedvar);
- }
- #endif
-
/*************************
memory alloc and free
*************************/
void *Fe::m_alloc(int size)
{
void *temp;
assert(size>0);
if(( temp = (void *) malloc(size)) == NULL)
err_quit("Memory insufficientn");
return(temp);
}
-
void *Fe::c_alloc(int num_of_elts, int size)
{
void *temp;
assert(size>0);
if(( temp = (void *) calloc(num_of_elts,size)) == NULL)
err_quit("Memory insufficientn");
return(temp);
}
-
void *Fe::re_alloc(void *block, int size)
{
void *temp;
assert(size>0);
if(( temp = (void *) realloc(block,size)) == NULL)
err_quit("Memory insufficientn");
return(temp);
}
-
char **Fe::alloc2d(int dim1, int dim2, int size)
{
int i;
- assert(size>0);
unsigned nelem = (unsigned) dim1 * dim2;
- assert(nelem>0);
char *p = (char *) c_alloc(nelem, (unsigned) size);
- assert(p);
char **pp = (char **) c_alloc((unsigned) dim1, (unsigned) sizeof(char *));
- assert(pp);
for (i = 0; i < dim1; i++) pp[i] = p + (i * dim2 * size);
return (pp);
}
- char ***Fe::alloc3d(int dim1, int dim2, int dim3, int size)
- {
- int i;
- assert(size>0);
- unsigned nelem = (unsigned) dim1 * dim2 * dim3;
- assert(nelem>0);
- char *p = (char *) c_alloc(nelem, (unsigned) size);
- assert(p);
- char **pp = (char **) c_alloc((unsigned) dim1*dim2, (unsigned) sizeof(char *));
- assert(pp);
- char ***ppp = (char ***) c_alloc((unsigned) dim1, (unsigned) sizeof(char **));
- assert(ppp);
- for (i = 0; i < dim1; i++) ppp[i] = pp + (i * dim2 * sizeof(char*));
- for (i = 0; i < dim1*dim2; i++) pp[i] = p + (i * dim3 * size);
- return ppp;
- }
-
void Fe::free2d(void **p)
{
if (p) {
if(p[0]) free(p[0]);
free(p);
}
return;
}
-
- void Fe::free3d(void ***p)
- {
- if(p){
- if(p[0]) {
- if(p[0][0]) free(p[0][0]);
- free(p[0]);
- }
- free(p);
- }
- return;
- }
-
/***********************************************
returns non-zero, if the h/w is little endian
***********************************************/
int Fe::little_endian()
{
char b[4];
register long *l = (long *) b;
*l = 1;
return ((int) b[0]);
}
-
/*================== Supporting Routines =====================*/
float Fe::sum(int nstart, int nfinal, float *seq)
{
int i;
float total=0;
for(i=nstart; i<=nfinal; i++) total += seq[i];
return(total);
}
-
float Fe::product (int nstart, int nfinal, float *seq )
{
int i;
float prod = 1;
for(i=nstart-1; i<nfinal; i++) prod *= seq[i];
return(prod);
}
-
float Fe::power(float x, int n)
{
float p;
if(n>0) for(p=1; n>0; --n) p=p*x;
else if(n<0) for(p=1; n<0; ++n) p=p/x;
else p=1;
return(p);
}
-
int Fe::ipower(int x, int n)
{
int p = 1;
for( ; n > 0; --n) p *= x ;
return p;
}