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 /* bitlist.c, routines for bit arrays
33 bitlist_t
* bitlist_new(int qty
)
39 bl
= malloc(sizeof(bitlist_t
)+qbytes
);
41 memset(bl
,0,sizeof(bitlist_t
)+qbytes
);
48 void bitlist_free(bitlist_t
*bl
)
54 void bitlist_setall(bitlist_t
*bl
)
56 memset(bl
->data
,0xff,bl
->qbytes
);
59 void bitlist_clearall(bitlist_t
*bl
)
61 memset(bl
->data
,0,bl
->qbytes
);
65 int bitlist_set(bitlist_t
*bl
, int index
)
71 assert(byte
<bl
->qbytes
);
72 if((bl
->data
[byte
]&(1<<bit
))!=0)r
=1;
73 bl
->data
[byte
]|=(1<<bit
);
77 int bitlist_test(bitlist_t
*bl
, int index
)
83 assert(byte
<bl
->qbytes
);
84 if((bl
->data
[byte
]&(1<<bit
))!=0)return 1;
89 int bitlist_clear(bitlist_t
*bl
, int index
)
95 assert(byte
<bl
->qbytes
);
96 if((bl
->data
[byte
]&(1<<bit
))!=0)r
=1;
97 bl
->data
[byte
]&=~(1<<bit
);
101 /* realloc bitlist */
102 bitlist_t
*bitlist_resize(bitlist_t
*bl
, int qty
)
109 bl
=realloc(bl
,sizeof(bitlist_t
)+qbytes
);
111 if( (oldqty
>>3) < (qbytes
-(oldqty
>>3)) )
112 memset(bl
->data
+(oldqty
>>3),0,qbytes
-(oldqty
>>3));
120 /* returns the index of the first bit set or cleared
124 int bitlist_scan(bitlist_t
*bl
, int flags
)
130 case (SCAN_FORWARD
|SCAN_SET
):
131 for(i
=0;i
<(bl
->qbytes
-1);i
++)
135 for(j
=(i
<<3);j
<bl
->qty
;j
++)
136 if(bitlist_test(bl
,j
))return j
;
140 case (SCAN_FORWARD
|SCAN_CLEAR
):
141 for(i
=0;i
<(bl
->qbytes
-1);i
++)
143 if( (bl
->data
[i
])< ((unsigned char)0x00ff))
146 for(j
=(i
<<3);j
<bl
->qty
;j
++)
147 if(!bitlist_test(bl
,j
))return j
;
160 int bitlist_copyhead(bitlist_t
*dest
, bitlist_t
*src
, int length
)
166 if(length
>src
->qty
)length
=src
->qty
;
167 if(length
>dest
->qty
)length
=dest
->qty
;
170 finalbitoffset
=length
-(finalbyte
<<3);
172 for(i
=0;i
<finalbyte
;i
++)
173 dest
->data
[i
]=src
->data
[i
];
175 a
=dest
->data
[finalbyte
];
176 b
=src
->data
[finalbyte
];
178 for(i
=0;i
<finalbitoffset
;i
++)
179 a
= (a
& (~(1<<i
)) ) | ((1<<i
)&b
) ;
181 dest
->data
[finalbyte
]=a
;