重複チェック関数 使用例



[ 簡単な説明 ]

重複チェック関数の使用例です。

プログラム・ソース("test2.c")        top (先頭に戻る)
/*		test2.c		*/
#include <stdio.h>
#include <stdlib.h>
#include "sort.h"

long _idum_o1;

double ornd1(void);
int irnd(int max, int min);
int *mkdata_i(int n, int min, int max);
uchar *mkdata_c(int n, uchar min, uchar max);
double *mkdata_d(int n, double min, double max);
uchar **mkdata_s(int n, int smin, int smax, uchar min, uchar max);

int main(void)
{
	int cn, cnn, i, m, max, min, n, nn;
	int *data_i, *ip, *work;
	uchar *data_c, *cp;
	double *data_d, *dp;
	uchar **data_s, **sp;

	max = 3;
	min = 1;
	n = 120;
	cn = 80;

	_idum_o1 = 987612345L;

	data_i = mkdata_i(n, 1, 99);
	data_c = mkdata_c(cn, 'A', 'z');
	data_d = mkdata_d(n, 1.0, 10.0);
	data_s = mkdata_s(n, min, max, 'a', 'z');
	work = (int *)malloc(cn * sizeof(int));
	if(data_i == NULL || data_c == NULL || data_d == NULL || data_s == NULL ||
		work == NULL)
	{
		fprintf(stderr, "Error : out of memory\n");
		exit(-1);
	}

	for(i = 0, dp = data_d; i < n; i++)
		*dp++ = (double)((int)(*dp * 10.)) / 10.;

	for(i = 0, cp = data_c, ip = work; i < cn; i++)	*ip++ = *cp++;

	qsort_i(data_i, n);
	qsort_i(work, cn);
	qsort_d(data_d, n);
	qsort_c(data_s, n);

	for(i = 0, cp = data_c, ip = work; i < cn; i++)	*cp++ = *ip++;
	free((uchar *)work);

	printf("\n*** int 型配列データ ***\n");
	printf("*** 重複データ非削除 ***\n");
	nn = dbldel_i(data_i, n, 0);
	printf("nn = %d, 重複数=%d\n", nn, n - nn);
	for(i = 0, ip = data_i; i < n; i++)
	{
		printf("%3d:%3d ", i, *ip++);
		if(i % 10 == 9)	putchar('\n');
	}
	putchar('\n');

	printf("*** 重複データ削除 ***\n");
	nn = dbldel_i(data_i, n, 1);
	printf("nn = %d, 重複数=%d\n", nn, n - nn);
	for(i = 0, ip = data_i; i < n; i++)
	{
		printf("%3d:%3d ", i, *ip++);
		if(i % 10 == 9)	putchar('\n');
	}
	putchar('\n');

	printf("\n*** uchar 型配列データ ***\n");
	printf("*** 重複データ非削除 ***\n");
	cnn = dbldel(data_c, cn, 0);
	printf("cnn = %d, 重複数=%d\n", cnn, cn - cnn);
	for(i = 0, cp = data_c; i < cn; i++)
	{
		printf("%2d:%c ", i, *cp++);
		if(i % 16 == 15)	putchar('\n');
	}
	putchar('\n');

	printf("*** 重複データ削除 ***\n");
	cnn = dbldel(data_c, cn, 1);
	printf("cnn = %d, 重複数=%d\n", cnn, cn - cnn);
	for(i = 0, cp = data_c; i < cn; i++)
	{
		printf("%2d:%c ", i, *cp++);
		if(i % 16 == 15)	putchar('\n');
	}
	putchar('\n');

	printf("\n*** double 型配列データ ***\n");
	printf("*** 重複データ非削除 ***\n");
	nn = dbldel_d(data_d, n, 0);
	printf("nn = %d, 重複数=%d\n", nn, n - nn);
	for(i = 0, dp = data_d; i < n; i++)
	{
		printf("%3d:%4.1f  ", i, *dp++);
		if(i % 8 == 7)	putchar('\n');
	}
	putchar('\n');

	printf("*** 重複データ削除 ***\n");
	nn = dbldel_d(data_d, n, 1);
	printf("nn = %d, 重複数=%d\n", nn, n - nn);
	for(i = 0, dp = data_d; i < n; i++)
	{
		printf("%3d:%4.1f  ", i, *dp++);
		if(i % 8 == 7)	putchar('\n');
	}
	putchar('\n');

	printf("\n*** 文字列配列データ ***\n");
	printf("*** 重複データ非削除 ***\n");
	nn = dbldel_c(data_s, n, 0);
	printf("nn = %d, 重複数=%d\n", nn, n - nn);
	for(i = 0, sp = data_s; i < n; i++)
	{
		printf("%3d:%3s ", i, *sp++);
		if(i % 10 == 9)	putchar('\n');
	}
	putchar('\n');

	printf("*** 重複データ削除 ***\n");
	nn = dbldel_c(data_s, n, 1);
	printf("nn = %d, 重複数=%d\n", nn, n - nn);
	for(i = 0, sp = data_s; i < n; i++)
	{
		printf("%3d:%3s ", i, *sp++);
		if(i % 10 == 9)	putchar('\n');
	}
	putchar('\n');
	return 1;
}

