/*
 * 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.
 */

#pragma once
#include <cstdint>
#include <functional>
#include <memory>

#include "paimon/common/memory/memory_slice.h"
#include "paimon/memory/bytes.h"
#include "paimon/status.h"
#include "paimon/visibility.h"

namespace paimon {
class MemoryPool;

/// BitSet based on MemorySegment.
class PAIMON_EXPORT BitSet {
 public:
    explicit BitSet(int64_t byte_length)
        : byte_length_(byte_length), bit_size_(static_cast<uint64_t>(byte_length) << 3) {}

    Status SetMemorySegment(MemorySegment segment, int32_t offset = 0);
    void UnsetMemorySegment();

    const MemorySegment& GetMemorySegment() const {
        return segment_;
    }

    MemorySlice ToSlice() {
        return MemorySlice(segment_, offset_, byte_length_);
    }

    int32_t Offset() const {
        return offset_;
    }
    int64_t BitSize() const {
        return bit_size_;
    }
    int64_t ByteLength() const {
        return byte_length_;
    }

    Status Set(uint32_t index);
    bool Get(uint32_t index);
    void Clear();

 private:
    static constexpr int32_t BYTE_INDEX_MASK = 0x00000007;

 private:
    int64_t byte_length_;
    int64_t bit_size_;
    int32_t offset_;
    MemorySegment segment_;
};
}  // namespace paimon
