aboutsummaryrefslogtreecommitdiff
path: root/src/hash.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-08-28 06:10:45 +0000
committerMike Buland <eichlan@xagasoft.com>2009-08-28 06:10:45 +0000
commit6e7f15f8157499796689a2bff1d110e83104ef43 (patch)
tree03c0f2949ecc811debd8b89bdfeb6f55b691c311 /src/hash.h
parent02782a0ac44aa1ddd4260198ec206ade293c82ba (diff)
downloadlibbu++-6e7f15f8157499796689a2bff1d110e83104ef43.tar.gz
libbu++-6e7f15f8157499796689a2bff1d110e83104ef43.tar.bz2
libbu++-6e7f15f8157499796689a2bff1d110e83104ef43.tar.xz
libbu++-6e7f15f8157499796689a2bff1d110e83104ef43.zip
Corrected the iterator in Bu::Hash, and fixed the erase function in Bu::List,
there are a couple more fine points to touch on in Bu::Hash::iterator, I should go through and review the whole thing at this point (iterator-wise).
Diffstat (limited to '')
-rw-r--r--src/hash.h85
1 files changed, 55 insertions, 30 deletions
diff --git a/src/hash.h b/src/hash.h
index f183a4c..10c661f 100644
--- a/src/hash.h
+++ b/src/hash.h
@@ -234,6 +234,7 @@ namespace Bu
234 class Hash 234 class Hash
235 { 235 {
236 friend struct HashProxy<key, value, sizecalc, keyalloc, valuealloc, challoc>; 236 friend struct HashProxy<key, value, sizecalc, keyalloc, valuealloc, challoc>;
237 typedef class Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> MyType;
237 public: 238 public:
238 Hash() : 239 Hash() :
239 nCapacity( 11 ), 240 nCapacity( 11 ),
@@ -452,7 +453,7 @@ namespace Bu
452 */ 453 */
453 virtual void erase( struct iterator &i ) 454 virtual void erase( struct iterator &i )
454 { 455 {
455 if( this != &i.hsh ) 456 if( this != i.hsh )
456 throw HashException("This iterator didn't come from this Hash."); 457 throw HashException("This iterator didn't come from this Hash.");
457 if( isFilled( i.nPos ) && !isDeleted( i.nPos ) ) 458 if( isFilled( i.nPos ) && !isDeleted( i.nPos ) )
458 { 459 {
@@ -556,22 +557,22 @@ namespace Bu
556 { 557 {
557 friend class Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>; 558 friend class Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>;
558 private: 559 private:
559 iterator( Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh ) : 560 iterator( MyType *hsh ) :
560 hsh( hsh ), 561 hsh( hsh ),
561 nPos( 0 ), 562 nPos( 0 ),
562 bFinished( false ) 563 bFinished( false )
563 { 564 {
564 nPos = hsh.getFirstPos( bFinished ); 565 nPos = hsh->getFirstPos( bFinished );
565 } 566 }
566 567
567 iterator( Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh, bool bDone ) : 568 iterator( MyType *hsh, bool bDone ) :
568 hsh( hsh ), 569 hsh( hsh ),
569 nPos( 0 ), 570 nPos( 0 ),
570 bFinished( bDone ) 571 bFinished( bDone )
571 { 572 {
572 } 573 }
573 574
574 Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh; 575 MyType *hsh;
575 uint32_t nPos; 576 uint32_t nPos;
576 bool bFinished; 577 bool bFinished;
577 578
@@ -583,6 +584,13 @@ namespace Bu
583 { 584 {
584 } 585 }
585 586
587 iterator() :
588 hsh( NULL ),
589 nPos( NULL ),
590 bFinished( true )
591 {
592 }
593
586 DEPRECATED bool isActive() const 594 DEPRECATED bool isActive() const
587 { 595 {
588 return !bFinished; 596 return !bFinished;
@@ -604,7 +612,7 @@ namespace Bu
604 iterator operator++( int ) 612 iterator operator++( int )
605 { 613 {
606 if( bFinished == false ) 614 if( bFinished == false )
607 nPos = hsh.getNextPos( nPos, bFinished ); 615 nPos = hsh->getNextPos( nPos, bFinished );
608 616
609 return *this; 617 return *this;
610 } 618 }
@@ -615,7 +623,7 @@ namespace Bu
615 iterator operator++() 623 iterator operator++()
616 { 624 {
617 if( bFinished == false ) 625 if( bFinished == false )
618 nPos = hsh.getNextPos( nPos, bFinished ); 626 nPos = hsh->getNextPos( nPos, bFinished );
619 627
620 return *this; 628 return *this;
621 } 629 }
@@ -652,9 +660,7 @@ namespace Bu
652 */ 660 */
653 iterator operator=( const iterator &oth ) 661 iterator operator=( const iterator &oth )
654 { 662 {
655 if( &hsh != &oth.hsh ) 663 hsh = oth.hsh;
656 throw HashException(
657 "Cannot mix iterators from different hash objects.");
658 nPos = oth.nPos; 664 nPos = oth.nPos;
659 bFinished = oth.bFinished; 665 bFinished = oth.bFinished;
660 return *this; 666 return *this;
@@ -666,12 +672,12 @@ namespace Bu
666 */ 672 */
667 value &operator *() 673 value &operator *()
668 { 674 {
669 return hsh.getValueAtPos( nPos ); 675 return hsh->getValueAtPos( nPos );
670 } 676 }
671 677
672 const value &operator *() const 678 const value &operator *() const
673 { 679 {
674 return hsh.getValueAtPos( nPos ); 680 return hsh->getValueAtPos( nPos );
675 } 681 }
676 682
677 /** 683 /**
@@ -680,7 +686,7 @@ namespace Bu
680 */ 686 */
681 key &getKey() 687 key &getKey()
682 { 688 {
683 return hsh.getKeyAtPos( nPos ); 689 return hsh->getKeyAtPos( nPos );
684 } 690 }
685 691
686 /** 692 /**
@@ -689,7 +695,7 @@ namespace Bu
689 */ 695 */
690 value &getValue() 696 value &getValue()
691 { 697 {
692 return hsh.getValueAtPos( nPos ); 698 return hsh->getValueAtPos( nPos );
693 } 699 }
694 } iterator; 700 } iterator;
695 701
@@ -700,26 +706,47 @@ namespace Bu
700 { 706 {
701 friend class Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>; 707 friend class Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>;
702 private: 708 private:
703 const_iterator( const Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh ) : 709 const_iterator( const MyType *hsh ) :
704 hsh( hsh ), 710 hsh( hsh ),
705 nPos( 0 ), 711 nPos( 0 ),
706 bFinished( false ) 712 bFinished( false )
707 { 713 {
708 nPos = hsh.getFirstPos( bFinished ); 714 nPos = hsh->getFirstPos( bFinished );
709 } 715 }
710 716
711 const_iterator( const Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh, bool bDone ) : 717 const_iterator( const MyType *hsh, bool bDone ) :
712 hsh( hsh ), 718 hsh( hsh ),
713 nPos( 0 ), 719 nPos( 0 ),
714 bFinished( bDone ) 720 bFinished( bDone )
715 { 721 {
716 } 722 }
717 723
718 const Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh; 724 const MyType *hsh;
719 uint32_t nPos; 725 uint32_t nPos;
720 bool bFinished; 726 bool bFinished;
721 727
722 public: 728 public:
729 const_iterator() :
730 hsh( NULL ),
731 nPos( 0 ),
732 bFinished( true )
733 {
734 }
735
736 const_iterator( const const_iterator &src ) :
737 hsh( src.hsh ),
738 nPos( src.nPos ),
739 bFinished( src.bFinished )
740 {
741 }
742
743 const_iterator( const iterator &src ) :
744 hsh( src.hsh ),
745 nPos( src.nPos ),
746 bFinished( src.bFinished )
747 {
748 }
749
723 bool isValid() const 750 bool isValid() const
724 { 751 {
725 return !bFinished; 752 return !bFinished;
@@ -736,7 +763,7 @@ namespace Bu
736 const_iterator operator++( int ) 763 const_iterator operator++( int )
737 { 764 {
738 if( bFinished == false ) 765 if( bFinished == false )
739 nPos = hsh.getNextPos( nPos, bFinished ); 766 nPos = hsh->getNextPos( nPos, bFinished );
740 767
741 return *this; 768 return *this;
742 } 769 }
@@ -747,7 +774,7 @@ namespace Bu
747 const_iterator operator++() 774 const_iterator operator++()
748 { 775 {
749 if( bFinished == false ) 776 if( bFinished == false )
750 nPos = hsh.getNextPos( nPos, bFinished ); 777 nPos = hsh->getNextPos( nPos, bFinished );
751 778
752 return *this; 779 return *this;
753 } 780 }
@@ -784,9 +811,7 @@ namespace Bu
784 */ 811 */
785 const_iterator operator=( const const_iterator &oth ) 812 const_iterator operator=( const const_iterator &oth )
786 { 813 {
787 if( &hsh != &oth.hsh ) 814 hsh = oth.hsh;
788 throw HashException(
789 "Cannot mix iterators from different hash objects.");
790 nPos = oth.nPos; 815 nPos = oth.nPos;
791 bFinished = oth.bFinished; 816 bFinished = oth.bFinished;
792 return *this; 817 return *this;
@@ -798,7 +823,7 @@ namespace Bu
798 */ 823 */
799 const value &operator *() const 824 const value &operator *() const
800 { 825 {
801 return hsh.getValueAtPos( nPos ); 826 return hsh->getValueAtPos( nPos );
802 } 827 }
803 828
804 /** 829 /**
@@ -807,7 +832,7 @@ namespace Bu
807 */ 832 */
808 const key &getKey() const 833 const key &getKey() const
809 { 834 {
810 return hsh.getKeyAtPos( nPos ); 835 return hsh->getKeyAtPos( nPos );
811 } 836 }
812 837
813 /** 838 /**
@@ -816,7 +841,7 @@ namespace Bu
816 */ 841 */
817 const value &getValue() const 842 const value &getValue() const
818 { 843 {
819 return hsh.getValueAtPos( nPos ); 844 return hsh->getValueAtPos( nPos );
820 } 845 }
821 } const_iterator; 846 } const_iterator;
822 847
@@ -827,12 +852,12 @@ namespace Bu
827 */ 852 */
828 iterator begin() 853 iterator begin()
829 { 854 {
830 return iterator( *this ); 855 return iterator( this );
831 } 856 }
832 857
833 const_iterator begin() const 858 const_iterator begin() const
834 { 859 {
835 return const_iterator( *this ); 860 return const_iterator( this );
836 } 861 }
837 862
838 /** 863 /**
@@ -843,12 +868,12 @@ namespace Bu
843 */ 868 */
844 iterator end() 869 iterator end()
845 { 870 {
846 return iterator( *this, true ); 871 return iterator( this, true );
847 } 872 }
848 873
849 const_iterator end() const 874 const_iterator end() const
850 { 875 {
851 return const_iterator( *this, true ); 876 return const_iterator( this, true );
852 } 877 }
853 878
854 /** 879 /**