[74a91c9] | 1 | /* |
---|
| 2 | * gfanlib_symmetry.cpp |
---|
| 3 | * |
---|
| 4 | * Created on: Oct 22, 2010 |
---|
| 5 | * Author: anders |
---|
| 6 | */ |
---|
[5443c1] | 7 | #include <stddef.h> |
---|
[74a91c9] | 8 | #include "gfanlib_symmetry.h" |
---|
| 9 | #include <map> |
---|
| 10 | |
---|
| 11 | using namespace std; |
---|
| 12 | |
---|
| 13 | namespace gfan{ |
---|
| 14 | class Trie |
---|
| 15 | { |
---|
| 16 | class TrieNode |
---|
| 17 | { |
---|
| 18 | typedef map<int,class TrieNode> Map; |
---|
| 19 | Map m; |
---|
| 20 | public: |
---|
| 21 | TrieNode() |
---|
| 22 | { |
---|
| 23 | |
---|
| 24 | } |
---|
| 25 | TrieNode(IntVector const &v, int i) |
---|
| 26 | { |
---|
[19addd1] | 27 | if(i<(int)v.size()) |
---|
[74a91c9] | 28 | m[v[i]]=TrieNode(v,i+1); |
---|
| 29 | } |
---|
| 30 | int stabilizerSize(ZVector const &v, int i)const |
---|
| 31 | { |
---|
| 32 | int ret=0; |
---|
[19addd1] | 33 | if(i==(int)v.size())return 1; |
---|
[74a91c9] | 34 | for(Map::const_iterator j=m.begin();j!=m.end();j++) |
---|
| 35 | { |
---|
| 36 | if(v[i]==v[j->first]) |
---|
| 37 | ret+=j->second.stabilizerSize(v,i+1); |
---|
| 38 | } |
---|
| 39 | return ret; |
---|
| 40 | } |
---|
| 41 | void search(ZVector const &v, ZVector &building, Permutation &tempPerm, Permutation &ret, ZVector &optimal, int i, bool &isImproving)const |
---|
| 42 | { |
---|
[19addd1] | 43 | if(i==(int)v.size()){ret=tempPerm;optimal=building;isImproving=false;return;} |
---|
[74a91c9] | 44 | if(isImproving) |
---|
| 45 | building[i]=-0x7fffffff; |
---|
| 46 | else |
---|
| 47 | building[i]=optimal[i]; |
---|
| 48 | for(Map::const_iterator j=m.begin();j!=m.end();j++) |
---|
| 49 | if(building[i]<v[j->first]) |
---|
| 50 | { |
---|
| 51 | isImproving=true; |
---|
| 52 | building[i]=v[j->first]; |
---|
| 53 | } |
---|
| 54 | for(Map::const_iterator j=m.begin();j!=m.end();j++) |
---|
| 55 | if(v[j->first]==building[i]) |
---|
| 56 | { |
---|
| 57 | tempPerm[i]=j->first; |
---|
| 58 | j->second.search(v,building,tempPerm,ret,optimal,i+1,isImproving); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | void searchStabalizer(ZVector const &v, ZVector &building, Permutation &tempPerm, Permutation &ret, ZVector &optimal, int i, bool &isImproving, ZVector const &toBeFixed)const |
---|
| 62 | { |
---|
[19addd1] | 63 | if(i==(int)v.size()) |
---|
[74a91c9] | 64 | if(!(tempPerm.apply(v)<optimal)) |
---|
| 65 | { |
---|
| 66 | ret=tempPerm; |
---|
| 67 | optimal=tempPerm.apply(v); |
---|
| 68 | return; |
---|
| 69 | } |
---|
| 70 | for(Map::const_iterator j=m.begin();j!=m.end();j++) |
---|
| 71 | if(toBeFixed[i]==toBeFixed[j->first]) |
---|
| 72 | { |
---|
| 73 | tempPerm[i]=j->first; |
---|
| 74 | j->second.searchStabalizer(v,building,tempPerm,ret,optimal,i+1,isImproving,toBeFixed); |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | /* this code contains mistakes void searchStabalizer(IntegerVector const &v, IntegerVector &building, IntegerVector &tempPerm, IntegerVector &ret, IntegerVector &optimal, int i, bool &isImproving, IntegerVector const &toBeFixed)const |
---|
| 78 | { |
---|
| 79 | if(i==v.size()){ret=tempPerm;optimal=building;isImproving=false;debug<<"DEEP";return;} |
---|
| 80 | if(isImproving) |
---|
| 81 | building[i]=-0x7fffffff; |
---|
| 82 | else |
---|
| 83 | building[i]=optimal[i]; |
---|
| 84 | for(Map::const_iterator j=m.begin();j!=m.end();j++) |
---|
| 85 | if(toBeFixed[i]==toBeFixed[j->first]) |
---|
| 86 | if(v[j->first]>building[i]) |
---|
| 87 | { |
---|
| 88 | isImproving=true; |
---|
| 89 | building[i]=v[j->first]; |
---|
| 90 | } |
---|
| 91 | for(Map::const_iterator j=m.begin();j!=m.end();j++) |
---|
| 92 | if(toBeFixed[i]==toBeFixed[j->first]) |
---|
| 93 | if(v[j->first]==building[i]) |
---|
| 94 | { |
---|
| 95 | debug.printInteger(i);debug<<":"; |
---|
| 96 | debug.printInteger(j->first);debug<<" "; |
---|
| 97 | tempPerm[i]=j->first; |
---|
| 98 | j->second.searchStabalizer(v,building,tempPerm,ret,optimal,i+1,isImproving,toBeFixed); |
---|
| 99 | } |
---|
| 100 | }*/ |
---|
| 101 | // void doubleSearch(); |
---|
| 102 | void insert(Permutation const &v, int i) |
---|
| 103 | { |
---|
[19addd1] | 104 | if(i==(int)v.size())return; |
---|
[74a91c9] | 105 | if(m.count(v[i])) |
---|
| 106 | m[v[i]].insert(v,i+1); |
---|
| 107 | else |
---|
| 108 | m[v[i]]= TrieNode(v,i+1); |
---|
| 109 | } |
---|
| 110 | /* void print(int i, int n)const |
---|
| 111 | { |
---|
| 112 | if(i==n)return; |
---|
| 113 | for(Map::const_iterator j=m.begin();j!=m.end();j++) |
---|
| 114 | { |
---|
| 115 | {for(int j=0;j<2*i;j++)debug<<" ";} |
---|
| 116 | debug.printInteger(j->first); |
---|
| 117 | debug<<"\n"; |
---|
| 118 | j->second.print(i+1,n); |
---|
| 119 | } |
---|
| 120 | }*/ |
---|
| 121 | int size(int i,int n)const |
---|
| 122 | { |
---|
| 123 | if(i==n)return 1; |
---|
| 124 | int ret=0; |
---|
| 125 | for(Map::const_iterator j=m.begin();j!=m.end();j++) |
---|
| 126 | ret+=j->second.size(i+1,n); |
---|
| 127 | return ret; |
---|
| 128 | } |
---|
| 129 | }; |
---|
| 130 | public: |
---|
| 131 | TrieNode theTree; |
---|
| 132 | int n; |
---|
| 133 | Trie(int n_): |
---|
[19addd1] | 134 | theTree(Permutation(n_),0), |
---|
| 135 | n(n_) |
---|
[74a91c9] | 136 | { |
---|
| 137 | } |
---|
| 138 | int size()const |
---|
| 139 | { |
---|
| 140 | return theTree.size(0,n); |
---|
| 141 | } |
---|
| 142 | void insert(Permutation const &v) |
---|
| 143 | { |
---|
| 144 | theTree.insert(v,0); |
---|
| 145 | // debug<<v; |
---|
| 146 | // theTree.print(0,v.size()); |
---|
| 147 | |
---|
| 148 | // debug<<"---------------------------------------------\n"; |
---|
| 149 | } |
---|
| 150 | /** |
---|
| 151 | * returns the sigma from the set with sigma(v) maximal in the lexicographic ordering. |
---|
| 152 | */ |
---|
| 153 | Permutation search(ZVector const &v) |
---|
| 154 | { |
---|
| 155 | Permutation tempPerm(v.size()); |
---|
| 156 | Permutation ret(v.size()); |
---|
| 157 | ZVector building(v.size()); |
---|
| 158 | ZVector optimal=v;//the identity is always in the trie |
---|
| 159 | bool isImproving=true; |
---|
| 160 | theTree.search(v,building,tempPerm,ret,optimal,0,isImproving); |
---|
| 161 | return ret; |
---|
| 162 | } |
---|
| 163 | Permutation searchStabalizer(ZVector const &v, ZVector const &toBeFixed) |
---|
| 164 | { |
---|
| 165 | Permutation tempPerm(v.size()); |
---|
| 166 | Permutation ret(v.size()); |
---|
| 167 | ZVector building(v.size()); |
---|
| 168 | ZVector optimal=v;//the identity is always in the trie |
---|
| 169 | bool isImproving=true; |
---|
| 170 | theTree.searchStabalizer(v,building,tempPerm,ret,optimal,0,isImproving,toBeFixed); |
---|
| 171 | return ret; |
---|
| 172 | } |
---|
| 173 | int stabilizerSize(ZVector const &v)const |
---|
| 174 | { |
---|
| 175 | return theTree.stabilizerSize(v,0); |
---|
| 176 | } |
---|
| 177 | }; |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | /*IntegerVector SymmetryGroup::identity(int n) |
---|
| 186 | { |
---|
| 187 | IntegerVector v(n); |
---|
| 188 | for(int i=0;i<n;i++)v[i]=i; |
---|
| 189 | |
---|
| 190 | return v; |
---|
| 191 | } |
---|
| 192 | */ |
---|
| 193 | |
---|
| 194 | Permutation Permutation::inverse()const |
---|
| 195 | { |
---|
| 196 | return applyInverse(Permutation(size())); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | SymmetryGroup::SymmetryGroup(int n): |
---|
| 201 | // byteTable(0), |
---|
| 202 | trie(0) |
---|
| 203 | { |
---|
| 204 | elements.insert(Permutation(n)); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | int SymmetryGroup::sizeOfBaseSet()const |
---|
| 209 | { |
---|
| 210 | assert(!elements.empty()); |
---|
| 211 | return elements.begin()->size(); |
---|
| 212 | } |
---|
| 213 | |
---|
[c4d065] | 214 | IntMatrix SymmetryGroup::getGenerators()const |
---|
| 215 | { |
---|
| 216 | IntMatrix ret(0,this->sizeOfBaseSet()); |
---|
| 217 | for(ElementContainer::const_iterator i=elements.begin();i!=elements.end();i++)ret.appendRow(i->toIntVector()); |
---|
| 218 | return ret; |
---|
| 219 | } |
---|
| 220 | |
---|
[74a91c9] | 221 | void SymmetryGroup::computeClosure(Permutation const &v) //does this work?? |
---|
| 222 | { |
---|
| 223 | ElementContainer newOnes; |
---|
| 224 | |
---|
| 225 | newOnes.insert(v); |
---|
| 226 | |
---|
| 227 | while(!newOnes.empty()) |
---|
| 228 | { |
---|
| 229 | Permutation v=*newOnes.begin(); |
---|
| 230 | for(ElementContainer::const_iterator i=elements.begin();i!=elements.end();i++) |
---|
| 231 | { |
---|
| 232 | { |
---|
| 233 | Permutation n=i->apply(v); |
---|
| 234 | if(0==elements.count(n)) |
---|
| 235 | newOnes.insert(n); |
---|
| 236 | } |
---|
| 237 | { |
---|
[c4d065] | 238 | Permutation n=v.apply(v); |
---|
[74a91c9] | 239 | if(0==elements.count(n)) |
---|
| 240 | newOnes.insert(n); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | newOnes.erase(v); |
---|
| 244 | elements.insert(v); |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[c4d065] | 249 | void SymmetryGroup::computeClosure(IntMatrix const &l) |
---|
[74a91c9] | 250 | { |
---|
[c4d065] | 251 | for(int i=0;i<l.getHeight();i++)computeClosure(Permutation(l[i])); |
---|
[74a91c9] | 252 | } |
---|
[c4d065] | 253 | |
---|
[74a91c9] | 254 | |
---|
| 255 | /* |
---|
| 256 | void SymmetryGroup::print(FILE *f) |
---|
| 257 | { |
---|
| 258 | AsciiPrinter P(f); |
---|
| 259 | P.printString("Printing SymmetryGroup\n"); |
---|
| 260 | IntegerVectorList l; |
---|
| 261 | for(ElementContainer::const_iterator i=elements.begin();i!=elements.end();i++) |
---|
| 262 | { |
---|
| 263 | // P.printVector(*i); |
---|
| 264 | // P.printNewLine(); |
---|
| 265 | l.push_back(*i); |
---|
| 266 | } |
---|
| 267 | P.printVectorList(l); |
---|
| 268 | fprintf(f,"Group order=%i\n",elements.size()); |
---|
| 269 | P.printString("Done printing SymmetryGroup.\n"); |
---|
| 270 | } |
---|
| 271 | */ |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | Permutation Permutation::apply(Permutation const &b)const |
---|
| 275 | { |
---|
| 276 | IntVector ret(size()); |
---|
| 277 | assert(size()==b.size()); |
---|
[19addd1] | 278 | for(unsigned i=0;i<size();i++)ret[i]=b[(*this)[i]]; |
---|
[74a91c9] | 279 | return Permutation(ret); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | Permutation Permutation::applyInverse(Permutation const &b)const |
---|
| 283 | { |
---|
| 284 | IntVector ret(size()); |
---|
| 285 | assert(size()==b.size()); |
---|
[19addd1] | 286 | for(unsigned i=0;i<size();i++)ret[(*this)[i]]=b[i]; |
---|
[74a91c9] | 287 | return Permutation(ret); |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | IntVector Permutation::apply(IntVector const &v)const |
---|
| 291 | { |
---|
| 292 | IntVector ret(size()); |
---|
| 293 | assert(size()==v.size()); |
---|
[19addd1] | 294 | for(unsigned i=0;i<size();i++)ret[i]=v[(*this)[i]]; |
---|
[74a91c9] | 295 | return ret; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | ZVector Permutation::apply(ZVector const &v)const |
---|
| 299 | { |
---|
| 300 | ZVector ret(size()); |
---|
| 301 | assert(size()==v.size()); |
---|
[19addd1] | 302 | for(unsigned i=0;i<size();i++)ret[i]=v[(*this)[i]]; |
---|
[74a91c9] | 303 | return ret; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | |
---|
| 307 | ZVector Permutation::applyInverse(ZVector const &v)const |
---|
| 308 | { |
---|
| 309 | ZVector ret(size()); |
---|
| 310 | assert(size()==v.size()); |
---|
[19addd1] | 311 | for(unsigned i=0;i<size();i++)ret[(*this)[i]]=v[i]; |
---|
[74a91c9] | 312 | return ret; |
---|
| 313 | } |
---|
| 314 | //ZVector apply(ZVector const &v)const; |
---|
| 315 | //ZMatrix apply(ZMatrix const &m)const; |
---|
| 316 | //IntVector applyInverse(IntVector const &v)const; |
---|
| 317 | //ZMatrix applyInverse(ZMatrix const &m)const; |
---|
| 318 | |
---|
| 319 | ZVector SymmetryGroup::orbitRepresentative(ZVector const &v, Permutation *usedPermutation)const |
---|
| 320 | { |
---|
| 321 | if(trie){ |
---|
| 322 | if(usedPermutation) |
---|
| 323 | { |
---|
| 324 | *usedPermutation=trie->search(v); |
---|
| 325 | return usedPermutation->apply(v); |
---|
| 326 | } |
---|
| 327 | return trie->search(v).apply(v); |
---|
| 328 | } |
---|
| 329 | ZVector ret=v; |
---|
| 330 | ElementContainer::const_iterator usedPerm; |
---|
| 331 | for(ElementContainer::const_iterator i=elements.begin();i!=elements.end();i++) |
---|
| 332 | { |
---|
| 333 | ZVector q=i->apply(v); |
---|
| 334 | if(! (q<ret))//negation to make sure that usedPerm is set |
---|
| 335 | { |
---|
| 336 | usedPerm=i; |
---|
| 337 | ret=q; |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | if(usedPermutation)*usedPermutation=*usedPerm; |
---|
| 342 | |
---|
| 343 | if(trie) |
---|
| 344 | { |
---|
| 345 | // debug<<"Input"<<v<<"\n"; |
---|
| 346 | // debug<<"Bruteforce"<<ret<<"\n"; |
---|
| 347 | Permutation triePerm=trie->search(v); |
---|
| 348 | // debug<<"Trie"<<compose(triePerm,v)<<"\n"; |
---|
| 349 | assert((triePerm.apply(v)-ret).isZero()); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | return ret; |
---|
| 353 | } |
---|
[5ff68b] | 354 | |
---|
| 355 | ZVector SymmetryGroup::orbitRepresentativeFixing(ZVector const &v, ZVector const &fixed)const |
---|
[74a91c9] | 356 | { |
---|
| 357 | if(trie){ |
---|
[5ff68b] | 358 | return trie->searchStabalizer(v,fixed).apply(v); |
---|
[74a91c9] | 359 | } |
---|
[5ff68b] | 360 | ZVector ret=v; |
---|
[74a91c9] | 361 | |
---|
| 362 | for(ElementContainer::const_iterator i=elements.begin();i!=elements.end();i++) |
---|
[5ff68b] | 363 | if(i->apply(fixed)==fixed) |
---|
[74a91c9] | 364 | { |
---|
[5ff68b] | 365 | ZVector q=i->apply(v); |
---|
[74a91c9] | 366 | if(ret<q)ret=q; |
---|
| 367 | } |
---|
| 368 | if(trie){ |
---|
[5ff68b] | 369 | ZVector temp=trie->searchStabalizer(v,fixed).apply(v); |
---|
[74a91c9] | 370 | // debug<<"Input"<<v; |
---|
| 371 | // debug<<"Brute"<<ret; |
---|
| 372 | // debug<<"Quick"<<temp; |
---|
| 373 | assert((temp-ret).isZero()); |
---|
| 374 | // return compose(trie->searchStabalizer(v,fixed),v); |
---|
| 375 | } |
---|
| 376 | return ret; |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | bool Permutation::isPermutation(IntVector const &a) |
---|
| 380 | { |
---|
| 381 | int n=a.size(); |
---|
| 382 | IntVector temp(n); |
---|
| 383 | for(int i=0;i<n;i++)temp[i]=-1; |
---|
| 384 | for(int i=0;i<n;i++) |
---|
| 385 | { |
---|
| 386 | if(a[i]<0 || a[i]>=n)return false; |
---|
| 387 | temp[i]=i; |
---|
| 388 | } |
---|
| 389 | for(int i=0;i<n;i++)if(temp[i]<0)return false; |
---|
| 390 | return true; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | |
---|
[43317d] | 394 | bool Permutation::arePermutations(IntMatrix const &m) |
---|
| 395 | { |
---|
| 396 | for(int i=0;i<m.getHeight();i++)if(!isPermutation(m[i]))return false; |
---|
| 397 | return true; |
---|
| 398 | } |
---|
| 399 | |
---|
[74a91c9] | 400 | int SymmetryGroup::orbitSize(ZVector const &stable)const |
---|
| 401 | { |
---|
| 402 | int groupSize=elements.size(); |
---|
| 403 | |
---|
| 404 | int n=stable.size(); |
---|
| 405 | int numFixed=0; |
---|
| 406 | |
---|
| 407 | if(trie) |
---|
| 408 | { |
---|
| 409 | numFixed=trie->stabilizerSize(stable); |
---|
| 410 | } |
---|
| 411 | else |
---|
| 412 | { |
---|
| 413 | for(SymmetryGroup::ElementContainer::const_iterator j=elements.begin();j!=elements.end();j++) |
---|
| 414 | { |
---|
| 415 | bool doesFix=true; |
---|
| 416 | |
---|
| 417 | for(int i=0;i<n;i++) |
---|
| 418 | if(stable[i]!=stable[(*j)[i]]) |
---|
| 419 | { |
---|
| 420 | doesFix=false; |
---|
| 421 | break; |
---|
| 422 | } |
---|
| 423 | if(doesFix)numFixed++; |
---|
| 424 | } |
---|
| 425 | } |
---|
| 426 | return groupSize/numFixed; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | |
---|
| 430 | ZVector Permutation::fundamentalDomainInequality()const |
---|
| 431 | { |
---|
[19addd1] | 432 | for(unsigned i=0;i<size();i++) |
---|
| 433 | if((*this)[i]!=(int)i) |
---|
[74a91c9] | 434 | return ZVector::standardVector(size(),i)-ZVector::standardVector(size(),(*this)[i]); |
---|
| 435 | return ZVector(size()); |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | |
---|
| 439 | ZMatrix SymmetryGroup::fundamentalDomainInequalities()const |
---|
| 440 | { |
---|
| 441 | set<ZVector> ret2; |
---|
| 442 | for(ElementContainer::const_iterator i=elements.begin();i!=elements.end();i++) |
---|
| 443 | ret2.insert(i->fundamentalDomainInequality()); |
---|
| 444 | |
---|
| 445 | ZMatrix ret(0,sizeOfBaseSet()); |
---|
| 446 | for(set<ZVector>::const_iterator i=ret2.begin();i!=ret2.end();i++) |
---|
| 447 | if(!i->isZero())ret.appendRow(*i); |
---|
| 448 | |
---|
| 449 | return ret; |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | |
---|
| 453 | void SymmetryGroup::createTrie() |
---|
| 454 | { |
---|
| 455 | trie=new Trie(sizeOfBaseSet()); |
---|
| 456 | for(ElementContainer::const_iterator i=elements.begin();i!=elements.end();i++) |
---|
| 457 | trie->insert(*i); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | |
---|
| 461 | |
---|
| 462 | bool SymmetryGroup::isTrivial()const |
---|
| 463 | { |
---|
| 464 | ElementContainer::const_iterator i=elements.begin(); |
---|
| 465 | assert(i!=elements.end()); |
---|
| 466 | i++; |
---|
| 467 | return i==elements.end(); |
---|
| 468 | } |
---|
| 469 | #if 0 |
---|
| 470 | static int mergeSortRek(IntegerVector &v, int begin, int end, IntegerVector &temp) |
---|
| 471 | { |
---|
| 472 | if(end-begin<2)return 0; |
---|
| 473 | int med=(begin+end)>>1; |
---|
| 474 | int nswaps=mergeSortRek(v,begin,med,temp); |
---|
| 475 | nswaps+=mergeSortRek(v,med,end,temp); |
---|
| 476 | |
---|
| 477 | { |
---|
| 478 | int Astart=begin; |
---|
| 479 | int Alength=med-begin; |
---|
| 480 | int Bstart=med; |
---|
| 481 | int Blength=end-med; |
---|
| 482 | int nextFree=begin; |
---|
| 483 | while(nextFree!=end) |
---|
| 484 | { |
---|
| 485 | // debug<<"Astart:"<<Astart<<"Alength:"<<Alength<<"Bstart:"<<Bstart<<"Blength:"<<Blength<<"nextFree:"<<nextFree<<"\n"; |
---|
| 486 | if(Blength==0 || (Alength!=0 && v[Astart]<v[Bstart])) |
---|
| 487 | { |
---|
| 488 | temp[nextFree++]=v[Astart++]; |
---|
| 489 | Alength--; |
---|
| 490 | } |
---|
| 491 | else |
---|
| 492 | { |
---|
| 493 | temp[nextFree++]=v[Bstart++]; |
---|
| 494 | nswaps+=Alength; |
---|
| 495 | Blength--; |
---|
| 496 | } |
---|
| 497 | } |
---|
| 498 | for(int i=begin;i!=end;i++)v[i]=temp[i]; |
---|
| 499 | } |
---|
| 500 | //debug<<"return\n"; |
---|
| 501 | return nswaps; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | int mergeSort(IntegerVector &v) |
---|
| 505 | { |
---|
| 506 | IntegerVector temp(v.size()); |
---|
| 507 | return mergeSortRek(v,0,v.size(),temp); |
---|
| 508 | } |
---|
| 509 | #endif |
---|
| 510 | } |
---|