WiscDB
 All Classes Functions Variables Friends
file_iterator.h
00001 
00008 #pragma once
00009 
00010 #include <cassert>
00011 #include "file.h"
00012 #include "page.h"
00013 #include "types.h"
00014 
00015 namespace wiscdb {
00016 
00023 class FileIterator {
00024  public:
00028   FileIterator()
00029       : file_(NULL),
00030         current_page_number_(Page::INVALID_NUMBER) {
00031   }
00032 
00039   FileIterator(File* file)
00040       : file_(file) {
00041     assert(file_ != NULL);
00042     const FileHeader& header = file_->readHeader();
00043     current_page_number_ = header.first_used_page;
00044   }
00045 
00053   FileIterator(File* file, PageId page_number)
00054       : file_(file),
00055         current_page_number_(page_number) {
00056   }
00057 
00061   inline FileIterator& operator++() {
00062     assert(file_ != NULL);
00063     const PageHeader& header = file_->readPageHeader(current_page_number_);
00064     current_page_number_ = header.next_page_number;
00065 
00066     return *this;
00067   }
00068 
00069   //postfix
00070   inline FileIterator operator++(int)
00071   {
00072     FileIterator tmp = *this;   // copy ourselves
00073 
00074     assert(file_ != NULL);
00075     const PageHeader& header = file_->readPageHeader(current_page_number_);
00076     current_page_number_ = header.next_page_number;
00077 
00078     return tmp;
00079   }
00080 
00087   inline bool operator==(const FileIterator& rhs) const {
00088     return file_->filename() == rhs.file_->filename() &&
00089         current_page_number_ == rhs.current_page_number_;
00090   }
00091 
00092   inline bool operator!=(const FileIterator& rhs) const {
00093     return (file_->filename() != rhs.file_->filename()) ||
00094         (current_page_number_ != rhs.current_page_number_);
00095   }
00096 
00103   inline Page operator*() const
00104   { return file_->readPage(current_page_number_); }
00105 
00106  private:
00110   File* file_;
00111 
00115   PageId current_page_number_;
00116 };
00117 
00118 }
 All Classes Functions Variables Friends