int *mkdata_i(int n, int min, int max)
{
	int i, *data, *p;

	if(n < 1 || max < min)
	{
		fprintf(stderr, "Error : illegal input  in mkdata_i()\n");
		fprintf(stderr, "    n=%d, min=%d, max=%d\n", n, min, max);
		return NULL;
	}
	data = (int *)malloc(n * sizeof(int));
	if(data == NULL)
	{
		fprintf(stderr, "Error : out of memory  in mkdata_i()\n");
		return NULL;
	}
	for(i = 0, p = data; i < n; i++)	*p++ = irnd(max, min);
	return data;
}

uchar *mkdata_c(int n, uchar min, uchar max)
{
	int i;
	uchar *data, *p;

	if(n < 1 || max < min)
	{
		fprintf(stderr, "Error : illegal input  in mkdata_c()\n");
		fprintf(stderr, "    n=%d, min=%u, max=%u\n", n, min, max);
		return NULL;
	}
	data = (uchar *)malloc((n + 1) * sizeof(uchar));
	if(data == NULL)
	{
		fprintf(stderr, "Error : out of memory  in mkdata_c()\n");
		return NULL;
	}
	for(i = 0, p = data; i < n; i++)	*p++ = (uchar)irnd(max, min);
	*p = '\0';
	return data;
}

double *mkdata_d(int n, double min, double max)
{
	int i;
	double *data, *p;

	if(n < 1 || max < min)
	{
		fprintf(stderr, "Error : illegal input  in mkdata_d()\n");
		fprintf(stderr, "    n=%d, min=%e, max=%e\n", n, min, max);
		return NULL;
	}
	data = (double *)malloc(n * sizeof(double));
	if(data == NULL)
	{
		fprintf(stderr, "Error : out of memory  in mkdata_d()\n");
		return NULL;
	}
	for(i = 0, p = data; i < n; i++)	*p++ = min + (max - min) * ornd1();
	return data;
}

uchar **mkdata_s(int n, int smin, int smax, uchar min, uchar max)
{
	int i, *len, *lp;
	uchar **data, **p, *pp;

	if(n < 1 || max < min || smax < smin || smin < 1)
	{
		fprintf(stderr, "Error : illegal input  in mkdata_s()\n");
		fprintf(stderr, "    n=%d, smin=%d, smax=%d, min=%u, max=%u\n",
			n, smin, smax, min, max);
		return NULL;
	}
	data = (uchar **)malloc(n * sizeof(uchar *));
	len = mkdata_i(n, smin, smax); 
	if(data == NULL || len == NULL)
	{
		fprintf(stderr, "Error : out of memory  in mkdata_s()\n");
		return NULL;
	}
	for(i = 0, p = data, lp = len; i < n; i++, lp++)
	{
		pp = *p++ = (uchar *)malloc((smax + 1) * sizeof(uchar *));
		while((*lp)--)	*pp++ = irnd(max, min);
		*pp = '\0';
	}
	free((uchar *)len);
	return data;
}

double ornd1(void)					/* Park and Miller の「最低基準」乱数 */
{
	long w;
	double d = 1. / 2147483647.;

	w = _idum_o1 / 44488L;
	if((_idum_o1 = (_idum_o1 - w * 44488L) * 48271L - w * 3399) <= 0)
		_idum_o1 += 2147483647L;
	return (double)_idum_o1 * d;
}

