1 /******************************************************************************
2 ** Filename: bitvec.c
3 ** Purpose: Routines for manipulating bit vectors
4 ** Author: Dan Johnson
5 ** History: Thu Mar 15 10:37:27 1990, DSJ, Created.
6 **
7 ** (c) Copyright Hewlett-Packard Company, 1988.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 ******************************************************************************/
18
19 /**----------------------------------------------------------------------------
20 Include Files and Type Defines
21 ----------------------------------------------------------------------------**/
22 #include "bitvec.h"
23
24 #include <stdio.h>
25
26 #include "emalloc.h"
27 #include "freelist.h"
28 #include "tprintf.h"
29
30 /**----------------------------------------------------------------------------
31 Global Data Definitions and Declarations
32 ----------------------------------------------------------------------------**/
33 static int BitVectorCount = 0;
34
35 /**----------------------------------------------------------------------------
36 Public Code
37 ----------------------------------------------------------------------------**/
38 /*---------------------------------------------------------------------------*/
ExpandBitVector(BIT_VECTOR Vector,int NewNumBits)39 BIT_VECTOR ExpandBitVector(BIT_VECTOR Vector, int NewNumBits) {
40 /*
41 ** Parameters:
42 ** Vector bit vector to be expanded
43 ** NewNumBits new size of bit vector
44 ** Globals: none
45 ** Operation: This routine uses realloc to increase the size of
46 ** the specified bit vector.
47 ** Return: New expanded bit vector.
48 ** Exceptions: none
49 ** History: Fri Nov 16 10:11:16 1990, DSJ, Created.
50 */
51 return ((BIT_VECTOR) Erealloc(Vector,
52 sizeof(Vector[0]) * WordsInVectorOfSize(NewNumBits)));
53 } /* ExpandBitVector */
54
55
56 /*---------------------------------------------------------------------------*/
FreeBitVector(BIT_VECTOR BitVector)57 void FreeBitVector(BIT_VECTOR BitVector) {
58 /*
59 ** Parameters:
60 ** BitVector bit vector to be freed
61 ** Globals:
62 ** BitVectorCount count of number of bit vectors allocated
63 ** Operation: This routine frees a bit vector. It also decrements
64 ** the global counter that keeps track of the number of
65 ** bit vectors allocated. If BitVector is NULL, then
66 ** the count is printed to stderr.
67 ** Return: none
68 ** Exceptions: none
69 ** History: Tue Oct 23 16:46:09 1990, DSJ, Created.
70 */
71 if (BitVector) {
72 Efree(BitVector);
73 BitVectorCount--;
74 } else {
75 tprintf("%6d BITVECTOR elements in use\n", BitVectorCount);
76 }
77 } /* FreeBitVector */
78
79
80 /*hamming_distance(array1,array2,length) computes the hamming distance
81 between two bit strings */
82 /*--------------------------------------------------------------------------*/
hamming_distance(uinT32 * array1,uinT32 * array2,int length)83 int hamming_distance(uinT32* array1, uinT32* array2, int length) {
84 register uinT32 diff; /*bit difference */
85 register int dist; /*total distance */
86
87 dist = 0;
88 for (; length > 0; length--) {
89 diff = *array1++ ^ *array2++;/*different bits */
90 while (diff) {
91 diff &= diff - 1; /*lose a bit */
92 dist++;
93 }
94 }
95 return dist; /*total distance */
96 }
97
98
99 /*---------------------------------------------------------------------------*/
NewBitVector(int NumBits)100 BIT_VECTOR NewBitVector(int NumBits) {
101 /*
102 ** Parameters:
103 ** NumBits number of bits in new bit vector
104 ** Globals:
105 ** BitVectorCount number of bit vectors allocated
106 ** Operation: Allocate and return a new bit vector large enough to
107 ** hold the specified number of bits.
108 ** Return: New bit vector.
109 ** Exceptions: none
110 ** History: Tue Oct 23 16:51:27 1990, DSJ, Created.
111 */
112 BitVectorCount++;
113 return ((BIT_VECTOR) Emalloc(sizeof(uinT32) *
114 WordsInVectorOfSize(NumBits)));
115 } /* NewBitVector */
116