diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-07-01 07:51:52 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-07-01 07:51:52 +0000 |
commit | 62fb9ee2ff8b1d9590c92a703d6506de6aa8df52 (patch) | |
tree | 456cae6b5b0aee217abff6ec447e4871998f68e0 /src | |
parent | 364b888d35f5acdef3721025e02210bce5ae3082 (diff) | |
download | libbu++-62fb9ee2ff8b1d9590c92a703d6506de6aa8df52.tar.gz libbu++-62fb9ee2ff8b1d9590c92a703d6506de6aa8df52.tar.bz2 libbu++-62fb9ee2ff8b1d9590c92a703d6506de6aa8df52.tar.xz libbu++-62fb9ee2ff8b1d9590c92a703d6506de6aa8df52.zip |
That was weird. Bu::Hash was basically unusable const'd, this has been
corrected. There may be more operations that need to be fixed, but all basic
data operations are fixed and a const_iterator has been added.
Diffstat (limited to '')
-rw-r--r-- | src/hash.h | 149 |
1 files changed, 143 insertions, 6 deletions
@@ -7,7 +7,8 @@ | |||
7 | #include <iostream> | 7 | #include <iostream> |
8 | #include <list> | 8 | #include <list> |
9 | #include <utility> | 9 | #include <utility> |
10 | #include "exceptionbase.h" | 10 | #include "bu/exceptionbase.h" |
11 | #include "bu/list.h" | ||
11 | ///#include "archival.h" | 12 | ///#include "archival.h" |
12 | ///#include "archive.h" | 13 | ///#include "archive.h" |
13 | 14 | ||
@@ -593,6 +594,122 @@ namespace Bu | |||
593 | return hsh.getValueAtPos( nPos ); | 594 | return hsh.getValueAtPos( nPos ); |
594 | } | 595 | } |
595 | }; | 596 | }; |
597 | |||
598 | /** | ||
599 | * Iteration structure for iterating through the hash (const). | ||
600 | */ | ||
601 | typedef struct const_iterator | ||
602 | { | ||
603 | friend class Hash<key, value, sizecalc, keyalloc, valuealloc, challoc>; | ||
604 | private: | ||
605 | const_iterator( const Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh ) : | ||
606 | hsh( hsh ), | ||
607 | nPos( 0 ), | ||
608 | bFinished( false ) | ||
609 | { | ||
610 | nPos = hsh.getFirstPos( bFinished ); | ||
611 | } | ||
612 | |||
613 | const_iterator( const Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh, bool bDone ) : | ||
614 | hsh( hsh ), | ||
615 | nPos( 0 ), | ||
616 | bFinished( bDone ) | ||
617 | { | ||
618 | } | ||
619 | |||
620 | const Hash<key, value, sizecalc, keyalloc, valuealloc, challoc> &hsh; | ||
621 | uint32_t nPos; | ||
622 | bool bFinished; | ||
623 | |||
624 | public: | ||
625 | /** | ||
626 | * Iterator incrementation operator. Move the iterator forward. | ||
627 | */ | ||
628 | const_iterator operator++( int ) | ||
629 | { | ||
630 | if( bFinished == false ) | ||
631 | nPos = hsh.getNextPos( nPos, bFinished ); | ||
632 | |||
633 | return *this; | ||
634 | } | ||
635 | |||
636 | /** | ||
637 | * Iterator incrementation operator. Move the iterator forward. | ||
638 | */ | ||
639 | const_iterator operator++() | ||
640 | { | ||
641 | if( bFinished == false ) | ||
642 | nPos = hsh.getNextPos( nPos, bFinished ); | ||
643 | |||
644 | return *this; | ||
645 | } | ||
646 | |||
647 | /** | ||
648 | * Iterator equality comparison operator. Iterators the same? | ||
649 | */ | ||
650 | bool operator==( const const_iterator &oth ) | ||
651 | { | ||
652 | if( bFinished != oth.bFinished ) | ||
653 | return false; | ||
654 | if( bFinished == true ) | ||
655 | { | ||
656 | return true; | ||
657 | } | ||
658 | else | ||
659 | { | ||
660 | if( oth.nPos == nPos ) | ||
661 | return true; | ||
662 | return false; | ||
663 | } | ||
664 | } | ||
665 | |||
666 | /** | ||
667 | * Iterator not equality comparison operator. Not the same? | ||
668 | */ | ||
669 | bool operator!=( const const_iterator &oth ) | ||
670 | { | ||
671 | return !(*this == oth ); | ||
672 | } | ||
673 | |||
674 | /** | ||
675 | * Iterator assignment operator. | ||
676 | */ | ||
677 | const_iterator operator=( const const_iterator &oth ) | ||
678 | { | ||
679 | if( &hsh != &oth.hsh ) | ||
680 | throw HashException( | ||
681 | "Cannot mix iterators from different hash objects."); | ||
682 | nPos = oth.nPos; | ||
683 | bFinished = oth.bFinished; | ||
684 | } | ||
685 | |||
686 | /** | ||
687 | * Iterator dereference operator... err.. get the value | ||
688 | *@returns (value_type &) The value behind this iterator. | ||
689 | */ | ||
690 | const value &operator *() const | ||
691 | { | ||
692 | return hsh.getValueAtPos( nPos ); | ||
693 | } | ||
694 | |||
695 | /** | ||
696 | * Get the key behind this iterator. | ||
697 | *@returns (key_type &) The key behind this iterator. | ||
698 | */ | ||
699 | const key &getKey() const | ||
700 | { | ||
701 | return hsh.getKeyAtPos( nPos ); | ||
702 | } | ||
703 | |||
704 | /** | ||
705 | * Get the value behind this iterator. | ||
706 | *@returs (value_type &) The value behind this iterator. | ||
707 | */ | ||
708 | const value &getValue() const | ||
709 | { | ||
710 | return hsh.getValueAtPos( nPos ); | ||
711 | } | ||
712 | }; | ||
596 | 713 | ||
597 | /** | 714 | /** |
598 | * Get an iterator pointing to the first item in the hash table. | 715 | * Get an iterator pointing to the first item in the hash table. |
@@ -604,6 +721,11 @@ namespace Bu | |||
604 | return iterator( *this ); | 721 | return iterator( *this ); |
605 | } | 722 | } |
606 | 723 | ||
724 | const_iterator begin() const | ||
725 | { | ||
726 | return const_iterator( *this ); | ||
727 | } | ||
728 | |||
607 | /** | 729 | /** |
608 | * Get an iterator pointing to a point just past the last item in the | 730 | * Get an iterator pointing to a point just past the last item in the |
609 | * hash table. | 731 | * hash table. |
@@ -614,14 +736,19 @@ namespace Bu | |||
614 | { | 736 | { |
615 | return iterator( *this, true ); | 737 | return iterator( *this, true ); |
616 | } | 738 | } |
739 | |||
740 | const_iterator end() const | ||
741 | { | ||
742 | return const_iterator( *this, true ); | ||
743 | } | ||
617 | 744 | ||
618 | /** | 745 | /** |
619 | * Get a list of all the keys in the hash table. | 746 | * Get a list of all the keys in the hash table. |
620 | *@returns (std::list<key_type>) The list of keys in the hash table. | 747 | *@returns (std::list<key_type>) The list of keys in the hash table. |
621 | */ | 748 | */ |
622 | std::list<key> getKeys() | 749 | Bu::List<key> getKeys() const |
623 | { | 750 | { |
624 | std::list<key> lKeys; | 751 | Bu::List<key> lKeys; |
625 | 752 | ||
626 | for( uint32_t j = 0; j < nCapacity; j++ ) | 753 | for( uint32_t j = 0; j < nCapacity; j++ ) |
627 | { | 754 | { |
@@ -629,7 +756,7 @@ namespace Bu | |||
629 | { | 756 | { |
630 | if( !isDeleted( j ) ) | 757 | if( !isDeleted( j ) ) |
631 | { | 758 | { |
632 | lKeys.push_back( aKeys[j] ); | 759 | lKeys.append( aKeys[j] ); |
633 | } | 760 | } |
634 | } | 761 | } |
635 | } | 762 | } |
@@ -682,12 +809,22 @@ namespace Bu | |||
682 | return aKeys[nPos]; | 809 | return aKeys[nPos]; |
683 | } | 810 | } |
684 | 811 | ||
812 | virtual const key &getKeyAtPos( uint32_t nPos ) const | ||
813 | { | ||
814 | return aKeys[nPos]; | ||
815 | } | ||
816 | |||
685 | virtual value &getValueAtPos( uint32_t nPos ) | 817 | virtual value &getValueAtPos( uint32_t nPos ) |
686 | { | 818 | { |
687 | return aValues[nPos]; | 819 | return aValues[nPos]; |
688 | } | 820 | } |
821 | |||
822 | virtual const value &getValueAtPos( uint32_t nPos ) const | ||
823 | { | ||
824 | return aValues[nPos]; | ||
825 | } | ||
689 | 826 | ||
690 | virtual uint32_t getFirstPos( bool &bFinished ) | 827 | virtual uint32_t getFirstPos( bool &bFinished ) const |
691 | { | 828 | { |
692 | for( uint32_t j = 0; j < nCapacity; j++ ) | 829 | for( uint32_t j = 0; j < nCapacity; j++ ) |
693 | { | 830 | { |
@@ -700,7 +837,7 @@ namespace Bu | |||
700 | return 0; | 837 | return 0; |
701 | } | 838 | } |
702 | 839 | ||
703 | virtual uint32_t getNextPos( uint32_t nPos, bool &bFinished ) | 840 | virtual uint32_t getNextPos( uint32_t nPos, bool &bFinished ) const |
704 | { | 841 | { |
705 | for( uint32_t j = nPos+1; j < nCapacity; j++ ) | 842 | for( uint32_t j = nPos+1; j < nCapacity; j++ ) |
706 | { | 843 | { |