GDAL

cpl_mask.h
1 /**********************************************************************
2 * $Id$
3 *
4 * Name: cpl_mask.h
5 * Project: CPL - Common Portability Library
6 * Purpose: Bitmask manipulation functions
7 * Author: Daniel Baston, dbaston@gmail.com
8 *
9 **********************************************************************
10 * Copyright (c) 2022, ISciences LLC
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 * DEALINGS IN THE SOFTWARE.
29 ****************************************************************************/
30
31 #ifndef CPL_MASK_H_INCLUDED
32 #define CPL_MASK_H_INCLUDED
33
34 #include "cpl_port.h"
35 #include "cpl_vsi.h"
36
37 #ifdef __cplusplus
38
39 #include <cstring>
40
47 inline
48 GUInt32* CPLMaskCreate(std::size_t size, bool default_value) {
49 std::size_t nBytes = (size + 31) / 8;
50 void* buf = VSI_MALLOC_VERBOSE(nBytes);
51 if (buf == nullptr) {
52 return nullptr;
53 }
54 std::memset(buf,
55 default_value ? 0xff : 0,
56 nBytes);
57 return static_cast<GUInt32*>(buf);
58 }
59
67 inline
68 bool CPLMaskGet(GUInt32* mask, std::size_t i) {
69 return mask[i >> 5] & (0x01 << (i & 0x1f));
70 }
71
78 inline
79 void CPLMaskClear(GUInt32* mask, std::size_t i) {
80 mask[i >> 5] &= ~(0x01 << (i & 0x1f));
81 }
82
89 inline
90 void CPLMaskClearAll(GUInt32* mask, std::size_t size) {
91 auto nBytes = (size + 31) / 8;
92 std::memset(mask, 0, nBytes);
93 }
94
101 inline
102 void CPLMaskSet(GUInt32* mask, std::size_t i) {
103 mask[i >> 5] |= (0x01 << (i & 0x1f));
104 }
105
112 inline
113 void CPLMaskSetAll(GUInt32* mask, std::size_t size) {
114 auto nBytes = (size + 31) / 8;
115 std::memset(mask, 0xff, nBytes);
116 }
117
125 inline
126 void CPLMaskMerge(GUInt32* mask1, GUInt32* mask2, std::size_t n) {
127 std::size_t nBytes = (n + 31) / 8;
128 std::size_t nIter = nBytes / 4;
129 for (std::size_t i = 0; i < nIter; i++) {
130 mask1[i] |= mask2[i];
131 }
132 }
133
134 #endif // __cplusplus
135
136 #endif // CPL_MASK_H
cpl_vsi.h
VSI_MALLOC_VERBOSE
#define VSI_MALLOC_VERBOSE(size)
VSI_MALLOC_VERBOSE.
Definition: cpl_vsi.h:313
cpl_port.h
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:195