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 * SPDX-License-Identifier: MIT
13 ****************************************************************************/
14
15#ifndef CPL_MASK_H_INCLUDED
16#define CPL_MASK_H_INCLUDED
17
18#include "cpl_port.h"
19#include "cpl_vsi.h"
20
21#ifdef __cplusplus
22
23#include <cstring>
24
31 inline GUInt32 *CPLMaskCreate(std::size_t size, bool default_value)
32{
33 std::size_t nBytes = (size + 31) / 8;
34 void *buf = VSI_MALLOC_VERBOSE(nBytes);
35 if (buf == nullptr)
36 {
37 return nullptr;
38 }
39 std::memset(buf, default_value ? 0xff : 0, nBytes);
40 return static_cast<GUInt32 *>(buf);
41}
42
50 inline bool CPLMaskGet(GUInt32 *mask, std::size_t i)
51{
52 return mask[i >> 5] & (0x01 << (i & 0x1f));
53}
54
61 inline void CPLMaskClear(GUInt32 *mask, std::size_t i)
62{
63 mask[i >> 5] &= ~(0x01 << (i & 0x1f));
64}
65
72 inline void CPLMaskClearAll(GUInt32 *mask, std::size_t size)
73{
74 auto nBytes = (size + 31) / 8;
75 std::memset(mask, 0, nBytes);
76}
77
84 inline void CPLMaskSet(GUInt32 *mask, std::size_t i)
85{
86 mask[i >> 5] |= (0x01 << (i & 0x1f));
87}
88
95 inline void CPLMaskSetAll(GUInt32 *mask, std::size_t size)
96{
97 auto nBytes = (size + 31) / 8;
98 std::memset(mask, 0xff, nBytes);
99}
100
108 inline void CPLMaskMerge(GUInt32 *mask1, GUInt32 *mask2, std::size_t n)
109{
110 std::size_t nBytes = (n + 31) / 8;
111 std::size_t nIter = nBytes / 4;
112 for (std::size_t i = 0; i < nIter; i++)
113 {
114 mask1[i] |= mask2[i];
115 }
116}
117
118#endif // __cplusplus
119
120#endif // CPL_MASK_H
cpl_port.h
Core portability definitions for CPL.
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition cpl_port.h:161
cpl_vsi.h
Standard C Covers.
VSI_MALLOC_VERBOSE
#define VSI_MALLOC_VERBOSE(size)
VSI_MALLOC_VERBOSE.
Definition cpl_vsi.h:332