/src/MapServer/src/mapbits.c
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * $Id$ |
3 | | * |
4 | | * Project: MapServer |
5 | | * Purpose: Implementation of bit array functions. |
6 | | * Author: Steve Lime and the MapServer team. |
7 | | * |
8 | | * Notes: Derived from code placed in the public domain by Bob Stout, for more |
9 | | * information see http://c.snippets.org/snip_lister.php?fname=bitarray.c. |
10 | | * |
11 | | ****************************************************************************** |
12 | | * Copyright (c) 1996-2005 Regents of the University of Minnesota. |
13 | | |
14 | | * Permission is hereby granted, free of charge, to any person obtaining a |
15 | | * copy of this software and associated documentation files (the "Software"), |
16 | | * to deal in the Software without restriction, including without limitation |
17 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
18 | | * and/or sell copies of the Software, and to permit persons to whom the |
19 | | * Software is furnished to do so, subject to the following conditions: |
20 | | * |
21 | | * The above copyright notice and this permission notice shall be included in |
22 | | * all copies of this Software or works derived from this Software. |
23 | | * |
24 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
25 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
26 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
27 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
28 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
29 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
30 | | * DEALINGS IN THE SOFTWARE. |
31 | | ****************************************************************************/ |
32 | | |
33 | | #include "mapserver.h" |
34 | | |
35 | | #include <limits.h> |
36 | | |
37 | | /* |
38 | | * Hardcoded size of our bit array. |
39 | | * See function msGetNextBit for another hardcoded value. |
40 | | */ |
41 | | |
42 | | /* #define msGetBit(array, index) (*((array) + (index)/MS_ARRAY_BIT) & ( 1 << |
43 | | * ((index) % MS_ARRAY_BIT))) */ |
44 | | |
45 | 0 | size_t msGetBitArraySize(int numbits) { |
46 | 0 | return ((numbits + MS_ARRAY_BIT - 1) / MS_ARRAY_BIT); |
47 | 0 | } |
48 | | |
49 | 0 | ms_bitarray msAllocBitArray(int numbits) { |
50 | 0 | ms_bitarray array = |
51 | 0 | calloc((numbits + MS_ARRAY_BIT - 1) / MS_ARRAY_BIT, MS_ARRAY_BIT); |
52 | |
|
53 | 0 | return (array); |
54 | 0 | } |
55 | | |
56 | 0 | int msGetBit(ms_const_bitarray array, int index) { |
57 | 0 | array += index / MS_ARRAY_BIT; |
58 | 0 | return (*array & (1U << (index % MS_ARRAY_BIT))) != 0; /* 0 or 1 */ |
59 | 0 | } |
60 | | |
61 | | /* |
62 | | ** msGetNextBit( status, start, size) |
63 | | ** |
64 | | ** Quickly find the next bit set. If start == 0 and 0 is set, will return 0. |
65 | | ** If hits end of bitmap without finding set bit, will return -1. |
66 | | ** |
67 | | */ |
68 | 0 | int msGetNextBit(ms_const_bitarray array, int i, int size) { |
69 | |
|
70 | 0 | register ms_uint32 b; |
71 | |
|
72 | 0 | while (i < size) { |
73 | 0 | b = *(array + (i / MS_ARRAY_BIT)); |
74 | 0 | if (b && (b >> (i % MS_ARRAY_BIT))) { |
75 | | /* There is something in this byte */ |
76 | | /* And it is not to the right of us */ |
77 | 0 | if (b & (1U << (i % MS_ARRAY_BIT))) { |
78 | | /* There is something at this bit! */ |
79 | 0 | return i; |
80 | 0 | } else { |
81 | 0 | i++; |
82 | 0 | } |
83 | 0 | } else { |
84 | | /* Nothing in this byte, move to start of next byte */ |
85 | 0 | i += MS_ARRAY_BIT - (i % MS_ARRAY_BIT); |
86 | 0 | } |
87 | 0 | } |
88 | | |
89 | | /* Got to the last byte with no hits! */ |
90 | 0 | return -1; |
91 | 0 | } |
92 | | |
93 | 0 | void msSetBit(ms_bitarray array, int index, int value) { |
94 | 0 | array += index / MS_ARRAY_BIT; |
95 | 0 | if (value) |
96 | 0 | *array |= 1U << (index % MS_ARRAY_BIT); /* set bit */ |
97 | 0 | else |
98 | 0 | *array &= ~(1U << (index % MS_ARRAY_BIT)); /* clear bit */ |
99 | 0 | } |
100 | | |
101 | 0 | void msSetAllBits(ms_bitarray array, int numbits, int value) { |
102 | 0 | if (value) |
103 | 0 | memset(array, 0xff, ((numbits + 7) / 8)); /* set bit */ |
104 | 0 | else |
105 | 0 | memset(array, 0x0, ((numbits + 7) / 8)); /* clear bit */ |
106 | 0 | } |
107 | | |
108 | 0 | void msFlipBit(ms_bitarray array, int index) { |
109 | 0 | array += index / MS_ARRAY_BIT; |
110 | 0 | *array ^= 1U << (index % MS_ARRAY_BIT); /* flip bit */ |
111 | 0 | } |