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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
/*
* KSeg
* Copyright (C) 1999-2006 Ilya Baran
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Send comments and/or bug reports to:
* ibaran@mit.edu
*/
#include "G_ref.H"
#include "G_refs.H"
#include "G_refSearcher.H"
#include "my_hash_set.H"
struct topSortState
{
topSortState(G_ref *e, int c) : element(e), child(c) {}
G_ref *element;
int child;
};
G_refs::G_refs()
{
mHoldingRemovals = false; mToBeRemoved = NULL;
}
G_refs::~G_refs()
{
ASSERT(!mHoldingRemovals);
}
void G_refs::appendUnique(G_ref *r)
{
if(findRef(r) == -1) append(r);
}
void G_refs::update(bool fromLocus)
{
//if fromLocus is true, formulas are not reparsed.
int i;
for(i = 0; i < (int)count(); i++) at(i)->update(fromLocus);
}
void G_refs::removeRef(G_ref *r)
{
if(mHoldingRemovals) mToBeRemoved->insert(r);
else remove(find(r));
}
bool G_refs::operator==(G_refs &r)
{
if(count() != r.count()) return false;
int i;
for(i = 0; i < (int)count(); i++) if(at(i) != r.at(i)) return false;
return true;
}
void G_refs::holdRemovals()
{
ASSERT(!mHoldingRemovals);
mHoldingRemovals = true;
mToBeRemoved = new hash_set<G_ref *>();
}
void G_refs::commitRemovals()
{
ASSERT(mHoldingRemovals);
int i;
int offs = 0;
for(i = 0; i < (int)count(); i++) {
(*this)[i - offs] = at(i);
if(mToBeRemoved->count(at(i))) offs++;
}
erase(end() - offs, end());
mHoldingRemovals = false;
delete mToBeRemoved;
mToBeRemoved = NULL;
}
void G_refs::topologicalSort(G_refs toBeAdded, const G_ref *target)
{
//this function creates a list such that every object comes after all its parents.
//all children of objects in toBeAdded will be inserted into the list.
//uses a hash table to keep track of what's been added.
//target is when only a certain object needs to be updated (like for loci). It's NULL by default.
int i;
clear();
hash_set<G_ref *> added;
vector<topSortState> stateStack;
AncestorSearcher *as = NULL;
if(target) as = new AncestorSearcher(target);
//loops are separate:
G_refs loops;
for(i = 0; i < (int)toBeAdded.count(); ++i) {
if(toBeAdded[i]->getType() == G_LOOP) {
loops.push_back(toBeAdded[i]);
}
}
//main loop:
for(i = 0; i < (int)toBeAdded.count(); ++i) {
if(added.count(toBeAdded[i])) continue;
added.insert(toBeAdded[i]);
stateStack.push_back(topSortState(toBeAdded[i], 0));
while(stateStack.size()) {
topSortState &state = stateStack.back();
if(state.child >= (int)state.element->getChildrenConst().count()) {
//finished!
if(state.element->getType() == G_LOOP) {
loops.appendUnique(state.element);
}
else {
append(state.element);
}
stateStack.pop_back();
continue;
}
G_ref *newRef = state.element->getChildrenConst()[state.child];
state.child++;
if(added.count(newRef)) continue;
if(target && !as->search(newRef)) continue;
added.insert(newRef);
stateStack.push_back(topSortState(newRef, 0));
}
}
reverse(begin(), end());
vector<G_ref *>::insert(end(), loops.begin(), loops.end());
if(target) delete as;
}
|