summaryrefslogtreecommitdiffstats
path: root/indexlib/stringset.h
blob: 0043d7a1386416201130e7483edd9890efc675ce (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#ifndef LPC_STRINGSET_H1106061353_INCLUDE_GUARD_
#define LPC_STRINGSET_H1106061353_INCLUDE_GUARD_

/* This file is part of indexlib.
 * Copyright (C) 2005 Luís Pedro Coelho <luis@luispedro.org>
 *
 * Indexlib is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation and available as file
 * GPL_V2 which is distributed along with indexlib.
 * 
 * Indexlib 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., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA
 * 
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of this program with any edition of
 * the Qt library by Trolltech AS, Norway (or with modified versions
 * of Qt that use the same license as Qt), and distribute linked
 * combinations including the two.  You must obey the GNU General
 * Public License in all respects for all of the code used other than
 * Qt.  If you modify this file, you may extend this exception to
 * your version of the file, but you are not obligated to do so.  If
 * you do not wish to do so, delete this exception statement from
 * your version.
 */


#include "memvector.h"
#include "stringarray.h"
#include <iterator>
#include <iostream>
#include <utility>


struct stringset {
	public:
		struct const_iterator : public std::iterator<STD_NAMESPACE_PREFIX random_access_iterator_tag,const char*> {
				const char* operator*() const {
					return mother_->strings_.get_cstr( mother_->ordered_[ idx_ ] );
				}
				unsigned id() const {
					return mother_->ordered_[ idx_ ];
				}
				unsigned order() const {
					return idx_;
				}
				const_iterator& operator ++() {
					++idx_;
					return *this;
				}
				const_iterator& operator --() {
					--idx_;
					return *this;
				}

				const_iterator& operator +=( ptrdiff_t d ) {
					idx_ += d;
					return *this;
				}
				const_iterator& operator -=( ptrdiff_t d ) {
					idx_ -= d;
					return *this;
				}
				const_iterator& operator = ( const const_iterator& other ) {
					mother_ = other.mother_;
					idx_ = other.idx_;
					return *this;
				}
				ptrdiff_t operator - ( const const_iterator& other ) const {
						assert( mother_ == other.mother_ );
						return idx_ - other.idx_;
				}
				bool operator < ( const const_iterator& other ) const {
						assert( mother_ == other.mother_ );
						return idx_ < other.idx_;
				}

				bool operator == ( const const_iterator& other ) const {
						return mother_ == other.mother_ && idx_ == other.idx_;
				}

				bool operator != ( const const_iterator& other ) const {
						return !( *this == other );
				}
				const_iterator():
					mother_( 0 ),
					idx_( 0 )
					{
					}
				const_iterator( const const_iterator& other ):
					mother_( other.mother_ ),
					idx_( other.idx_ )
					{
					}
			private:
				friend std::ostream& operator << ( std::ostream& out, const const_iterator& other );
				friend class stringset;
				const_iterator( const stringset* m, unsigned i ):
						mother_( m ),
						idx_( i )
						{
						}

				const stringset* mother_;
				unsigned idx_;
				
		};
		friend class const_iterator;
	public:
		stringset( std::string );
		bool count( const char* str ) const { return order_of( str ) != unsigned( -1 ); }
		unsigned id_of( const char* str ) const {
			unsigned order = order_of( str );
			return order == unsigned( -1 ) ?
			       unsigned( -1 ):
			       ordered_[ order ];
		}
		unsigned order_of( const char* ) const;
		unsigned add( std::string str ) { return add( str.c_str() ); }
		unsigned add( const char* );

		void clear();

		/**
		 * Returns std::make_pair( find( word ), find( word + 'Z' ) ) which makes it easy
		 * to implement word* searches
		 */
		std::pair<const_iterator, const_iterator> upper_lower( const char* ) const;

		const_iterator begin() const { return const_iterator( this, 0 ); }
		const_iterator end() const { return const_iterator( this, ordered_.size() ); }
		bool empty() const { return !ordered_.size(); }
		unsigned size() const { return ordered_.size(); }

		static void remove( std::string );
		const_iterator lower_bound( const char* ) const;
	private:
		stringarray strings_;
		memvector<stringarray::index_type> ordered_;
		memvector<stringarray::index_type> trie_;
};

inline
stringset::const_iterator operator + ( stringset::const_iterator a, stringset::const_iterator::difference_type d ) {
				return a += d;
}

inline
std::ostream& operator << ( std::ostream& out, const stringset::const_iterator& other ) {
				return out << "[ " << other.idx_ << " ]";
}


#endif /* LPC_STRINGSET_H1106061353_INCLUDE_GUARD_ */