comparison data/model/SparseModel.h @ 608:d7f3dfe6f9a4

* solaris build fixes
author Chris Cannam
date Thu, 10 Sep 2009 18:44:45 +0000
parents 1415e35881f6
children e22b6e89a7f7
comparison
equal deleted inserted replaced
607:a67651386253 608:d7f3dfe6f9a4
24 #include <iostream> 24 #include <iostream>
25 25
26 #include <set> 26 #include <set>
27 #include <vector> 27 #include <vector>
28 #include <algorithm> 28 #include <algorithm>
29 #include <iterator>
29 30
30 #include <cmath> 31 #include <cmath>
31 32
32 #include <QMutex> 33 #include <QMutex>
33 #include <QTextStream> 34 #include <QTextStream>
285 virtual int getRowForFrame(long frame) const 286 virtual int getRowForFrame(long frame) const
286 { 287 {
287 if (m_rows.empty()) rebuildRowVector(); 288 if (m_rows.empty()) rebuildRowVector();
288 std::vector<long>::iterator i = 289 std::vector<long>::iterator i =
289 std::lower_bound(m_rows.begin(), m_rows.end(), frame); 290 std::lower_bound(m_rows.begin(), m_rows.end(), frame);
291 #if defined(__SUNPRO_CC) && defined(__STD_RW_ITERATOR__)
292 int row = 0;
293 std::distance(m_rows.begin(), i, row);
294 #else
290 int row = std::distance(m_rows.begin(), i); 295 int row = std::distance(m_rows.begin(), i);
296 #endif
291 if (i != m_rows.begin() && (i == m_rows.end() || *i != frame)) { 297 if (i != m_rows.begin() && (i == m_rows.end() || *i != frame)) {
292 --row; 298 --row;
293 } 299 }
294 return row; 300 return row;
295 } 301 }
366 mutable QMutex m_mutex; 372 mutable QMutex m_mutex;
367 int m_completion; 373 int m_completion;
368 374
369 void getPointIterators(long frame, 375 void getPointIterators(long frame,
370 PointListIterator &startItr, 376 PointListIterator &startItr,
371 PointListIterator &endItr) const; 377 PointListIterator &endItr);
378 void getPointIterators(long frame,
379 PointListConstIterator &startItr,
380 PointListConstIterator &endItr) const;
372 381
373 // This is only used if the model is called on to act in 382 // This is only used if the model is called on to act in
374 // TabularModel mode 383 // TabularModel mode
375 mutable std::vector<long> m_rows; // map from row number to frame 384 mutable std::vector<long> m_rows; // map from row number to frame
376 void rebuildRowVector() const 385 void rebuildRowVector() const
377 { 386 {
378 m_rows.clear(); 387 m_rows.clear();
379 for (PointListIterator i = m_points.begin(); i != m_points.end(); ++i) { 388 for (PointListConstIterator i = m_points.begin(); i != m_points.end(); ++i) {
380 m_rows.push_back(i->frame); 389 m_rows.push_back(i->frame);
381 } 390 }
382 } 391 }
383 392
384 PointListIterator getPointListIteratorForRow(int row) const 393 PointListIterator getPointListIteratorForRow(int row)
385 { 394 {
386 if (m_rows.empty()) rebuildRowVector(); 395 if (m_rows.empty()) rebuildRowVector();
387 if (row < 0 || row + 1 > int(m_rows.size())) return m_points.end(); 396 if (row < 0 || row + 1 > int(m_rows.size())) return m_points.end();
388 397
389 size_t frame = m_rows[row]; 398 size_t frame = m_rows[row];
393 int initialIndexAtFrame = indexAtFrame; 402 int initialIndexAtFrame = indexAtFrame;
394 403
395 PointListIterator i0, i1; 404 PointListIterator i0, i1;
396 getPointIterators(frame, i0, i1); 405 getPointIterators(frame, i0, i1);
397 PointListIterator i = i0; 406 PointListIterator i = i0;
407
408 for (i = i0; i != i1; ++i) {
409 if (indexAtFrame > 0) { --indexAtFrame; continue; }
410 return i;
411 }
412
413 if (indexAtFrame > 0) {
414 std::cerr << "WARNING: SparseModel::getPointListIteratorForRow: No iterator available for row " << row << " (frame = " << frame << ", index at frame = " << initialIndexAtFrame << ", leftover index " << indexAtFrame << ")" << std::endl;
415 }
416 return i;
417 }
418
419 PointListConstIterator getPointListIteratorForRow(int row) const
420 {
421 if (m_rows.empty()) rebuildRowVector();
422 if (row < 0 || row + 1 > int(m_rows.size())) return m_points.end();
423
424 size_t frame = m_rows[row];
425 int indexAtFrame = 0;
426 int ri = row;
427 while (ri > 0 && m_rows[ri-1] == m_rows[row]) { --ri; ++indexAtFrame; }
428 int initialIndexAtFrame = indexAtFrame;
429
430 PointListConstIterator i0, i1;
431 getPointIterators(frame, i0, i1);
432 PointListConstIterator i = i0;
398 433
399 for (i = i0; i != i1; ++i) { 434 for (i = i0; i != i1; ++i) {
400 if (indexAtFrame > 0) { --indexAtFrame; continue; } 435 if (indexAtFrame > 0) { --indexAtFrame; continue; }
401 return i; 436 return i;
402 } 437 }
441 SparseModel<PointType>::getEndFrame() const 476 SparseModel<PointType>::getEndFrame() const
442 { 477 {
443 QMutexLocker locker(&m_mutex); 478 QMutexLocker locker(&m_mutex);
444 size_t f = 0; 479 size_t f = 0;
445 if (!m_points.empty()) { 480 if (!m_points.empty()) {
446 PointListIterator i(m_points.end()); 481 PointListConstIterator i(m_points.end());
447 f = (--i)->frame; 482 f = (--i)->frame;
448 } 483 }
449 return f; 484 return f;
450 } 485 }
451 486
491 if (start > end) return PointList(); 526 if (start > end) return PointList();
492 QMutexLocker locker(&m_mutex); 527 QMutexLocker locker(&m_mutex);
493 528
494 PointType startPoint(start), endPoint(end); 529 PointType startPoint(start), endPoint(end);
495 530
496 PointListIterator startItr = m_points.lower_bound(startPoint); 531 PointListConstIterator startItr = m_points.lower_bound(startPoint);
497 PointListIterator endItr = m_points.upper_bound(endPoint); 532 PointListConstIterator endItr = m_points.upper_bound(endPoint);
498 533
499 if (startItr != m_points.begin()) --startItr; 534 if (startItr != m_points.begin()) --startItr;
500 if (startItr != m_points.begin()) --startItr; 535 if (startItr != m_points.begin()) --startItr;
501 if (endItr != m_points.end()) ++endItr; 536 if (endItr != m_points.end()) ++endItr;
502 if (endItr != m_points.end()) ++endItr; 537 if (endItr != m_points.end()) ++endItr;
503 538
504 PointList rv; 539 PointList rv;
505 540
506 for (PointListIterator i = startItr; i != endItr; ++i) { 541 for (PointListConstIterator i = startItr; i != endItr; ++i) {
507 rv.insert(*i); 542 rv.insert(*i);
508 } 543 }
509 544
510 return rv; 545 return rv;
511 } 546 }
512 547
513 template <typename PointType> 548 template <typename PointType>
514 typename SparseModel<PointType>::PointList 549 typename SparseModel<PointType>::PointList
515 SparseModel<PointType>::getPoints(long frame) const 550 SparseModel<PointType>::getPoints(long frame) const
516 { 551 {
517 PointListIterator startItr, endItr; 552 PointListConstIterator startItr, endItr;
518 getPointIterators(frame, startItr, endItr); 553 getPointIterators(frame, startItr, endItr);
519 554
520 PointList rv; 555 PointList rv;
521 556
522 for (PointListIterator i = startItr; i != endItr; ++i) { 557 for (PointListConstIterator i = startItr; i != endItr; ++i) {
523 rv.insert(*i); 558 rv.insert(*i);
524 } 559 }
525 560
526 return rv; 561 return rv;
527 } 562 }
528 563
529 template <typename PointType> 564 template <typename PointType>
530 void 565 void
531 SparseModel<PointType>::getPointIterators(long frame, 566 SparseModel<PointType>::getPointIterators(long frame,
532 PointListIterator &startItr, 567 PointListIterator &startItr,
533 PointListIterator &endItr) const 568 PointListIterator &endItr)
534 { 569 {
535 QMutexLocker locker(&m_mutex); 570 QMutexLocker locker(&m_mutex);
536 571
537 if (m_resolution == 0) { 572 if (m_resolution == 0) {
538 startItr = m_points.end(); 573 startItr = m_points.end();
548 startItr = m_points.lower_bound(startPoint); 583 startItr = m_points.lower_bound(startPoint);
549 endItr = m_points.upper_bound(endPoint); 584 endItr = m_points.upper_bound(endPoint);
550 } 585 }
551 586
552 template <typename PointType> 587 template <typename PointType>
588 void
589 SparseModel<PointType>::getPointIterators(long frame,
590 PointListConstIterator &startItr,
591 PointListConstIterator &endItr) const
592 {
593 QMutexLocker locker(&m_mutex);
594
595 if (m_resolution == 0) {
596 startItr = m_points.end();
597 endItr = m_points.end();
598 return;
599 }
600
601 long start = (frame / m_resolution) * m_resolution;
602 long end = start + m_resolution;
603
604 PointType startPoint(start), endPoint(end);
605
606 startItr = m_points.lower_bound(startPoint);
607 endItr = m_points.upper_bound(endPoint);
608 }
609
610 template <typename PointType>
553 typename SparseModel<PointType>::PointList 611 typename SparseModel<PointType>::PointList
554 SparseModel<PointType>::getPreviousPoints(long originFrame) const 612 SparseModel<PointType>::getPreviousPoints(long originFrame) const
555 { 613 {
556 QMutexLocker locker(&m_mutex); 614 QMutexLocker locker(&m_mutex);
557 615
558 PointType lookupPoint(originFrame); 616 PointType lookupPoint(originFrame);
559 PointList rv; 617 PointList rv;
560 618
561 PointListIterator i = m_points.lower_bound(lookupPoint); 619 PointListConstIterator i = m_points.lower_bound(lookupPoint);
562 if (i == m_points.begin()) return rv; 620 if (i == m_points.begin()) return rv;
563 621
564 --i; 622 --i;
565 long frame = i->frame; 623 long frame = i->frame;
566 while (i->frame == frame) { 624 while (i->frame == frame) {
579 QMutexLocker locker(&m_mutex); 637 QMutexLocker locker(&m_mutex);
580 638
581 PointType lookupPoint(originFrame); 639 PointType lookupPoint(originFrame);
582 PointList rv; 640 PointList rv;
583 641
584 PointListIterator i = m_points.upper_bound(lookupPoint); 642 PointListConstIterator i = m_points.upper_bound(lookupPoint);
585 if (i == m_points.end()) return rv; 643 if (i == m_points.end()) return rv;
586 644
587 long frame = i->frame; 645 long frame = i->frame;
588 while (i != m_points.end() && i->frame == frame) { 646 while (i != m_points.end() && i->frame == frame) {
589 rv.insert(*i); 647 rv.insert(*i);
736 out << indent; 794 out << indent;
737 out << QString("<dataset id=\"%1\" dimensions=\"%2\">\n") 795 out << QString("<dataset id=\"%1\" dimensions=\"%2\">\n")
738 .arg(getObjectExportId(&m_points)) 796 .arg(getObjectExportId(&m_points))
739 .arg(PointType(0).getDimensions()); 797 .arg(PointType(0).getDimensions());
740 798
741 for (PointListIterator i = m_points.begin(); i != m_points.end(); ++i) { 799 for (PointListConstIterator i = m_points.begin(); i != m_points.end(); ++i) {
742 i->toXml(out, indent + " "); 800 i->toXml(out, indent + " ");
743 } 801 }
744 802
745 out << indent; 803 out << indent;
746 out << "</dataset>\n"; 804 out << "</dataset>\n";