AlexanderSaydakov commented on code in PR #438: URL: https://github.com/apache/datasketches-cpp/pull/438#discussion_r1717634021
########## filters/include/bit_array_ops.hpp: ########## @@ -0,0 +1,180 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _BIT_ARRAY_OPS_HPP_ +#define _BIT_ARRAY_OPS_HPP_ + +#include <bitset> + +namespace datasketches { + +/** + * This class comprises methods that operate one or more arrays of bits (uint8_t*) to + * provide bit array operations. The class does not take ownership of memory and operates On + * arrays in-place. Sizes of the arrays, in bytes, are passed in as arguments. + * + * None of the methods in this class perform bounds checks. The caller is responsible for ensuring + * that indices are within the array bounds. + * + * Implementation assumes the actual arrays are multiples of 64 bits in length. + */ +namespace bit_array_ops { + + /** + * Get the value of a bit at the given index. + * @param array the array of bits + * @param index the index of the bit to get + * @return the value of the bit at the given index. + */ + static inline bool get_bit(uint8_t* array, const uint64_t index) { + return (array[index >> 3] & (1 << (index & 7))) != 0; + } + + /** + * Set the bit at the given index to 1. + * @param array the array of bits + * @param index the index of the bit to set. + */ + static inline void set_bit(uint8_t* array, const uint64_t index) { + array[index >> 3] |= (1 << (index & 7)); + } + + /** + * Set the bit at the given index to 0. + * @param array the array of bits + * @param index the index of the bit to clear. + */ + static inline void clear_bit(uint8_t* array, const uint64_t index) { + array[index >> 3] &= ~(1 << (index & 7)); + } + + /** + * Assign the value of the bit at the given index. + * @param array the array of bits + * @param index the index of the bit to set. + */ + static inline void assign_bit(uint8_t* array, const uint64_t index, const bool value) { + // read-only checks handled by set_bit() and clear_bit() + if (value) { + set_bit(array, index); + } else { + clear_bit(array, index); + } + } + + /** + * Gets teh value of a bit at the specified index and sets it to true Review Comment: typo -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
