GDAL

cpl_mask.h
1/**********************************************************************
2 *
3 * Name: cpl_mask.h
4 * Project: CPL - Common Portability Library
5 * Purpose: Bitmask manipulation functions
6 * Author: Daniel Baston, dbaston@gmail.com
7 *
8 **********************************************************************
9 * Copyright (c) 2022, ISciences LLC
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14#ifndef CPL_MASK_H_INCLUDED
15#define CPL_MASK_H_INCLUDED
16
17#include "cpl_port.h"
18#include "cpl_vsi.h"
19
20#ifdef __cplusplus
21
22#include <cstring>
23
30 inline GUInt32 *CPLMaskCreate(std::size_t size, bool default_value)
31{
32 std::size_t nBytes = (size + 31) / 8;
33 void *buf = VSI_MALLOC_VERBOSE(nBytes);
34 if (buf == nullptr)
35 {
36 return nullptr;
37 }
38 std::memset(buf, default_value ? 0xff : 0, nBytes);
39 return static_cast<GUInt32 *>(buf);
40}
41
49 inline bool CPLMaskGet(GUInt32 *mask, std::size_t i)
50{
51 return mask[i >> 5] & (0x01 << (i & 0x1f));
52}
53
60 inline void CPLMaskClear(GUInt32 *mask, std::size_t i)
61{
62 mask[i >> 5] &= ~(0x01 << (i & 0x1f));
63}
64
71 inline void CPLMaskClearAll(GUInt32 *mask, std::size_t size)
72{
73 auto nBytes = (size + 31) / 8;
74 std::memset(mask, 0, nBytes);
75}
76
83 inline void CPLMaskSet(GUInt32 *mask, std::size_t i)
84{
85 mask[i >> 5] |= (0x01 << (i & 0x1f));
86}
87
94 inline void CPLMaskSetAll(GUInt32 *mask, std::size_t size)
95{
96 auto nBytes = (size + 31) / 8;
97 std::memset(mask, 0xff, nBytes);
98}
99
107 inline void CPLMaskMerge(GUInt32 *mask1, GUInt32 *mask2, std::size_t n)
108{
109 std::size_t nBytes = (n + 31) / 8;
110 std::size_t nIter = nBytes / 4;
111 for (std::size_t i = 0; i < nIter; i++)
112 {
113 mask1[i] |= mask2[i];
114 }
115}
116
117#endif // __cplusplus
118
119#endif // CPL_MASK_H
cpl_port.h
Core portability definitions for CPL.
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition cpl_port.h:167
cpl_vsi.h
Standard C Covers.
VSI_MALLOC_VERBOSE
#define VSI_MALLOC_VERBOSE(size)
VSI_MALLOC_VERBOSE.
Definition cpl_vsi.h:338