aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/common/allocatedbitvector.h
blob: a47082dc413c241d67f472f4d76af64532fd0a9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include "bitvector.h"

namespace search {

class GrowableBitVector;
class BitVectorTest;

/**
 * search::AllocatedBitVector provides an interface to a bit vector
 * internally implemented as an array of words.
 */
class AllocatedBitVector : public BitVector
{
public:
    /**
     * Class constructor specifying size but not content.  New bitvector
     * is cleared.
     *
     * @param numberOfElements  The size of the bit vector in bits.
     *
     */
    explicit AllocatedBitVector(Index numberOfElements);
    /**
     *
     * @param numberOfElements  The size of the bit vector in bits.
     * @param buffer            The buffer backing the bit vector.
     * @param offset            Where bitvector image is located in the buffer.
     */
    AllocatedBitVector(Index numberOfElements, Alloc buffer, size_t offset);

    /**
     * Creates a new bitvector with size of numberOfElements bits and at least a capacity of capacity.
     * Copies what it can from the original vector. This is used for extending vector.
     */
    AllocatedBitVector(Index numberOfElements, Index capacity, const void * rhsBuf, size_t rhsSize, const Alloc* init_alloc);

    AllocatedBitVector(const BitVector &other);
    AllocatedBitVector(const AllocatedBitVector &other);
    ~AllocatedBitVector() override;

    /**
     * Query the size of the bit vector.
     *
     * @return number of legal index positions (bits).
     */
    Index capacity() const { return _capacityBits; }

    Index extraByteSize() const { return _alloc.size(); }

    /**
     * Set new length of bit vector, possibly destroying content.
     *
     * @param newLength the new length of the bit vector (in bits)
     */
    void resize(Index newLength);

protected:
    Index          _capacityBits;
    Alloc          _alloc;

private:
    friend class BitVectorTest;
    friend class GrowableBitVector;

    AllocatedBitVector(const BitVector &other, std::pair<Index, Index> size_capacity);
};

} // namespace search