2 This file is part of the software library CADLIB written by Conrad Ziesler
3 Copyright 2003, Conrad Ziesler, all rights reserved.
5 *************************
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* mergedup.c -> code to find and merge common entities,
22 works in nlogn time, for general entities
33 mergedup_t
*mergedup_alloc(int q
, int qs
)
36 int es
=sizeof(sortlist_t
)+qs
-1;
38 m
=malloc(sizeof(mergedup_t
) + (es
* q
) + ((q
>>3)+2));
44 m
->run
=(qs
-(m
->qun
*4));
47 m
->data
= (void *) (((char *)m
)+sizeof(mergedup_t
)) ;
48 m
->freelist
= (void *) (((char *)(m
->data
))+ (es
*q
) );
49 m
->last
=(void *)(m
->data
);
50 memset(m
->freelist
,0,m
->flb
);
55 void mergedup_fill(mergedup_t
*m
, unsigned char *d
, int index
)
59 p
= (sortlist_t
*) ( (char *)m
->data
+ (m
->es
*m
->i
) );
68 static int mergedup__qs
=0;
70 static int comparesorted(const void *a
, const void *b
)
72 unsigned char *pa
=((sortlist_t
*)a
)->sorted
;
73 unsigned char *pb
=((sortlist_t
*)b
)->sorted
;
78 if(pb
==NULL
)return -1;
80 for(i
=0;i
<mergedup__qs
;i
++)
82 if (pa
[i
]<pb
[i
]) return 1;
83 else if (pa
[i
]>pb
[i
]) return -1;
89 void mergedup_sort(mergedup_t
*m
)
92 comparesorted(NULL
,NULL
);
93 qsort(m
->data
,m
->q
, m
->es
, comparesorted
);
98 void mergedup_setbit(mergedup_t
*m
, int index
)
104 m
->freelist
[byte
]|=(1<<bit
);
107 int mergedup_testbit(mergedup_t
*m
, int index
)
113 if((m
->freelist
[byte
]&(1<<bit
))!=0)return 1;
117 int mergedup_visit(mergedup_t
*m
, int w
)
123 if(m
->i
>=m
->q
)return -1;
125 /* get unsorted of current data[m->i] */
126 p
= (sortlist_t
*) ( (char *)m
->data
+ (m
->es
*m
->i
) );
129 if(w
) /* w=1, get next item */
135 else /* w=0, return next same item */
137 if(m
->last
==NULL
)return -1;
139 if(p
->sorted
[i
]!=m
->last
->sorted
[i
])return -1;
149 void mergedup_free(mergedup_t
*m
)