iterator.cc modification

/* iterator.cc

 * This file is part of cdbxx library

 * Copyright (c) 2004 by Stanislav Ievlev

 *

 * This file is covered by the GNU Library General Public License,

 * which should be included with libcdbxx as the file COPYING.

 */

#include <sstream>

#include <stdexcept>

#include <fcntl.h>

#include <unistd.h>

#include "cdbxx/iterator.hh"

using namespace cdbxx;

iterator::iterator(mem_pos pos,bool fill):

        pos_(pos)

{

    if (pos_ && fill) fill_pair(); //if position is not zero fill pair with the some values

}

iterator& iterator::operator++() /**< prefix operator */

{

    pos_ += keylen_ + valuelen_ + 2 * cdbi_size; //go to the next record

    fill_pair();//save all values in temporary place

    return *this;

}

iterator iterator::operator++(int)

{

    iterator tmp(*this);

    operator++();

    return tmp;

}

iterator& iterator::operator--() /**< prefix operator */

{

    pos_ -= keylen_ + valuelen_ + 2 * cdbi_size; //go to the next record

    fill_pair();//save all values in temporary place

    return *this;

}

iterator iterator::operator--(int)

{

    iterator tmp(*this);

    operator--();

    return tmp;

}

iterator& iterator::operator+= ( int j )

{

    pos_ += (keylen_ + valuelen_ + 2 * cdbi_size) * j; //go to the next record

    fill_pair();//save all values in temporary place

    return *this;

}

iterator& iterator::operator-= ( int j )

{

    pos_ -= (keylen_ + valuelen_ + 2 * cdbi_size) * j; //go to the next record

    fill_pair();//save all values in temporary place

    return *this;

}

bool iterator::equal(const iterator& other) const

{

    return (pos_ == other.pos_);

}

const data_pair& iterator::operator*() const

{

    return pair_;

}

const data_pair* iterator::operator->() const

{

    return &(operator*());

}

void iterator::fill_pair()

{

    keylen_ = ::cdb_unpack(pos_);

    valuelen_ = ::cdb_unpack(pos_ + cdbi_size);

    pair_.first.len_ = keylen_;

    pair_.first.data_ = pos_ + 2 * cdbi_size;

    pair_.second.len_ = valuelen_;

    pair_.second.data_ = pos_ + 2 * cdbi_size + keylen_;

}