OpenASIP  2.0
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  */
58 template<class DataType, class ValueType = long long unsigned int>
59 class BitTrie {
60 public:
61  BitTrie();
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 
84  BitVector longestCompletePattern(const BitVector& bits) const;
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);
92 private:
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;
99  static BitVector uniqueUnusedPrefixImpl(const BitTrie& bitTrie);
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.
110  bool bitReadLeftToRight_ = false;
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 */
BitTrie::longestCompletePattern
BitVector longestCompletePattern(const BitVector &bits) const
BitTrie::clear
void clear()
BitTrie::bitReadLeftToRight_
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
Exception.hh
BitVector
Definition: BitVector.hh:44
BitTrie::stripBit
BitVector stripBit(BitVector bitVector) const
BitTrie::uniquePrefixImpl
static BitVector uniquePrefixImpl(const BitTrie &bitTrie)
BitTrie::~BitTrie
virtual ~BitTrie()
BitTrie::isRootNode
static bool isRootNode(const BitTrie &node)
BitTrie::BitTrie
BitTrie()
BitTrie::at
DataType & at(BitVector bits)
BitTrie::insert
bool insert(BitVector bits)
BitTrie::patternAtNode
static BitVector patternAtNode(const BitTrie &node)
BitTrie::uniqueUnusedPrefix
BitVector uniqueUnusedPrefix() const
BitTrie::uniqueUnusedPrefixImpl
static BitVector uniqueUnusedPrefixImpl(const BitTrie &bitTrie)
BitTrie::nextBit
bool nextBit(const BitVector &bitVector) const
BitTrie::Frequency
unsigned int Frequency
The type for frequencies of the patterns.
Definition: BitTrie.hh:72
BitTrie.icc
BitTrie::node_
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
BitTrie::nodeBitValue
static bool nodeBitValue(const BitTrie &node)
BitTrie::size
unsigned size() const
BitTrie::frequency_
Frequency frequency_
The frequency. The current occurrence of the bit pattern so far.
Definition: BitTrie.hh:118
BitTrie::value
static ValueType & value(BitVector &bitVector)
BitTrie::WidthType
unsigned WidthType
The type for width of bit vector.
Definition: BitTrie.hh:66
BitTrie::data_
DataType * data_
The data at this node.
Definition: BitTrie.hh:120
BitTrie::check
bool check(const BitVector &exactbits) const
BitTrie::depth
static unsigned depth(const BitTrie &node)
ProGe::DataType
DataType
Data types of hardware ports.
Definition: ProGeTypes.hh:46
BitTrie::parent_
BitTrie * parent_
Parent of the (sub)trie. If the pointer is null, the node is root of the trie.
Definition: BitTrie.hh:116
BitTrie::frequency
unsigned frequency(const BitVector &prefixBits) const
BitTrie
Definition: BitTrie.hh:59
BitTrie::erase
bool erase(BitVector bits)
BitTrie::dump
void dump(std::ostream &out) const
BitTrie::width
static WidthType & width(BitVector &bitVector)
BitTrie::nodeTerminatesPattern
static bool nodeTerminatesPattern(const BitTrie &node)
BitTrie::findNode
BitTrie * findNode(const BitVector &bits)