My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef AFX_NSEARCH_H__8336E133_90C5_4059_8605_6066BD37D042__INCLUDED_
#define AFX_NSEARCH_H__8336E133_90C5_4059_8605_6066BD37D042__INCLUDED_
#include "Search.h"

//=========================================================================
//@{ @pkg Gp.Search //@}
// BM–@ŒŸõ—pƒ|ƒŠƒV[s
//=========================================================================

//@{ ‘å•¶Žš¬•¶Žš‚ð‹æ•Ê‚·‚éƒ|ƒŠƒV[ //@}
struct CaseSensitive
{
static unicode map( unicode c )
{ return c; }
static bool not_equal( unicode c1, unicode c2 )
{ return c1!=c2; }
};

//@{ ‘å•¶Žš¬•¶Žš‚ð‹æ•Ê‚µ‚È‚¢ƒ|ƒŠƒV[ //@}
struct IgnoreCase
{
static unicode map( unicode c )
{ return (L'a'<=c && c<=L'z' ? c-L'a'+L'A' : c); }
static bool not_equal( unicode c1, unicode c2 )
{ return map(c1)!=map(c2); }
};



//=========================================================================
//@{
// BM–@‚É‚æ‚镁’ʂ̐³•ûŒüŒŸõ
//@}
//=========================================================================
#ifdef __GNUC__
inline void memFF( void* ptrv, int siz )
{ BYTE* ptr = (BYTE*)ptrv;
for(;siz>3;siz-=4,ptr+=4) *(DWORD*)ptr = 0xffffffff;
for(;siz;--siz,++ptr) *ptr = 0xff; }
#endif
template<class ComparisonPolicy> class BMSearch
{
public:
BMSearch( const unicode* key )
: keylen_( my_lstrlenW(key) )
, key_( my_lstrcpyW( new unicode[keylen_+1], key ) )
{
memFF( lastAppearance_, sizeof(lastAppearance_) );
for( int i=0, e=keylen_; i<e; ++i )
lastAppearance_[ ComparisonPolicy::map(key[i]) ] = i;
}

~BMSearch()
{
delete [] key_;
}

int Search( const unicode* str, int strlen )
{
for( int i=0, e=strlen-keylen_, j, t; i<=e; i+=(j>t?j-t:1) )
{
for( j=keylen_-1; j>=0; --j )
{
if( ComparisonPolicy::not_equal( key_[j], str[i+j] ) )
break;
}
if( j < 0 )
return i;
t = lastAppearance_[ ComparisonPolicy::map(str[i+j]) ];
}
return -1;
}

int keylen() const { return keylen_; }

private:
int keylen_;
unicode* key_;
int lastAppearance_[65536];
};



//=========================================================================
//@{
// BM–@‚É‚æ‚é‹t•ûŒüŒŸõ
//@}
//=========================================================================

template<class ComparisonPolicy> class BMSearchRev
{
public:
BMSearchRev( const unicode* key )
: keylen_( my_lstrlenW(key) )
, key_( my_lstrcpyW( new unicode[keylen_+1], key ) )
{
memFF( firstAppearance_, sizeof(firstAppearance_) );
for( int i=keylen_-1; i>=0; --i )
firstAppearance_[ ComparisonPolicy::map(key[i]) ] = i;
}

~BMSearchRev()
{
delete [] key_;
}

int Search( const unicode* str, int strlen )
{
for( int i=strlen-keylen_-1, j, e, t; i>=0; i-=(t>j?t-j:1) )
{
for( j=0, e=keylen_; j<e; ++j )
if( ComparisonPolicy::not_equal( key_[j], str[i+j] ) )
break;
if( j >= e )
return i;
t = firstAppearance_[ ComparisonPolicy::map(str[i+j]) ];
if( t == -1 ) t = keylen_;
}
return -1;
}

int keylen() const { return keylen_; }

private:
int keylen_;
unicode* key_;
int firstAppearance_[65536];
};



//=========================================================================
//@{
// Searhcable‚Æ‚µ‚Ă̎À‘•i³•ûŒüŒŸõj
//@}
//=========================================================================

template<class CompalisonPolicy>
class NSearch : public Searchable
{
public:
NSearch( const unicode* key ) : s_(key) {}

private:
bool Search(
const unicode* str, ulong len, ulong stt, ulong* mbg, ulong* med )
{
int n = s_.Search( str+stt, len-stt );
if( n < 0 )
return false;
*mbg = stt + n;
*med = stt + n + s_.keylen();
return true;
}

private:
BMSearch<CompalisonPolicy> s_;
};



//=========================================================================
//@{
// Searhcable‚Æ‚µ‚Ă̎À‘•i‹t•ûŒüŒŸõj
//@}
//=========================================================================

template<class CompalisonPolicy>
class NSearchRev : public Searchable
{
public:
NSearchRev( const unicode* key ) : s_(key) {}

private:
bool Search(
const unicode* str, ulong len, ulong stt, ulong* mbg, ulong* med )
{
int n = s_.Search( str, stt+s_.keylen() );
if( n < 0 )
return false;
*mbg = n;
*med = n + s_.keylen();
return true;
}

private:
BMSearchRev<CompalisonPolicy> s_;
};



//=========================================================================

#endif

Change log

r97 by roytam on May 6, 2010   Diff
- [GreenPad] gcc fix
Go to: 
Project members, sign in to write a code review

Older revisions

r94 by roytam on May 5, 2010   Diff
- [GreenPad] import kinaba's bbs #9776
patch: gcc support, DateFormat
r89 by roytam on Apr 29, 2010   Diff
- [GreenPad] import GreenPad with NT
3.51 fix.
All revisions of this file

File info

Size: 4357 bytes, 194 lines

File properties

svn:mime-type
text/plain; charset=shift_jis
svn:eol-style
native
Powered by Google Project Hosting