1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*
** PJL C++ Library
** file_vector.h
**
** Copyright (C) 1998 Paul J. Lucas
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef file_vector_H
#define file_vector_H
// standard
#include <iostream.h>
#include <iterator>
#include <sys/types.h> /* for off_t */
// local
#include "fake_ansi.h"
class file_vector_base {
public:
typedef off_t size_type;
typedef ptrdiff_t difference_type;
file_vector_base() { init(); }
file_vector_base( char const *path, ios::open_mode mode = ios::in ) {
init();
open( path, mode );
}
~file_vector_base() { close(); }
bool open( char const *path, ios::open_mode = ios::in );
void close();
bool empty() const { return !size_; }
int error() const { return error_; }
size_type max_size() const { return size_; }
size_type size() const { return size_; }
operator bool() const { return !error_; }
protected:
void* base() const { return addr_; }
private:
size_type size_;
int fd_; // Unix file descriptor
void *addr_;
int error_;
void init();
};
//*****************************************************************************
//
// SYNOPSYS
//
// file_vector< T >
//
// DESCRIPTION
//
// A file_vector is an object that maps a file into memory (via the Unix
// system call mmap(2)) allowing it to be accessed via iterators.
// Processing a file, especially files accessed randomly, is MUCH faster
// than standard I/O. The type T is usually a character type, but could
// be a numeric type for reading vectors or matricies of, say, long or
// double.
//
// SEE ALSO
//
// mmap(2)
//
//*****************************************************************************
template< class T > class file_vector : public file_vector_base {
public:
////////// typedefs ///////////////////////////////////////////////////
typedef T value_type;
typedef value_type* pointer;
typedef value_type const* const_pointer;
typedef value_type& reference;
typedef value_type const& const_reference;
////////// constructors ///////////////////////////////////////////////
file_vector() { }
file_vector( char const *path, ios::open_mode mode = ios::in ) :
file_vector_base( path, mode ) { }
////////// iterators //////////////////////////////////////////////////
typedef pointer iterator;
typedef const_pointer const_iterator;
typedef reverse_bidirectional_iterator<
iterator, value_type, reference, difference_type
> reverse_iterator;
typedef reverse_bidirectional_iterator<
const_iterator, value_type, const_reference, difference_type
> const_reverse_iterator;
iterator begin() { return (iterator)base(); }
const_iterator begin() const { return (const_iterator)base(); }
iterator end() { return begin() + size(); }
const_iterator end() const { return begin() + size(); }
reverse_iterator rbegin() {
return reverse_iterator( end() );
}
reverse_iterator rend() {
return reverse_iterator( begin() );
}
const_reverse_iterator rbegin() const {
return const_reverse_iterator( end() );
}
const_reverse_iterator rend() const {
return const_reverse_iterator( begin() );
}
////////// element access /////////////////////////////////////////////
reference back() { return *( end() - 1 ); }
const_reference back() const { return *( end() - 1 ); }
reference front() { return *begin(); }
const_reference front() const { return *begin(); }
reference operator[]( size_type i ) {
return *( begin() + i );
}
const_reference operator[]( size_type i ) const {
return *( begin() + i );
}
};
#endif /* file_vector_H */
|