int irnd(int max, int min)
{
	return min + (int)(ornd1() * (double)(max - min + 1));
}


出力結果           top (先頭に戻る)

*** int 型配列データ ***
*** 重複データ非削除 ***
nn = 63, 重複数=57
  0:  1   1:  1   2:  1   3:  4   4:  5   5:  5   6:  7   7: 13   8: 14   9: 16 
 10: 16  11: 17  12: 18  13: 18  14: 18  15: 19  16: 19  17: 19  18: 21  19: 22 
 20: 22  21: 22  22: 22  23: 23  24: 23  25: 26  26: 28  27: 28  28: 29  29: 29 
 30: 33  31: 33  32: 36  33: 37  34: 38  35: 38  36: 38  37: 39  38: 39  39: 41 
 40: 45  41: 45  42: 45  43: 46  44: 46  45: 49  46: 49  47: 49  48: 50  49: 50 
 50: 50  51: 50  52: 51  53: 52  54: 53  55: 56  56: 57  57: 57  58: 57  59: 57 
 60: 57  61: 57  62: 57  63: 59  64: 59  65: 60  66: 60  67: 61  68: 61  69: 62 
 70: 64  71: 64  72: 65  73: 65  74: 67  75: 67  76: 68  77: 68  78: 70  79: 71 
 80: 71  81: 72  82: 73  83: 73  84: 74  85: 75  86: 75  87: 75  88: 76  89: 76 
 90: 77  91: 77  92: 77  93: 79  94: 79  95: 79  96: 79  97: 81  98: 82  99: 83 
100: 83 101: 84 102: 84 103: 84 104: 85 105: 87 106: 88 107: 89 108: 89 109: 89 
110: 90 111: 90 112: 91 113: 92 114: 93 115: 95 116: 97 117: 97 118: 98 119: 98 

*** 重複データ削除 ***
nn = 63, 重複数=57
  0:  1   1:  4   2:  5   3:  7   4: 13   5: 14   6: 16   7: 17   8: 18   9: 19 
 10: 21  11: 22  12: 23  13: 26  14: 28  15: 29  16: 33  17: 36  18: 37  19: 38 
 20: 39  21: 41  22: 45  23: 46  24: 49  25: 50  26: 51  27: 52  28: 53  29: 56 
 30: 57  31: 59  32: 60  33: 61  34: 62  35: 64  36: 65  37: 67  38: 68  39: 70 
 40: 71  41: 72  42: 73  43: 74  44: 75  45: 76  46: 77  47: 79  48: 81  49: 82 
 50: 83  51: 84  52: 85  53: 87  54: 88  55: 89  56: 90  57: 91  58: 92  59: 93 
 60: 95  61: 97  62: 98  63:  0  64:  0  65:  0  66:  0  67:  0  68:  0  69:  0 
 70:  0  71:  0  72:  0  73:  0  74:  0  75:  0  76:  0  77:  0  78:  0  79:  0 
 80:  0  81:  0  82:  0  83:  0  84:  0  85:  0  86:  0  87:  0  88:  0  89:  0 
 90:  0  91:  0  92:  0  93:  0  94:  0  95:  0  96:  0  97:  0  98:  0  99:  0 
100:  0 101:  0 102:  0 103:  0 104:  0 105:  0 106:  0 107:  0 108:  0 109:  0 
110:  0 111:  0 112:  0 113:  0 114:  0 115:  0 116:  0 117:  0 118:  0 119:  0 


*** uchar 型配列データ ***
*** 重複データ非削除 ***
cnn = 40, 重複数=40
 0:A  1:B  2:B  3:C  4:C  5:D  6:D  7:D  8:E  9:E 10:F 11:F 12:F 13:H 14:I 15:J 
16:J 17:K 18:K 19:K 20:M 21:M 22:M 23:N 24:N 25:O 26:Q 27:T 28:T 29:W 30:X 31:Y 
32:Y 33:Z 34:[ 35:[ 36:] 37:] 38:^ 39:` 40:` 41:b 42:b 43:b 44:c 45:d 46:f 47:g 
48:g 49:g 50:g 51:h 52:j 53:j 54:j 55:j 56:j 57:l 58:l 59:l 60:l 61:l 62:n 63:n 
64:o 65:q 66:q 67:r 68:r 69:r 70:s 71:t 72:t 73:u 74:u 75:u 76:x 77:x 78:y 79:y 

