[ create a new paste ] login | about

Link: http://codepad.org/KdWNUfYK    [ raw code | output | fork ]

C, pasted on Jun 27:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
 
/* #define DEBUG */
#if defined(DEBUG)
#include "xmalloc.h"
#else
#define xmalloc(x, y) malloc(x)
#define xfree(x, y) free(x)
#define xrealloc(x, y, z) realloc(x, y)
#define xmallocdump()
#endif
#define ID_NODE  1001
#define ID_DATA1 2001
#define ID_DATA2 2002
struct node {
  void *data;
  int c;
  struct node *left, *right;
};

void *push(struct node **root, void *data, int (*cmp)(void *, void *)) {
  struct node *p;
  int c;
  if (!*root) {
    if ((p = xmalloc(sizeof(struct node), ID_NODE)) == NULL) {
      fprintf(stderr, "cannot allocate enough memory(struct node), aborted\n");
      exit(-1);
    }
    p->data = data;
    p->left = p->right = NULL;
    *root = p;
    return data;
  } 
  if ((c = (*cmp)(data, (*root)->data)) != 0) {
    if ((*cmp)(data, (*root)->data) < 0) {
      return push(&((*root)->left), data, cmp);
    } else {
      return push(&((*root)->right), data, cmp);
    }
  } else {
    return (*root)->data;
  }
}

void *pop(struct node **root) {
  struct node *p;
  void *data;
  if (*root == NULL) {
    return NULL;
  }
  if ((*root)->left) {
    data = pop(&((*root)->left));
    return data;
  }
  p = *root;
  data = p->data;
  *root = p->right;
  xfree(p, ID_NODE);
  return data;
}

struct data1 {
  unsigned short n;
  int c;
  struct node *root;
};

struct data2 {
  int i;
};

int cmp1(struct data1 *p, struct data1 *q) { return (p->n == q->n) ? 0 : (p->n > q->n) ? 1 : -1; }
int cmp2(struct data2 *p, struct data2 *q) { return 1; }

void add(struct node **root, unsigned short n, int i) {
  struct data1 *p, *q;
  struct data2 *r;
  if ((p = xmalloc(sizeof(struct data1), ID_DATA1)) != 0) {
    p->n = n;
    p->c = 1;
    p->root = 0;
    if ((r = xmalloc(sizeof(struct data2), ID_DATA2)) != 0) {
      r->i = i;
      if ((q = push(root, p, (int (*)(void *, void *))cmp1)) != p) {
        xfree(p, ID_DATA1);
        q->c++;
        push(&(q->root), r, (int (*)(void *, void *))cmp2);
      } else {
        push(&(p->root), r, (int (*)(void *, void *))cmp2);
      }
    }
  }
}

#define SEED 31415926
#define MAX 100000
#define THRESHOLD 6
int main() {
  unsigned short s[MAX];
  struct node *root;
  struct data1 *p1;
  struct data2 *p2;
  int i;

  assert(sizeof(unsigned short) == 2);
  root = 0;
  srand(SEED);
  for (i = 0; i < MAX; i++)
    s[i] = rand() & 0xffff;
  for (i = 0; i < MAX; i++) {
    if (i % (MAX / 10) == 0)
      printf("i=%d\n", i);
    add(&root, s[i], i);
  }
  while ((p1 = pop(&root)) != 0) {
    if (p1->c <= THRESHOLD) {
      while ((p2 = pop(&(p1->root))) != 0)
        xfree(p2, ID_DATA2);
    } else {
      printf("%d: ", p1->n);
      while ((p2 = pop(&(p1->root))) != 0) {
        printf("%d, ", p2->i);
        xfree(p2, ID_DATA2);
      }
      putchar('\n');
    }
    xfree(p1, ID_DATA1);
  }
  xmallocdump();
  return 0;
}
/* end */


