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
47inline GUInt32 *CPLMaskCreate(std::size_t size, bool default_value)
48{
49 std::size_t nBytes = (size + 31) / 8;
50 void *buf = VSI_MALLOC_VERBOSE(nBytes);
51 if (buf == nullptr)
52 {
53 return nullptr;
54 }
55 std::memset(buf, default_value ? 0xff : 0, nBytes);
56 return static_cast<GUInt32 *>(buf);
57}
58
66inline bool CPLMaskGet(GUInt32 *mask, std::size_t i)
67{
68 return mask[i >> 5] & (0x01 << (i & 0x1f));
69}
70
77inline void CPLMaskClear(GUInt32 *mask, std::size_t i)
78{
79 mask[i >> 5] &= ~(0x01 << (i & 0x1f));
80}
81
88inline void CPLMaskClearAll(GUInt32 *mask, std::size_t size)
89{
90 auto nBytes = (size + 31) / 8;
91 std::memset(mask, 0, nBytes);
92}
93
100inline void CPLMaskSet(GUInt32 *mask, std::size_t i)
101{
102 mask[i >> 5] |= (0x01 << (i & 0x1f));
103}
104
111inline void CPLMaskSetAll(GUInt32 *mask, std::size_t size)
112{
113 auto nBytes = (size + 31) / 8;
114 std::memset(mask, 0xff, nBytes);
115}
116
124inline void CPLMaskMerge(GUInt32 *mask1, GUInt32 *mask2, std::size_t n)
125{
126 std::size_t nBytes = (n + 31) / 8;
127 std::size_t nIter = nBytes / 4;
128 for (std::size_t i = 0; i < nIter; i++)
129 {
130 mask1[i] |= mask2[i];
131 }
132}
133
134#endif // __cplusplus
135
136#endif // CPL_MASK_H
cpl_port.h
Core portability definitions for CPL.
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:177
cpl_vsi.h
Standard C Covers.
VSI_MALLOC_VERBOSE
#define VSI_MALLOC_VERBOSE(size)
VSI_MALLOC_VERBOSE.
Definition: cpl_vsi.h:346