*** 重複データ削除 ***
cnn = 40, 重複数=40
 0:A  1:B  2:C  3:D  4:E  5:F  6:H  7:I  8:J  9:K 10:M 11:N 12:O 13:Q 14:T 15:W 
16:X 17:Y 18:Z 19:[ 20:] 21:^ 22:` 23:b 24:c 25:d 26:f 27:g 28:h 29:j 30:l 31:n 
32:o 33:q 34:r 35:s 36:t 37:u 38:x 39:y 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:  


*** double 型配列データ ***
*** 重複データ非削除 ***
nn = 64, 重複数=56
  0: 1.0    1: 1.0    2: 1.1    3: 1.1    4: 1.1    5: 1.1    6: 1.2    7: 2.2  
  8: 2.2    9: 2.2   10: 2.3   11: 2.3   12: 2.4   13: 2.4   14: 2.5   15: 2.6  
 16: 2.6   17: 2.6   18: 2.7   19: 2.7   20: 2.7   21: 2.9   22: 3.0   23: 3.1  
 24: 3.1   25: 3.3   26: 3.3   27: 3.3   28: 3.3   29: 3.4   30: 3.5   31: 3.6  
 32: 3.6   33: 3.7   34: 3.8   35: 3.9   36: 4.1   37: 4.1   38: 4.2   39: 4.2  
 40: 4.2   41: 4.3   42: 4.3   43: 4.4   44: 4.4   45: 4.5   46: 4.5   47: 4.5  
 48: 4.5   49: 4.5   50: 4.5   51: 4.6   52: 4.6   53: 4.6   54: 4.6   55: 4.6  
 56: 4.8   57: 4.9   58: 4.9   59: 4.9   60: 4.9   61: 5.1   62: 5.2   63: 5.2  
 64: 5.2   65: 5.2   66: 5.3   67: 5.5   68: 5.7   69: 5.8   70: 5.8   71: 5.9  
 72: 5.9   73: 5.9   74: 6.1   75: 6.2   76: 6.4   77: 6.6   78: 6.6   79: 6.7  
 80: 6.9   81: 6.9   82: 6.9   83: 6.9   84: 6.9   85: 7.0   86: 7.0   87: 7.1  
 88: 7.2   89: 7.3   90: 7.4   91: 7.5   92: 7.6   93: 7.8   94: 7.9   95: 8.0  
 96: 8.0   97: 8.0   98: 8.0   99: 8.1  100: 8.1  101: 8.2  102: 8.2  103: 8.3  
104: 8.4  105: 8.5  106: 8.5  107: 8.6  108: 8.6  109: 8.7  110: 9.0  111: 9.0  
112: 9.1  113: 9.1  114: 9.1  115: 9.2  116: 9.3  117: 9.5  118: 9.6  119: 9.8  

*** 重複データ削除 ***
nn = 64, 重複数=56
  0: 1.0    1: 1.1    2: 1.2    3: 2.2    4: 2.3    5: 2.4    6: 2.5    7: 2.6  
  8: 2.7    9: 2.9   10: 3.0   11: 3.1   12: 3.3   13: 3.4   14: 3.5   15: 3.6  
 16: 3.7   17: 3.8   18: 3.9   19: 4.1   20: 4.2   21: 4.3   22: 4.4   23: 4.5  
 24: 4.6   25: 4.8   26: 4.9   27: 5.1   28: 5.2   29: 5.3   30: 5.5   31: 5.7  
 32: 5.8   33: 5.9   34: 6.1   35: 6.2   36: 6.4   37: 6.6   38: 6.7   39: 6.9  
 40: 7.0   41: 7.1   42: 7.2   43: 7.3   44: 7.4   45: 7.5   46: 7.6   47: 7.8  
 48: 7.9   49: 8.0   50: 8.1   51: 8.2   52: 8.3   53: 8.4   54: 8.5   55: 8.6  
 56: 8.7   57: 9.0   58: 9.1   59: 9.2   60: 9.3   61: 9.5   62: 9.6   63: 9.8  
 64: 0.0   65: 0.0   66: 0.0   67: 0.0   68: 0.0   69: 0.0   70: 0.0   71: 0.0  
 72: 0.0   73: 0.0   74: 0.0   75: 0.0   76: 0.0   77: 0.0   78: 0.0   79: 0.0  
 80: 0.0   81: 0.0   82: 0.0   83: 0.0   84: 0.0   85: 0.0   86: 0.0   87: 0.0  
 88: 0.0   89: 0.0   90: 0.0   91: 0.0   92: 0.0   93: 0.0   94: 0.0   95: 0.0  
 96: 0.0   97: 0.0   98: 0.0   99: 0.0  100: 0.0  101: 0.0  102: 0.0  103: 0.0  
104: 0.0  105: 0.0  106: 0.0  107: 0.0  108: 0.0  109: 0.0  110: 0.0  111: 0.0  
112: 0.0  113: 0.0  114: 0.0  115: 0.0  116: 0.0  117: 0.0  118: 0.0  119: 0.0  


*** 文字列配列データ ***
*** 重複データ非削除 ***
nn = 97, 重複数=23
  0: ae   1: aw   2: ay   3:azs   4:  b   5:  c   6:  c   7: ce   8: cp   9: da 
 10: db  11: di  12:diq  13:  e  14:  e  15:  e  16:efn  17:eik  18: ep  19: et 
 20:fdm  21:fgf  22: ft  23:fzw  24:  g  25:gkb  26:gxz  27:gyh  28:  h  29: hl 
 30:ibo  31:ixi  32:  j  33:  j  34:  j  35:jiy  36:joy  37:  k  38:klb  39: ku 
 40: kv  41:kwq  42:  l  43:  l  44:  l  45: lh  46:lrn  47:ltx  48: mh  49: mm 
 50: mo  51:mzm  52:  n  53:  n  54:  n  55: na  56:naz  57: nh  58:nwz  59:  o 
 60:obq  61: oe  62:opz  63:osk  64: pb  65: pv  66: pv  67:  q  68:  q  69: qz 
 70:  r  71:  r  72:rlp  73: rv  74: ry  75:sgt  76:ssc  77:  t  78:  t  79:  t 
 80:tbu  81:tim  82:tlh  83: ty  84:  u  85:  u  86:  u  87:  u  88:  u  89:ueq 
 90:uxd  91:uxm  92: vd  93:vfy  94: vk  95:vkn  96: vm  97: vn  98: vo  99: vx 
100:  w 101:  w 102:  w 103:  w 104: wg 105: wv 106:  x 107:  x 108:xeh 109: xn 
110:  y 111: yc 112:yfi 113: yr 114:  z 115:  z 116:zgi 117:zrc 118:zrw 119: zv 

*** 重複データ削除 ***
nn = 97, 重複数=23
  0: ae   1: aw   2: ay   3:azs   4:  b   5:  c   6:      7: ce   8: cp   9: da 
 10: db  11: di  12:diq  13:  e  14:     15:     16:efn  17:eik  18: ep  19: et 
 20:fdm  21:fgf  22: ft  23:fzw  24:  g  25:gkb  26:gxz  27:gyh  28:  h  29: hl 
 30:ibo  31:ixi  32:  j  33:     34:     35:jiy  36:joy  37:  k  38:klb  39: ku 
 40: kv  41:kwq  42:  l  43:     44:     45: lh  46:lrn  47:ltx  48: mh  49: mm 
 50: mo  51:mzm  52:  n  53:     54:     55: na  56:naz  57: nh  58:nwz  59:  o 
 60:obq  61: oe  62:opz  63:osk  64: pb  65: pv  66:     67:  q  68:     69: qz 
 70:  r  71:     72:rlp  73: rv  74: ry  75:sgt  76:ssc  77:  t  78:     79:    
 80:tbu  81:tim  82:tlh  83: ty  84:  u  85:     86:     87:     88:     89:ueq 
 90:uxd  91:uxm  92: vd  93:vfy  94: vk  95:vkn  96: vm  97: vn  98: vo  99: vx 
100:  w 101:    102:    103:    104: wg 105: wv 106:  x 107:    108:xeh 109: xn 
110:  y 111: yc 112:yfi 113: yr 114:  z 115:    116:zgi 117:zrc 118:zrw 119: zv