Output:
i=0
i=10000
i=20000
i=30000
i=40000
i=50000
i=60000
i=70000
i=80000
i=90000
1551: 934, 29601, 31828, 42504, 61101, 73785, 90224, 
3350: 22786, 31524, 40476, 54669, 55489, 65414, 75623, 
3563: 2317, 7521, 12503, 33594, 36598, 48981, 96528, 
3598: 3826, 42531, 52670, 69095, 81545, 87360, 92992, 
3612: 13453, 16441, 47924, 51011, 54028, 73506, 81943, 
4935: 3929, 16104, 17282, 33319, 59787, 75483, 96030, 
5349: 13, 618, 18951, 41420, 42186, 59326, 66220, 
5527: 1053, 23519, 26841, 34589, 53260, 53879, 73962, 78096, 89436, 93253, 
7494: 23161, 41429, 59004, 67159, 76505, 84642, 95376, 
7572: 6372, 29710, 32868, 44687, 56989, 67495, 80174, 
7896: 18110, 30809, 44893, 52527, 68312, 75691, 76402, 
10139: 25886, 27039, 34861, 47820, 80954, 81116, 82263, 
11670: 1081, 19993, 43102, 54098, 58940, 63590, 98222, 
11890: 50600, 60221, 65639, 68490, 69643, 91094, 92132, 92592, 
12329: 23419, 30108, 37965, 39261, 48568, 76709, 94425, 
12715: 7126, 20286, 45041, 62904, 65663, 83878, 97631, 
13403: 1068, 41493, 42063, 44345, 76320, 98350, 99530, 
13881: 17693, 50601, 64752, 66398, 70304, 79225, 91517, 
14220: 26397, 28709, 62964, 79058, 81838, 96288, 96550, 
15936: 11851, 14002, 28989, 30774, 37114, 57748, 62060, 
17325: 887, 34764, 38031, 38430, 41279, 84146, 92035, 
17718: 7658, 31233, 51395, 53656, 74558, 85463, 91233, 
17806: 25799, 29014, 45314, 46895, 52246, 79790, 86744, 
19122: 29426, 44208, 53187, 77976, 80319, 88372, 98114, 
19388: 1393, 26296, 32572, 71936, 73224, 77463, 89927, 
20486: 1613, 18615, 19378, 38916, 47299, 75110, 99551, 
21777: 2969, 6150, 19587, 20125, 20197, 81866, 92190, 
21925: 7109, 24456, 26526, 38715, 64277, 82890, 96017, 
22293: 582, 7468, 26497, 42267, 58334, 58672, 86829, 
22768: 3230, 5058, 6469, 56970, 69170, 73880, 79989, 
22956: 6612, 9127, 21403, 54275, 62225, 73812, 96026, 
23245: 3610, 4013, 10955, 48665, 52784, 81741, 84709, 
23673: 5253, 16008, 37596, 63063, 87077, 89114, 95779, 
23684: 3449, 5601, 5835, 35419, 83331, 91727, 94699, 
26169: 8188, 46623, 47045, 48578, 58132, 70754, 95789, 
27155: 9761, 11434, 51215, 59871, 82157, 89133, 98825, 
28236: 7605, 17563, 49235, 49421, 64553, 94878, 99802, 
28596: 14070, 15678, 21791, 25455, 40624, 44661, 47928, 85338, 
29952: 528, 4906, 34271, 43927, 48306, 55967, 69520, 
30863: 2288, 33735, 40990, 53602, 59881, 65884, 69940, 82792, 
31066: 8720, 14265, 28224, 39040, 41186, 53971, 59316, 
31397: 16597, 28635, 37427, 61037, 63519, 64527, 78726, 85630, 
31905: 19261, 24991, 27683, 32960, 78450, 92409, 96028, 
32445: 27859, 37158, 55163, 74718, 91598, 92742, 99040, 
34215: 612, 31156, 42375, 51619, 67801, 70812, 90371, 
35196: 21777, 26338, 70155, 71412, 79530, 90152, 91864, 
35276: 16611, 34572, 38483, 40769, 46451, 64871, 87335, 
39334: 32411, 32454, 40174, 52996, 65247, 84944, 92517, 
40284: 7, 6939, 18141, 26756, 44540, 85764, 96434, 
40421: 31525, 51367, 56592, 60493, 66077, 79110, 86479, 
42909: 3666, 9782, 24279, 35526, 56008, 61248, 86533, 
43251: 13937, 31360, 65142, 79292, 83850, 90521, 95633, 
44983: 846, 3142, 43433, 45803, 72531, 74919, 90451, 
47354: 12163, 21010, 22936, 23626, 27979, 66719, 90086, 95047, 95891, 96172, 
48635: 12318, 21769, 24352, 29128, 35412, 72304, 74290, 
48897: 24094, 27359, 33402, 40219, 76735, 76801, 99289, 
51432: 21505, 54480, 77920, 78405, 78723, 83887, 97596, 
54123: 2466, 26410, 33185, 35716, 45998, 55602, 58831, 99683, 
55642: 39127, 42278, 43570, 70211, 86175, 91595, 97533, 
55927: 1919, 28472, 46691, 53574, 54484, 67793, 94472, 
60356: 10087, 20678, 23542, 32596, 43899, 73681, 79958, 
60638: 1540, 24086, 69140, 83336, 83543, 83849, 89385, 93525, 
61830: 498, 47029, 50572, 54543, 75209, 78341, 99968, 
61941: 21380, 26030, 31731, 38974, 49411, 53390, 86427, 92596, 
62223: 45350, 46811, 50052, 53246, 80641, 85480, 94962, 
63008: 8113, 8920, 11494, 29541, 35233, 57086, 98665, 
63382: 2503, 17532, 44988, 47127, 67842, 68165, 77791, 
64739: 261, 4360, 29689, 33938, 61293, 71543, 91493, 


Create a new paste based on this one


Comments: