Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

tools.cc

Go to the documentation of this file.
00001 /*
00002  *      HT Editor
00003  *      tools.cc
00004  *
00005  *      Copyright (C) 1999-2002 Sebastian Biallas (sb@web-productions.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "tools.h"
00022 
00023 #include "htdebug.h"
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <math.h>
00028 
00029 dword delinearize(dword d)
00030 {
00031         return d*0x8088405+1;   /* there's magic in here... */
00032 }
00033 
00034 int compare_keys_int_delinear(Object *key_a, Object *key_b)
00035 {
00036         int a = delinearize(((ht_data_uint*)key_a)->value);
00037         int b = delinearize(((ht_data_uint*)key_b)->value);
00038         if (a>b) return 1; else if (a<b) return -1;
00039         return 0;
00040 }
00041 
00042 int compare_keys_uint_delinear(Object *key_a, Object *key_b)
00043 {
00044         UINT a = delinearize(((ht_data_uint*)key_a)->value);
00045         UINT b = delinearize(((ht_data_uint*)key_b)->value);
00046         if (a>b) return 1; else if (a<b) return -1;
00047         return 0;
00048 }
00049 
00050 int *random_permutation(int max)
00051 {
00052         if (!max) return NULL;
00053         int *table= (int *)smalloc(max * sizeof(int));
00054         int i,j,k,l,m;
00055         for (i=0; i<max; i++) table[i] = i;
00056         for (i=0; i<max; i++) {
00057                 k=rand()%(max);
00058                 l=rand()%(max);
00059                 m=rand()%(max);
00060                 j=table[k];
00061                 table[k]=table[l];
00062                 table[l]=j;
00063                 j=table[i];
00064                 table[i]=table[m];
00065                 table[m]=j;
00066         }
00067         return table;
00068 }
00069 
00070 /*
00071  *      entropy shit
00072  */
00073 #define LN2 0.693147180559945309417232121458177
00074 
00075 double calc_entropy(byte *buf, int size)
00076 {
00077         int p[256];
00078         if (!size) return 0.0;
00079         memset(p, 0, sizeof p);
00080         for (int i=0; i<size; i++) {
00081                 p[*buf]++;
00082                 buf++;
00083         }
00084         double result = 0.0;
00085         for (int i=0; i<256; i++) {
00086                 if (p[i]) {
00087                         double pi = p[i];
00088                         pi /= size;
00089                         result += pi * log(pi) / LN2;
00090                 }
00091         }
00092         return -result;
00093 }
00094 
00095 /*
00096  *      buffer size must be 64 bytes
00097  *      return value will be in range from 0 to 100
00098  */
00099 int calc_entropy2(byte *buf, int size)
00100 {
00101         int p[256];
00102         int result = 0;
00103         if (size<3) return 0;
00104         memset(p, 0, sizeof p);
00105         // pass1
00106         byte *b = buf;
00107         for (int i=0; i < size; i++) {
00108                 if (p[*b]++ == 0) {
00109                         result ++;
00110                 }
00111                 b++;
00112         }
00113         memset(p, 0, sizeof p);
00114         b = buf;
00115         // pass2
00116         size--;
00117         for (int i=0; i < size; i++) {
00118                 int d = b[0]-b[1];
00119                 if (d<0) d = -d;
00120                 if (p[d]++ == 0) {
00121                         result ++;
00122                 }
00123                 b++;
00124         }
00125         memset(p, 0, sizeof p);
00126         b = buf;
00127         // pass3
00128         size--;
00129         for (int i=0; i < size; i++) {
00130                 int d = b[0]-b[1];
00131                 int e = b[1]-b[2];
00132                 if (d<0) d = -d;
00133                 if (e<0) e = -e;
00134                 d = d-e;
00135                 if (d<0) d = -d;
00136                 if (p[d]++ == 0) {
00137                         result ++;
00138                 }
00139                 b++;
00140         }
00141         return (result-3)*100/(size*3+3);
00142 }
00143 
00144 /*
00145  * "out of memory" - handlers
00146  */
00147 
00148 int out_of_memory(int size)
00149 {
00150         printf("Out of memory.");
00151         exit(1);
00152         return OUT_OF_MEMORY_FAIL;
00153 }
00154 
00155 int (*out_of_memory_func)(int size)=&out_of_memory;
00156 
00157 void *smalloc(size_t size)
00158 {
00159         void *p;
00160 retry:
00161         if ((p=malloc(size))) return p;
00162         switch (out_of_memory_func(size)) {
00163                 case OUT_OF_MEMORY_IGNORE:
00164                         return NULL;
00165                 case OUT_OF_MEMORY_RETRY:
00166                         goto retry;
00167                 default:
00168                         exit(666);
00169         }
00170 }
00171 
00172 void *smalloc0(size_t size)
00173 {
00174         void *p;
00175 retry0:
00176         if ((p=malloc(size))) return memset(p, 0, size);
00177         switch (out_of_memory_func(size)) {
00178                 case OUT_OF_MEMORY_IGNORE:
00179                         return NULL;
00180                 case OUT_OF_MEMORY_RETRY:
00181                         goto retry0;
00182                 default:
00183                         exit(666);
00184         }
00185 }
00186 

Generated on Fri May 7 21:15:45 2004 by doxygen 1.3.5