OpenASIP 2.2
Loading...
Searching...
No Matches
BitTrie.hh
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2016 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/*
25 * @file BitTrie.hh
26 *
27 * Implementation/Declaration of BitTrie class.
28 *
29 * Created on: 28.1.2016
30 * @author Henry Linjamäki 2016 (henry.linjamaki-no.spam-tut.fi)
31 * @note rating: red
32 */
33
34#ifndef BITTRIE_HH
35#define BITTRIE_HH
36
37#include <tuple>
38#include <iostream>
39
40#include "Exception.hh"
41
42/*
43 * Binary trie class.
44 *
45 * Bit patterns are inserted into the trie using BitVector, which is tuple
46 * of (binary value, bit width). The value is right aligned in the bit vector.
47 * For example (5, 6) is as bit vector 0b000101.
48 *
49 * When default constructed BitTrie or with BitTrie(false), the inserted bit
50 * pattern are stored LSB first. That is, when bit patterns 0b110101 and
51 * 0b111101 is added to the trie, the common prefix of the patterns is 0b101.
52 *
53 * When constructed with BitTrie(true), the inserted bit pattern are stored
54 * MSB first (or in reserve order in contrast to default constructed BitTrie).
55 * That is, when bit patterns 0b110101 and 0b111101 is added to the trie, the
56 * common prefix of the patterns is 0b11.
57 */
58template<class DataType, class ValueType = long long unsigned int>
59class BitTrie {
60public:
62 BitTrie(bool bitReadLeftToRight);
63 virtual ~BitTrie();
64
65 /// The type for width of bit vector.
66 using WidthType = unsigned;
67 /// The bit vector type used to store patterns to tries.
68 /// Binary value is queried by value() method.
69 /// Width value is queried by width() method.
70 using BitVector = std::tuple<ValueType, WidthType>;
71 /// The type for frequencies of the patterns.
72 using Frequency = unsigned int;
73
74 unsigned frequency(const BitVector& prefixBits) const;
75 bool check(const BitVector& exactbits) const;
76 bool insert(BitVector bits);
77 bool insert(BitVector bits, const DataType& data);
78 bool erase(BitVector bits);
79 void clear();
80 unsigned size() const;
82 DataType& at(BitVector bits);
83
85
86 void dump(std::ostream& out) const;
87
88 static WidthType& width(BitVector& bitVector);
89 static const WidthType& width(const BitVector& bitVector);
90 static ValueType& value(BitVector& bitVector);
91 static const ValueType& value(const BitVector& bitVector);
92private:
93
94 BitTrie(bool bitReadLeftToRight, BitTrie& parent);
95 bool nextBit(const BitVector& bitVector) const;
96 BitVector stripBit(BitVector bitVector) const;
97 BitTrie* findNode(const BitVector& bits);
98 const BitTrie* findNode(const BitVector& bits) const;
100 static BitVector uniquePrefixImpl(const BitTrie& bitTrie);
101 static bool nodeTerminatesPattern(const BitTrie& node);
102 static BitVector patternAtNode(const BitTrie& node);
103 static bool nodeBitValue(const BitTrie& node);
104 static bool isRootNode(const BitTrie& node);
105 static unsigned depth(const BitTrie& node);
106
107 /// The configuration how bits are read and stored into the trie.
108 /// If true, the rightmost (LSB) bit is placed in the leaf node.
109 /// If false, the leftmost (MSB) bit is placed in the leaf node.
111 /// The bits are encoded as indexes where 0: zero node, 1: one node.
112 /// If node at index is not nullptr, a bit has been stored in the trie.
113 BitTrie* node_[2] = {nullptr, nullptr};
114 /// Parent of the (sub)trie. If the pointer is null, the node is root of
115 /// the trie.
116 BitTrie* parent_ = nullptr;
117 /// The frequency. The current occurrence of the bit pattern so far.
119 /// The data at this node.
120 DataType* data_ = nullptr;
121
122};
123
124#include "BitTrie.icc"
125
126#endif /* BITTRIE_HH */
bool nextBit(const BitVector &bitVector) const
unsigned size() const
unsigned WidthType
The type for width of bit vector.
Definition BitTrie.hh:66
static bool isRootNode(const BitTrie &node)
Frequency frequency_
The frequency. The current occurrence of the bit pattern so far.
Definition BitTrie.hh:118
void clear()
static ValueType & value(BitVector &bitVector)
static bool nodeBitValue(const BitTrie &node)
static const WidthType & width(const BitVector &bitVector)
BitTrie * parent_
Parent of the (sub)trie. If the pointer is null, the node is root of the trie.
Definition BitTrie.hh:116
static BitVector uniquePrefixImpl(const BitTrie &bitTrie)
bool check(const BitVector &exactbits) const
BitVector longestCompletePattern(const BitVector &bits) const
void dump(std::ostream &out) const
unsigned frequency(const BitVector &prefixBits) const
BitTrie(bool bitReadLeftToRight)
virtual ~BitTrie()
BitTrie * findNode(const BitVector &bits)
BitTrie * node_[2]
The bits are encoded as indexes where 0: zero node, 1: one node. If node at index is not nullptr,...
Definition BitTrie.hh:113
static bool nodeTerminatesPattern(const BitTrie &node)
static BitVector patternAtNode(const BitTrie &node)
static WidthType & width(BitVector &bitVector)
DataType & at(BitVector bits)
bool insert(BitVector bits, const DataType &data)
DataType * data_
The data at this node.
Definition BitTrie.hh:120
const BitTrie * findNode(const BitVector &bits) const
static const ValueType & value(const BitVector &bitVector)
static unsigned depth(const BitTrie &node)
bool bitReadLeftToRight_
The configuration how bits are read and stored into the trie. If true, the rightmost (LSB) bit is pla...
Definition BitTrie.hh:110
BitVector uniqueUnusedPrefix() const
bool insert(BitVector bits)
bool erase(BitVector bits)
BitVector stripBit(BitVector bitVector) const
BitTrie(bool bitReadLeftToRight, BitTrie &parent)
static BitVector uniqueUnusedPrefixImpl(const BitTrie &bitTrie)
unsigned int Frequency
The type for frequencies of the patterns.
Definition BitTrie.hh:72