|
| 1 | +/* |
| 2 | + * BracketMap.cc |
| 3 | + * |
| 4 | + * Copyright 2013 Asif Amin <asifamin@utexas.edu> |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation; either version 2 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | + * MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +#ifdef HAVE_CONFIG_H |
| 22 | +# include "config.h" |
| 23 | +#endif |
| 24 | + |
| 25 | +#include <stack> |
| 26 | +#include <set> |
| 27 | + |
| 28 | +#include "BracketMap.h" |
| 29 | + |
| 30 | + |
| 31 | +// ----------------------------------------------------------------------------- |
| 32 | + BracketMap::BracketMap() |
| 33 | +/* |
| 34 | + Constructor |
| 35 | +----------------------------------------------------------------------------- */ |
| 36 | +{ |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +// ----------------------------------------------------------------------------- |
| 42 | + BracketMap::~BracketMap() |
| 43 | +/* |
| 44 | + Destructor |
| 45 | +----------------------------------------------------------------------------- */ |
| 46 | +{ |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +// ----------------------------------------------------------------------------- |
| 52 | + void BracketMap::Update(Index index, Length length) |
| 53 | +/* |
| 54 | +
|
| 55 | +----------------------------------------------------------------------------- */ |
| 56 | +{ |
| 57 | + auto it = mBracketMap.find(index); |
| 58 | + if (it != mBracketMap.end()) { |
| 59 | + auto &bracket = it->second; |
| 60 | + GetLength(bracket) = length; |
| 61 | + } |
| 62 | + else { |
| 63 | + mBracketMap.insert( |
| 64 | + std::make_pair(index, std::make_tuple(length, 0)) |
| 65 | + ); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +// ----------------------------------------------------------------------------- |
| 72 | + void BracketMap::ComputeOrder() |
| 73 | +/* |
| 74 | +
|
| 75 | +----------------------------------------------------------------------------- */ |
| 76 | +{ |
| 77 | + std::stack<Index> orderStack; |
| 78 | + |
| 79 | + for (auto &it : mBracketMap) { |
| 80 | + |
| 81 | + const Index &startIndex = it.first; |
| 82 | + Bracket &bracket = it.second; |
| 83 | + Length length = GetLength(bracket); |
| 84 | + Index endPos = startIndex + length; |
| 85 | + |
| 86 | + if (length == UNDEFINED) { |
| 87 | + // Invalid brackets |
| 88 | + GetOrder(bracket) = UNDEFINED; |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + if (orderStack.size() == 0) { |
| 94 | + // First bracket |
| 95 | + orderStack.push(endPos); |
| 96 | + } |
| 97 | + else if (startIndex > orderStack.top()) { |
| 98 | + // not nested |
| 99 | + while(orderStack.size() > 0 and orderStack.top() < startIndex) { |
| 100 | + orderStack.pop(); |
| 101 | + } |
| 102 | + orderStack.push(endPos); |
| 103 | + } |
| 104 | + else { |
| 105 | + // nested bracket |
| 106 | + orderStack.push(endPos); |
| 107 | + } |
| 108 | + |
| 109 | + GetOrder(bracket) = orderStack.size() - 1; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +// ----------------------------------------------------------------------------- |
| 116 | + void BracketMap::Show() |
| 117 | +/* |
| 118 | +
|
| 119 | +----------------------------------------------------------------------------- */ |
| 120 | +{ g_debug("%s: Showing bracket map ...", __FUNCTION__); |
| 121 | + |
| 122 | + for (const auto it : mBracketMap) { |
| 123 | + |
| 124 | + const Index &startIndex = it.first; |
| 125 | + const Bracket &bracket = it.second; |
| 126 | + |
| 127 | + Length length = std::get<0>(bracket); |
| 128 | + Order order = std::get<1>(bracket); |
| 129 | + |
| 130 | + Index end = -1; |
| 131 | + if (length > 0) { |
| 132 | + end = startIndex + length; |
| 133 | + } |
| 134 | + |
| 135 | + g_debug( |
| 136 | + "%s: Bracket at %d, Length: %d, End: %d, Order: %d", |
| 137 | + __FUNCTION__, startIndex, length, end, order |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + g_debug("%s: ... Finished showing bracket map", __FUNCTION__); |
| 142 | +} |
| 143 | + |
0 commit comments