00001 /************************************************************************** 00002 *** 00003 *** Copyright (c) 1995-2000 Regents of the University of California, 00004 *** Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov 00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan, 00006 *** Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov 00007 *** 00008 *** Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu 00009 *** Original Affiliation: UCLA, Computer Science Department, 00010 *** Los Angeles, CA 90095-1596 USA 00011 *** 00012 *** Permission is hereby granted, free of charge, to any person obtaining 00013 *** a copy of this software and associated documentation files (the 00014 *** "Software"), to deal in the Software without restriction, including 00015 *** without limitation 00016 *** the rights to use, copy, modify, merge, publish, distribute, sublicense, 00017 *** and/or sell copies of the Software, and to permit persons to whom the 00018 *** Software is furnished to do so, subject to the following conditions: 00019 *** 00020 *** The above copyright notice and this permission notice shall be included 00021 *** in all copies or substantial portions of the Software. 00022 *** 00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00030 *** 00031 *** 00032 ***************************************************************************/ 00033 00034 00035 00036 00037 #ifndef BTREECOMPACT_H 00038 #define BTREECOMPACT_H 00039 00040 #include "btree.h" 00041 00042 #include <vector> 00043 #include <algorithm> 00044 using namespace std; 00045 00046 // -------------------------------------------------------- 00047 class BTreeCompactor : public BTree 00048 { 00049 public: 00050 inline BTreeCompactor(const BTree& orig_tree); 00051 inline void operator =(const BTree& new_tree); 00052 inline void slimAssign(const BTree& new_tree); // suffices if evaluation 00053 // follows 00054 00055 inline int compact(); 00056 void build_orth_tree(); 00057 00058 const vector<BTreeNode>& orth_tree; 00059 00060 private: 00061 vector<BTreeNode> in_orth_tree; // [NUM_BLOCKS] ~ in_tree[NUM_BLOCKS+1] 00062 // [NUM_BLOCKS+1] ~ in_tree[NUM_BLOCKS] 00063 00064 void build_orth_tree_add_block(int treePtr); 00065 00066 // (a) swap NUM_BLK vs. NUM_BLK+1 00067 // (b) fix parent of BL-block 00068 // used by build_orth_tree() only!!! 00069 inline static void fix_orth_tree(vector<BTreeNode>& orth_tree); 00070 }; 00071 // -------------------------------------------------------- 00072 00073 // ========================= 00074 // IMPLEMENTATIONS 00075 // ========================= 00076 BTreeCompactor::BTreeCompactor(const BTree& orig_tree) 00077 : BTree(orig_tree), 00078 orth_tree(in_orth_tree), 00079 in_orth_tree(orig_tree.tree.size()) 00080 { 00081 int vec_size = in_orth_tree.size(); 00082 for (int i = 0; i < vec_size; i++) 00083 { 00084 in_orth_tree[i].parent = UNDEFINED; 00085 in_orth_tree[i].left = UNDEFINED; 00086 in_orth_tree[i].right = UNDEFINED; 00087 in_orth_tree[i].block_index = orig_tree.tree[i].block_index; 00088 in_orth_tree[i].orient = 00089 OrientedPacking::flip(OrientedPacking::ORIENT(orig_tree.tree[i].orient)); 00090 } 00091 } 00092 // -------------------------------------------------------- 00093 inline void BTreeCompactor::operator =(const BTree& new_tree) 00094 { 00095 this->BTree::operator =(new_tree); 00096 for (unsigned int i = 0; i < in_orth_tree.size(); i++) 00097 { 00098 in_orth_tree[i].parent = UNDEFINED; 00099 in_orth_tree[i].left = UNDEFINED; 00100 in_orth_tree[i].right = UNDEFINED; 00101 in_orth_tree[i].block_index = new_tree.tree[i].block_index; 00102 in_orth_tree[i].orient = 00103 OrientedPacking::flip(OrientedPacking::ORIENT(new_tree.tree[i].orient)); 00104 } 00105 } 00106 // -------------------------------------------------------- 00107 inline void BTreeCompactor::slimAssign(const BTree& new_tree) 00108 { 00109 in_tree = new_tree.tree; 00110 in_blockArea = new_tree.blockArea(); 00111 for (unsigned int i = 0; i < in_orth_tree.size(); i++) 00112 { 00113 in_orth_tree[i].parent = UNDEFINED; 00114 in_orth_tree[i].left = UNDEFINED; 00115 in_orth_tree[i].right = UNDEFINED; 00116 in_orth_tree[i].block_index = new_tree.tree[i].block_index; 00117 in_orth_tree[i].orient = 00118 OrientedPacking::flip(OrientedPacking::ORIENT(new_tree.tree[i].orient)); 00119 } 00120 } 00121 // -------------------------------------------------------- 00122 inline int BTreeCompactor::compact() 00123 { 00124 vector<double> orig_xloc(in_xloc); 00125 vector<double> orig_yloc(in_yloc); 00126 00127 build_orth_tree(); 00128 swap_ranges(in_tree.begin(), in_tree.end(), 00129 in_orth_tree.begin()); 00130 // cout << "-----here1: after build_orth_tree()-----" << endl; 00131 // cout << "in_tree: " << endl; 00132 // OutputBTree(cout, in_tree); 00133 // cout << endl << endl; 00134 // cout << "in_orth_tree:" << endl; 00135 // OutputBTree(cout, in_orth_tree); 00136 // cout << endl << endl; 00137 00138 build_orth_tree(); 00139 swap_ranges(in_tree.begin(), in_tree.end(), 00140 in_orth_tree.begin()); 00141 // cout << "-----here2: after build_orth_tree()-----" << endl; 00142 // cout << "in_tree: " << endl; 00143 // OutputBTree(cout, in_tree); 00144 // cout << endl << endl; 00145 // cout << "in_orth_tree: " << endl; 00146 // OutputBTree(cout, in_orth_tree); 00147 // cout << endl << endl; 00148 // cout << "-----flipped packing-----" << endl; 00149 // OutputPacking(cout, BTreeOrientedPacking(*this)); 00150 00151 // ofstream outfile; 00152 // outfile.open("dummy_flipped"); 00153 // Save_bbb(outfile, BTreeOrientedPacking(*this)); 00154 // outfile.close(); 00155 00156 build_orth_tree(); 00157 00158 int count = 0; 00159 for (int i = 0; i < NUM_BLOCKS; i++) 00160 { 00161 if ((orig_xloc[i] != in_xloc[i]) || 00162 (orig_yloc[i] != in_yloc[i])) 00163 count ++; 00164 } 00165 return count; 00166 } 00167 // -------------------------------------------------------- 00168 inline void BTreeCompactor::fix_orth_tree(vector<BTree::BTreeNode>& orth_tree) 00169 { 00170 // fix the tree s.t. in_tree[NUM_BLOCKS] corresponds to the root 00171 const int NUM_BLOCKS = orth_tree.size() - 2; 00172 int bottomLeftBlock = orth_tree[NUM_BLOCKS+1].left; 00173 00174 orth_tree[bottomLeftBlock].parent = NUM_BLOCKS; 00175 iter_swap(&(orth_tree[NUM_BLOCKS]), &(orth_tree[NUM_BLOCKS+1])); 00176 } 00177 // -------------------------------------------------------- 00178 #endif