MyUtils.cpp
上传用户:szb0815
上传日期:2007-06-13
资源大小:338k
文件大小:9k
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "MyUtils.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- float** __fastcall newmatrix(int n, int m)
- {
- float **TempPointer = new float*[n];
- for (int i=0;i<n;i++)
- {
- TempPointer[i] = new float[m];
- for (int j=0; j<m; j++)
- {
- TempPointer[i][j] = 0;
- }
- }
- return TempPointer;
- }
- void __fastcall deletematrix(float** Matrix, int n)
- {
- for (int i=0; i<n; i++)
- {
- delete Matrix[i];
- }
- delete Matrix;
- }
- int** __fastcall newimatrix(int n, int m)
- {
- int **TempPointer = new int*[n];
- for (int i=0;i<n;i++)
- {
- TempPointer[i] = new int[m];
- for (int j=0; j<m; j++)
- {
- TempPointer[i][j] = 0;
- }
- }
- return TempPointer;
- }
- void __fastcall deleteimatrix(int** Matrix, int n)
- {
- for (int i=0; i<n; i++)
- {
- delete Matrix[i];
- }
- delete Matrix;
- }
- float * __fastcall newvector(int n)
- {
- float *TempVector = new float[n];
- for (int i=0; i<n; i++)
- {
- TempVector[i] = 0;
- }
- return TempVector;
- }
- int * __fastcall newivector(int n)
- {
- int *TempVector = new int[n];
- for (int i=0; i<n; i++)
- {
- TempVector[i] = 0;
- }
- return TempVector;
- }
- #define NRANSI
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
#define M 7
#define NSTACK 50
- void sort2fi(unsigned long n, float arr[], int brr[])
- {
unsigned long i,ir=n,j,k,l=1;
int *istack,jstack=0;
float a,temp;
int b;
istack=ivector(1,NSTACK);
for (;;) {
if (ir-l < M) {
for (j=l+1;j<=ir;j++) {
a=arr[j];
b=brr[j];
for (i=j-1;i>=l;i--) {
if (arr[i] <= a) break;
arr[i+1]=arr[i];
brr[i+1]=brr[i];
}
arr[i+1]=a;
brr[i+1]=b;
}
if (!jstack) {
free_ivector(istack,1,NSTACK);
return;
}
ir=istack[jstack];
l=istack[jstack-1];
jstack -= 2;
} else {
k=(l+ir) >> 1;
SWAP(arr[k],arr[l+1])
SWAP(brr[k],brr[l+1])
if (arr[l] > arr[ir]) {
SWAP(arr[l],arr[ir])
SWAP(brr[l],brr[ir])
}
if (arr[l+1] > arr[ir]) {
SWAP(arr[l+1],arr[ir])
SWAP(brr[l+1],brr[ir])
}
if (arr[l] > arr[l+1]) {
SWAP(arr[l],arr[l+1])
SWAP(brr[l],brr[l+1])
}
i=l+1;
j=ir;
a=arr[l+1];
b=brr[l+1];
for (;;) {
do i++; while (arr[i] < a);
do j--; while (arr[j] > a);
if (j < i) break;
SWAP(arr[i],arr[j])
SWAP(brr[i],brr[j])
}
arr[l+1]=arr[j];
arr[j]=a;
brr[l+1]=brr[j];
brr[j]=b;
jstack += 2;
if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
}
#undef M
#undef NSTACK
#undef SWAP
#undef NRANSI
- #define NRANSI
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
- #define M 7
- #define NSTACK 50
- void sort2i(unsigned long n, int arr[], int brr[])
- {
- unsigned long i,ir=n,j,k,l=1;
- int *istack,jstack=0;
- int a,b,temp;
- istack=ivector(1,NSTACK);
- for (;;) {
- if (ir-l < M) {
- for (j=l+1;j<=ir;j++) {
- a=arr[j];
- b=brr[j];
- for (i=j-1;i>=l;i--) {
- if (arr[i] <= a) break;
- arr[i+1]=arr[i];
- brr[i+1]=brr[i];
- }
- arr[i+1]=a;
- brr[i+1]=b;
- }
- if (!jstack) {
- free_ivector(istack,1,NSTACK);
- return;
- }
- ir=istack[jstack];
- l=istack[jstack-1];
- jstack -= 2;
- } else {
- k=(l+ir) >> 1;
- SWAP(arr[k],arr[l+1])
- SWAP(brr[k],brr[l+1])
- if (arr[l] > arr[ir]) {
- SWAP(arr[l],arr[ir])
- SWAP(brr[l],brr[ir])
- }
- if (arr[l+1] > arr[ir]) {
- SWAP(arr[l+1],arr[ir])
- SWAP(brr[l+1],brr[ir])
- }
- if (arr[l] > arr[l+1]) {
- SWAP(arr[l],arr[l+1])
- SWAP(brr[l],brr[l+1])
- }
- i=l+1;
- j=ir;
- a=arr[l+1];
- b=brr[l+1];
- for (;;) {
- do i++; while (arr[i] < a);
- do j--; while (arr[j] > a);
- if (j < i) break;
- SWAP(arr[i],arr[j])
- SWAP(brr[i],brr[j])
- }
- arr[l+1]=arr[j];
- arr[j]=a;
- brr[l+1]=brr[j];
- brr[j]=b;
- jstack += 2;
- if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
- if (ir-i+1 >= j-l) {
- istack[jstack]=ir;
- istack[jstack-1]=i;
- ir=j-1;
- } else {
- istack[jstack]=j-1;
- istack[jstack-1]=l;
- l=i;
- }
- }
- }
- }
- #undef M
- #undef NSTACK
- #undef SWAP
- #undef NRANSI
- #define NRANSI
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
#define M 7
#define NSTACK 50
- void sort3fff(unsigned long n, float arr[], float brr[], float crr[])
- {
unsigned long i,ir=n,j,k,l=1;
int *istack,jstack=0;
float a,temp;
float b,c;
istack=ivector(1,NSTACK);
for (;;) {
if (ir-l < M) {
for (j=l+1;j<=ir;j++) {
a=arr[j];
b=brr[j];
c=crr[j];
for (i=j-1;i>=l;i--) {
if (arr[i] <= a) break;
arr[i+1]=arr[i];
brr[i+1]=brr[i];
crr[i+1]=crr[i];
}
arr[i+1]=a;
brr[i+1]=b;
crr[i+1]=c;
}
if (!jstack) {
free_ivector(istack,1,NSTACK);
return;
}
ir=istack[jstack];
l=istack[jstack-1];
jstack -= 2;
} else {
k=(l+ir) >> 1;
SWAP(arr[k],arr[l+1])
SWAP(brr[k],brr[l+1])
SWAP(crr[k],crr[l+1])
if (arr[l] > arr[ir]) {
SWAP(arr[l],arr[ir])
SWAP(brr[l],brr[ir])
SWAP(crr[l],crr[ir])
}
if (arr[l+1] > arr[ir]) {
SWAP(arr[l+1],arr[ir])
SWAP(brr[l+1],brr[ir])
SWAP(crr[l+1],crr[ir])
}
if (arr[l] > arr[l+1]) {
SWAP(arr[l],arr[l+1])
SWAP(brr[l],brr[l+1])
SWAP(crr[l],crr[l+1])
}
i=l+1;
j=ir;
a=arr[l+1];
b=brr[l+1];
c=crr[l+1];
for (;;) {
do i++; while (arr[i] < a);
do j--; while (arr[j] > a);
if (j < i) break;
SWAP(arr[i],arr[j])
SWAP(brr[i],brr[j])
SWAP(crr[i],crr[j])
}
arr[l+1]=arr[j];
arr[j]=a;
brr[l+1]=brr[j];
brr[j]=b;
crr[l+1]=crr[j];
crr[j]=c;
jstack += 2;
if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
}
#undef M
#undef NSTACK
#undef SWAP
#undef NRANSI
- #define NRANSI
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
#define M 7
#define NSTACK 50
- void sort2fi_0(unsigned long n, float arr[], int brr[])
- {
unsigned long i,ir=n-1,j,k,l=0;
int *istack,jstack=0;
float a,temp;
int b;
istack=ivector(1,NSTACK);
for (;;) {
if (ir-l < M) {
for (j=l+1;j<ir;j++) {
a=arr[j];
b=brr[j];
for (i=j-1;i>=l;i--) {
if (arr[i] <= a) break;
arr[i+1]=arr[i];
brr[i+1]=brr[i];
}
arr[i+1]=a;
brr[i+1]=b;
}
if (!jstack) {
free_ivector(istack,1,NSTACK);
return;
}
ir=istack[jstack];
l=istack[jstack-1];
jstack -= 2;
} else {
k=(l+ir) >> 1;
SWAP(arr[k],arr[l+1])
SWAP(brr[k],brr[l+1])
if (arr[l] > arr[ir]) {
SWAP(arr[l],arr[ir])
SWAP(brr[l],brr[ir])
}
if (arr[l+1] > arr[ir]) {
SWAP(arr[l+1],arr[ir])
SWAP(brr[l+1],brr[ir])
}
if (arr[l] > arr[l+1]) {
SWAP(arr[l],arr[l+1])
SWAP(brr[l],brr[l+1])
}
i=l+1;
j=ir;
a=arr[l+1];
b=brr[l+1];
for (;;) {
do i++; while (arr[i] < a);
do j--; while (arr[j] > a);
if (j < i) break;
SWAP(arr[i],arr[j])
SWAP(brr[i],brr[j])
}
arr[l+1]=arr[j];
arr[j]=a;
brr[l+1]=brr[j];
brr[j]=b;
jstack += 2;
if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
}
#undef M
#undef NSTACK
#undef SWAP
#undef NRANSI
- #define NRANSI
- #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
#define M 7
#define NSTACK 50
- void sort2_0(unsigned long n, float arr[], float brr[])
- {
unsigned long i,ir=n-1,j,k,l=0;
int *istack,jstack=0;
float a,temp;
float b;
istack=new int[NSTACK];
for (;;) {
if (ir-l < M) {
for (j=l+1;j<ir;j++) {
a=arr[j];
b=brr[j];
for (i=j-1;i>=l;i--) {
if (arr[i] <= a) break;
arr[i+1]=arr[i];
brr[i+1]=brr[i];
}
arr[i+1]=a;
brr[i+1]=b;
}
if (!jstack) {
delete istack;
return;
}
ir=istack[jstack];
l=istack[jstack-1];
jstack -= 2;
} else {
k=(l+ir) >> 1;
SWAP(arr[k],arr[l+1])
SWAP(brr[k],brr[l+1])
if (arr[l] > arr[ir]) {
SWAP(arr[l],arr[ir])
SWAP(brr[l],brr[ir])
}
if (arr[l+1] > arr[ir]) {
SWAP(arr[l+1],arr[ir])
SWAP(brr[l+1],brr[ir])
}
if (arr[l] > arr[l+1]) {
SWAP(arr[l],arr[l+1])
SWAP(brr[l],brr[l+1])
}
i=l+1;
j=ir;
a=arr[l+1];
b=brr[l+1];
for (;;) {
do i++; while (arr[i] < a);
do j--; while (arr[j] > a);
if (j < i) break;
SWAP(arr[i],arr[j])
SWAP(brr[i],brr[j])
}
arr[l+1]=arr[j];
arr[j]=a;
brr[l+1]=brr[j];
brr[j]=b;
jstack += 2;
if (jstack > NSTACK) nrerror("NSTACK too small in sort2.");
if (ir-i+1 >= j-l) {
istack[jstack]=ir;
istack[jstack-1]=i;
ir=j-1;
} else {
istack[jstack]=j-1;
istack[jstack-1]=l;
l=i;
}
}
}
}
#undef M
#undef NSTACK
#undef SWAP
#undef NRANSI