| 1 |
/** |
|---|
| 2 |
* The array module provides array manipulation routines in a manner that |
|---|
| 3 |
* balances performance and flexibility. Operations are provided for sorting, |
|---|
| 4 |
* and for processing both sorted and unsorted arrays. |
|---|
| 5 |
* |
|---|
| 6 |
* Copyright: Copyright (C) 2005-2006 Sean Kelly. All rights reserved. |
|---|
| 7 |
* License: BSD style: $(LICENSE) |
|---|
| 8 |
* Authors: Sean Kelly |
|---|
| 9 |
*/ |
|---|
| 10 |
module tango.core.Array; |
|---|
| 11 |
|
|---|
| 12 |
private import tango.core.Traits; |
|---|
| 13 |
private import tango.stdc.stdlib : alloca, rand; |
|---|
| 14 |
|
|---|
| 15 |
version( TangoDoc ) |
|---|
| 16 |
{ |
|---|
| 17 |
typedef int Num; |
|---|
| 18 |
typedef int Elem; |
|---|
| 19 |
|
|---|
| 20 |
typedef bool function( Elem ) Pred1E; |
|---|
| 21 |
typedef bool function( Elem, Elem ) Pred2E; |
|---|
| 22 |
typedef size_t function( size_t ) Oper1A; |
|---|
| 23 |
} |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
private |
|---|
| 27 |
{ |
|---|
| 28 |
struct IsEqual( T ) |
|---|
| 29 |
{ |
|---|
| 30 |
static bool opCall( T p1, T p2 ) |
|---|
| 31 |
{ |
|---|
| 32 |
// TODO: Fix this if/when opEquals is changed to return a bool. |
|---|
| 33 |
static if( is( T == class ) || is( T == struct ) ) |
|---|
| 34 |
return (p1 == p2) != 0; |
|---|
| 35 |
else |
|---|
| 36 |
return p1 == p2; |
|---|
| 37 |
} |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
struct IsLess( T ) |
|---|
| 42 |
{ |
|---|
| 43 |
static bool opCall( T p1, T p2 ) |
|---|
| 44 |
{ |
|---|
| 45 |
return p1 < p2; |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
struct RandOper() |
|---|
| 51 |
{ |
|---|
| 52 |
static size_t opCall( size_t lim ) |
|---|
| 53 |
{ |
|---|
| 54 |
// NOTE: The use of 'max' here is intended to eliminate modulo bias |
|---|
| 55 |
// in this routine. |
|---|
| 56 |
size_t max = size_t.max - (size_t.max % lim); |
|---|
| 57 |
size_t val; |
|---|
| 58 |
|
|---|
| 59 |
do |
|---|
| 60 |
{ |
|---|
| 61 |
static if( size_t.sizeof == 4 ) |
|---|
| 62 |
{ |
|---|
| 63 |
val = (((cast(size_t)rand()) << 16) & 0xffff0000u) | |
|---|
| 64 |
(((cast(size_t)rand())) & 0x0000ffffu); |
|---|
| 65 |
} |
|---|
| 66 |
else // assume size_t.sizeof == 8 |
|---|
| 67 |
{ |
|---|
| 68 |
val = (((cast(size_t)rand()) << 48) & 0xffff000000000000uL) | |
|---|
| 69 |
(((cast(size_t)rand()) << 32) & 0x0000ffff00000000uL) | |
|---|
| 70 |
(((cast(size_t)rand()) << 16) & 0x00000000ffff0000uL) | |
|---|
| 71 |
(((cast(size_t)rand())) & 0x000000000000ffffuL); |
|---|
| 72 |
} |
|---|
| 73 |
} while( val > max ); |
|---|
| 74 |
return val % lim; |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
template ElemTypeOf( T ) |
|---|
| 80 |
{ |
|---|
| 81 |
alias typeof(T[0]) ElemTypeOf; |
|---|
| 82 |
} |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 87 |
// Find |
|---|
| 88 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
version( TangoDoc ) |
|---|
| 92 |
{ |
|---|
| 93 |
/** |
|---|
| 94 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 95 |
* the index of the first element matching pat, or buf.length if no match |
|---|
| 96 |
* was found. Comparisons will be performed using the supplied predicate |
|---|
| 97 |
* or '==' if none is supplied. |
|---|
| 98 |
* |
|---|
| 99 |
* Params: |
|---|
| 100 |
* buf = The array to search. |
|---|
| 101 |
* pat = The pattern to search for. |
|---|
| 102 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 103 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 104 |
* callable type. |
|---|
| 105 |
* |
|---|
| 106 |
* Returns: |
|---|
| 107 |
* The index of the first match or buf.length if no match was found. |
|---|
| 108 |
*/ |
|---|
| 109 |
size_t find( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
/** |
|---|
| 113 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 114 |
* the index of the first element matching pat, or buf.length if no match |
|---|
| 115 |
* was found. Comparisons will be performed using the supplied predicate |
|---|
| 116 |
* or '==' if none is supplied. |
|---|
| 117 |
* |
|---|
| 118 |
* Params: |
|---|
| 119 |
* buf = The array to search. |
|---|
| 120 |
* pat = The pattern to search for. |
|---|
| 121 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 122 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 123 |
* callable type. |
|---|
| 124 |
* |
|---|
| 125 |
* Returns: |
|---|
| 126 |
* The index of the first match or buf.length if no match was found. |
|---|
| 127 |
*/ |
|---|
| 128 |
size_t find( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); |
|---|
| 129 |
|
|---|
| 130 |
} |
|---|
| 131 |
else |
|---|
| 132 |
{ |
|---|
| 133 |
template find_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 134 |
{ |
|---|
| 135 |
static assert( isCallableType!(Pred) ); |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 139 |
{ |
|---|
| 140 |
foreach( size_t pos, Elem cur; buf ) |
|---|
| 141 |
{ |
|---|
| 142 |
if( pred( cur, pat ) ) |
|---|
| 143 |
return pos; |
|---|
| 144 |
} |
|---|
| 145 |
return buf.length; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
size_t fn( Elem[] buf, Elem[] pat, Pred pred = Pred.init ) |
|---|
| 150 |
{ |
|---|
| 151 |
if( buf.length == 0 || |
|---|
| 152 |
pat.length == 0 || |
|---|
| 153 |
buf.length < pat.length ) |
|---|
| 154 |
{ |
|---|
| 155 |
return buf.length; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
size_t end = buf.length - pat.length + 1; |
|---|
| 159 |
|
|---|
| 160 |
for( size_t pos = 0; pos < end; ++pos ) |
|---|
| 161 |
{ |
|---|
| 162 |
if( pred( buf[pos], pat[0] ) ) |
|---|
| 163 |
{ |
|---|
| 164 |
size_t mat = 0; |
|---|
| 165 |
|
|---|
| 166 |
do |
|---|
| 167 |
{ |
|---|
| 168 |
if( ++mat >= pat.length ) |
|---|
| 169 |
return pos - pat.length + 1; |
|---|
| 170 |
if( ++pos >= buf.length ) |
|---|
| 171 |
return buf.length; |
|---|
| 172 |
} while( pred( buf[pos], pat[mat] ) ); |
|---|
| 173 |
pos -= mat; |
|---|
| 174 |
} |
|---|
| 175 |
} |
|---|
| 176 |
return buf.length; |
|---|
| 177 |
} |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
template find( Buf, Pat ) |
|---|
| 182 |
{ |
|---|
| 183 |
size_t find( Buf buf, Pat pat ) |
|---|
| 184 |
{ |
|---|
| 185 |
return find_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 186 |
} |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
template find( Buf, Pat, Pred ) |
|---|
| 191 |
{ |
|---|
| 192 |
size_t find( Buf buf, Pat pat, Pred pred ) |
|---|
| 193 |
{ |
|---|
| 194 |
return find_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 195 |
} |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
debug( UnitTest ) |
|---|
| 200 |
{ |
|---|
| 201 |
unittest |
|---|
| 202 |
{ |
|---|
| 203 |
// find element |
|---|
| 204 |
assert( find( "", 'a' ) == 0 ); |
|---|
| 205 |
assert( find( "abc", 'a' ) == 0 ); |
|---|
| 206 |
assert( find( "abc", 'b' ) == 1 ); |
|---|
| 207 |
assert( find( "abc", 'c' ) == 2 ); |
|---|
| 208 |
assert( find( "abc", 'd' ) == 3 ); |
|---|
| 209 |
|
|---|
| 210 |
// null parameters |
|---|
| 211 |
assert( find( "", "" ) == 0 ); |
|---|
| 212 |
assert( find( "a", "" ) == 1 ); |
|---|
| 213 |
assert( find( "", "a" ) == 0 ); |
|---|
| 214 |
|
|---|
| 215 |
// exact match |
|---|
| 216 |
assert( find( "abc", "abc" ) == 0 ); |
|---|
| 217 |
|
|---|
| 218 |
// simple substring match |
|---|
| 219 |
assert( find( "abc", "a" ) == 0 ); |
|---|
| 220 |
assert( find( "abca", "a" ) == 0 ); |
|---|
| 221 |
assert( find( "abc", "b" ) == 1 ); |
|---|
| 222 |
assert( find( "abc", "c" ) == 2 ); |
|---|
| 223 |
assert( find( "abc", "d" ) == 3 ); |
|---|
| 224 |
|
|---|
| 225 |
// multi-char substring match |
|---|
| 226 |
assert( find( "abc", "ab" ) == 0 ); |
|---|
| 227 |
assert( find( "abcab", "ab" ) == 0 ); |
|---|
| 228 |
assert( find( "abc", "bc" ) == 1 ); |
|---|
| 229 |
assert( find( "abc", "ac" ) == 3 ); |
|---|
| 230 |
assert( find( "abrabracadabra", "abracadabra" ) == 3 ); |
|---|
| 231 |
} |
|---|
| 232 |
} |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 237 |
// Reverse Find |
|---|
| 238 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
version( TangoDoc ) |
|---|
| 242 |
{ |
|---|
| 243 |
/** |
|---|
| 244 |
* Performs a linear scan of buf from $(LP)buf.length .. 0$(RB), returning |
|---|
| 245 |
* the index of the first element matching pat, or buf.length if no match |
|---|
| 246 |
* was found. Comparisons will be performed using the supplied predicate |
|---|
| 247 |
* or '==' if none is supplied. |
|---|
| 248 |
* |
|---|
| 249 |
* Params: |
|---|
| 250 |
* buf = The array to search. |
|---|
| 251 |
* pat = The pattern to search for. |
|---|
| 252 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 253 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 254 |
* callable type. |
|---|
| 255 |
* |
|---|
| 256 |
* Returns: |
|---|
| 257 |
* The index of the first match or buf.length if no match was found. |
|---|
| 258 |
*/ |
|---|
| 259 |
size_t rfind( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
/** |
|---|
| 263 |
* Performs a linear scan of buf from $(LP)buf.length .. 0$(RB), returning |
|---|
| 264 |
* the index of the first element matching pat, or buf.length if no match |
|---|
| 265 |
* was found. Comparisons will be performed using the supplied predicate |
|---|
| 266 |
* or '==' if none is supplied. |
|---|
| 267 |
* |
|---|
| 268 |
* Params: |
|---|
| 269 |
* buf = The array to search. |
|---|
| 270 |
* pat = The pattern to search for. |
|---|
| 271 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 272 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 273 |
* callable type. |
|---|
| 274 |
* |
|---|
| 275 |
* Returns: |
|---|
| 276 |
* The index of the first match or buf.length if no match was found. |
|---|
| 277 |
*/ |
|---|
| 278 |
size_t rfind( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); |
|---|
| 279 |
} |
|---|
| 280 |
else |
|---|
| 281 |
{ |
|---|
| 282 |
template rfind_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 283 |
{ |
|---|
| 284 |
static assert( isCallableType!(Pred) ); |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 288 |
{ |
|---|
| 289 |
if( buf.length == 0 ) |
|---|
| 290 |
return buf.length; |
|---|
| 291 |
|
|---|
| 292 |
size_t pos = buf.length; |
|---|
| 293 |
|
|---|
| 294 |
do |
|---|
| 295 |
{ |
|---|
| 296 |
if( pred( buf[--pos], pat ) ) |
|---|
| 297 |
return pos; |
|---|
| 298 |
} while( pos > 0 ); |
|---|
| 299 |
return buf.length; |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
size_t fn( Elem[] buf, Elem[] pat, Pred pred = Pred.init ) |
|---|
| 304 |
{ |
|---|
| 305 |
if( buf.length == 0 || |
|---|
| 306 |
pat.length == 0 || |
|---|
| 307 |
buf.length < pat.length ) |
|---|
| 308 |
{ |
|---|
| 309 |
return buf.length; |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
size_t pos = buf.length - pat.length + 1; |
|---|
| 313 |
|
|---|
| 314 |
do |
|---|
| 315 |
{ |
|---|
| 316 |
if( pred( buf[--pos], pat[0] ) ) |
|---|
| 317 |
{ |
|---|
| 318 |
size_t mat = 0; |
|---|
| 319 |
|
|---|
| 320 |
do |
|---|
| 321 |
{ |
|---|
| 322 |
if( ++mat >= pat.length ) |
|---|
| 323 |
return pos - pat.length + 1; |
|---|
| 324 |
if( ++pos >= buf.length ) |
|---|
| 325 |
return buf.length; |
|---|
| 326 |
} while( pred( buf[pos], pat[mat] ) ); |
|---|
| 327 |
pos -= mat; |
|---|
| 328 |
} |
|---|
| 329 |
} while( pos > 0 ); |
|---|
| 330 |
return buf.length; |
|---|
| 331 |
} |
|---|
| 332 |
} |
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
template rfind( Buf, Pat ) |
|---|
| 336 |
{ |
|---|
| 337 |
size_t rfind( Buf buf, Pat pat ) |
|---|
| 338 |
{ |
|---|
| 339 |
return rfind_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 340 |
} |
|---|
| 341 |
} |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
template rfind( Buf, Pat, Pred ) |
|---|
| 345 |
{ |
|---|
| 346 |
size_t rfind( Buf buf, Pat pat, Pred pred ) |
|---|
| 347 |
{ |
|---|
| 348 |
return rfind_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 349 |
} |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
debug( UnitTest ) |
|---|
| 354 |
{ |
|---|
| 355 |
unittest |
|---|
| 356 |
{ |
|---|
| 357 |
// rfind element |
|---|
| 358 |
assert( rfind( "", 'a' ) == 0 ); |
|---|
| 359 |
assert( rfind( "abc", 'a' ) == 0 ); |
|---|
| 360 |
assert( rfind( "abc", 'b' ) == 1 ); |
|---|
| 361 |
assert( rfind( "abc", 'c' ) == 2 ); |
|---|
| 362 |
assert( rfind( "abc", 'd' ) == 3 ); |
|---|
| 363 |
|
|---|
| 364 |
// null parameters |
|---|
| 365 |
assert( rfind( "", "" ) == 0 ); |
|---|
| 366 |
assert( rfind( "a", "" ) == 1 ); |
|---|
| 367 |
assert( rfind( "", "a" ) == 0 ); |
|---|
| 368 |
|
|---|
| 369 |
// exact match |
|---|
| 370 |
assert( rfind( "abc", "abc" ) == 0 ); |
|---|
| 371 |
|
|---|
| 372 |
// simple substring match |
|---|
| 373 |
assert( rfind( "abc", "a" ) == 0 ); |
|---|
| 374 |
assert( rfind( "abca", "a" ) == 3 ); |
|---|
| 375 |
assert( rfind( "abc", "b" ) == 1 ); |
|---|
| 376 |
assert( rfind( "abc", "c" ) == 2 ); |
|---|
| 377 |
assert( rfind( "abc", "d" ) == 3 ); |
|---|
| 378 |
|
|---|
| 379 |
// multi-char substring match |
|---|
| 380 |
assert( rfind( "abc", "ab" ) == 0 ); |
|---|
| 381 |
assert( rfind( "abcab", "ab" ) == 3 ); |
|---|
| 382 |
assert( rfind( "abc", "bc" ) == 1 ); |
|---|
| 383 |
assert( rfind( "abc", "ac" ) == 3 ); |
|---|
| 384 |
assert( rfind( "abracadabrabra", "abracadabra" ) == 0 ); |
|---|
| 385 |
} |
|---|
| 386 |
} |
|---|
| 387 |
} |
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 391 |
// KMP Find |
|---|
| 392 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
version( TangoDoc ) |
|---|
| 396 |
{ |
|---|
| 397 |
/** |
|---|
| 398 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 399 |
* the index of the first element matching pat, or buf.length if no match |
|---|
| 400 |
* was found. Comparisons will be performed using the supplied predicate |
|---|
| 401 |
* or '==' if none is supplied. |
|---|
| 402 |
* |
|---|
| 403 |
* This function uses the KMP algorithm and offers O(M+N) performance but |
|---|
| 404 |
* must allocate a temporary buffer of size pat.sizeof to do so. If it is |
|---|
| 405 |
* available on the target system, alloca will be used for the allocation, |
|---|
| 406 |
* otherwise a standard dynamic memory allocation will occur. |
|---|
| 407 |
* |
|---|
| 408 |
* Params: |
|---|
| 409 |
* buf = The array to search. |
|---|
| 410 |
* pat = The pattern to search for. |
|---|
| 411 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 412 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 413 |
* callable type. |
|---|
| 414 |
* |
|---|
| 415 |
* Returns: |
|---|
| 416 |
* The index of the first match or buf.length if no match was found. |
|---|
| 417 |
*/ |
|---|
| 418 |
size_t kfind( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
/** |
|---|
| 422 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 423 |
* the index of the first element matching pat, or buf.length if no match |
|---|
| 424 |
* was found. Comparisons will be performed using the supplied predicate |
|---|
| 425 |
* or '==' if none is supplied. |
|---|
| 426 |
* |
|---|
| 427 |
* This function uses the KMP algorithm and offers O(M+N) performance but |
|---|
| 428 |
* must allocate a temporary buffer of size pat.sizeof to do so. If it is |
|---|
| 429 |
* available on the target system, alloca will be used for the allocation, |
|---|
| 430 |
* otherwise a standard dynamic memory allocation will occur. |
|---|
| 431 |
* |
|---|
| 432 |
* Params: |
|---|
| 433 |
* buf = The array to search. |
|---|
| 434 |
* pat = The pattern to search for. |
|---|
| 435 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 436 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 437 |
* callable type. |
|---|
| 438 |
* |
|---|
| 439 |
* Returns: |
|---|
| 440 |
* The index of the first match or buf.length if no match was found. |
|---|
| 441 |
*/ |
|---|
| 442 |
size_t kfind( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); |
|---|
| 443 |
} |
|---|
| 444 |
else |
|---|
| 445 |
{ |
|---|
| 446 |
template kfind_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 447 |
{ |
|---|
| 448 |
static assert( isCallableType!(Pred) ); |
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 452 |
{ |
|---|
| 453 |
foreach( size_t pos, Elem cur; buf ) |
|---|
| 454 |
{ |
|---|
| 455 |
if( pred( cur, pat ) ) |
|---|
| 456 |
return pos; |
|---|
| 457 |
} |
|---|
| 458 |
return buf.length; |
|---|
| 459 |
} |
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
size_t fn( Elem[] buf, Elem[] pat, Pred pred = Pred.init ) |
|---|
| 463 |
{ |
|---|
| 464 |
if( buf.length == 0 || |
|---|
| 465 |
pat.length == 0 || |
|---|
| 466 |
buf.length < pat.length ) |
|---|
| 467 |
{ |
|---|
| 468 |
return buf.length; |
|---|
| 469 |
} |
|---|
| 470 |
|
|---|
| 471 |
static if( is( alloca ) ) // always false, alloca usage should be rethought |
|---|
| 472 |
{ |
|---|
| 473 |
size_t[] func = (cast(size_t*) alloca( (pat.length + 1) * size_t.sizeof ))[0 .. pat.length + 1]; |
|---|
| 474 |
} |
|---|
| 475 |
else |
|---|
| 476 |
{ |
|---|
| 477 |
size_t[] func = new size_t[pat.length + 1]; |
|---|
| 478 |
scope( exit ) delete func; // force cleanup |
|---|
| 479 |
} |
|---|
| 480 |
|
|---|
| 481 |
func[0] = 0; |
|---|
| 482 |
|
|---|
| 483 |
// |
|---|
| 484 |
// building prefix-function |
|---|
| 485 |
// |
|---|
| 486 |
for( size_t m = 0, i = 1 ; i < pat.length ; ++i ) |
|---|
| 487 |
{ |
|---|
| 488 |
while( ( m > 0 ) && !pred( pat[m], pat[i] ) ) |
|---|
| 489 |
m = func[m - 1]; |
|---|
| 490 |
if( pred( pat[m], pat[i] ) ) |
|---|
| 491 |
++m; |
|---|
| 492 |
func[i] = m; |
|---|
| 493 |
} |
|---|
| 494 |
|
|---|
| 495 |
// |
|---|
| 496 |
// searching |
|---|
| 497 |
// |
|---|
| 498 |
for( size_t m = 0, i = 0; i < buf.length; ++i ) |
|---|
| 499 |
{ |
|---|
| 500 |
while( ( m > 0 ) && !pred( pat[m], buf[i] ) ) |
|---|
| 501 |
m = func[m - 1]; |
|---|
| 502 |
if( pred( pat[m], buf[i] ) ) |
|---|
| 503 |
{ |
|---|
| 504 |
++m; |
|---|
| 505 |
if( m == pat.length ) |
|---|
| 506 |
{ |
|---|
| 507 |
return i - pat.length + 1; |
|---|
| 508 |
} |
|---|
| 509 |
} |
|---|
| 510 |
} |
|---|
| 511 |
|
|---|
| 512 |
return buf.length; |
|---|
| 513 |
} |
|---|
| 514 |
} |
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
template kfind( Buf, Pat ) |
|---|
| 518 |
{ |
|---|
| 519 |
size_t kfind( Buf buf, Pat pat ) |
|---|
| 520 |
{ |
|---|
| 521 |
return kfind_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 522 |
} |
|---|
| 523 |
} |
|---|
| 524 |
|
|---|
| 525 |
|
|---|
| 526 |
template kfind( Buf, Pat, Pred ) |
|---|
| 527 |
{ |
|---|
| 528 |
size_t kfind( Buf buf, Pat pat, Pred pred ) |
|---|
| 529 |
{ |
|---|
| 530 |
return kfind_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 531 |
} |
|---|
| 532 |
} |
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
debug( UnitTest ) |
|---|
| 536 |
{ |
|---|
| 537 |
unittest |
|---|
| 538 |
{ |
|---|
| 539 |
// find element |
|---|
| 540 |
assert( kfind( "", 'a' ) == 0 ); |
|---|
| 541 |
assert( kfind( "abc", 'a' ) == 0 ); |
|---|
| 542 |
assert( kfind( "abc", 'b' ) == 1 ); |
|---|
| 543 |
assert( kfind( "abc", 'c' ) == 2 ); |
|---|
| 544 |
assert( kfind( "abc", 'd' ) == 3 ); |
|---|
| 545 |
|
|---|
| 546 |
// null parameters |
|---|
| 547 |
assert( kfind( "", "" ) == 0 ); |
|---|
| 548 |
assert( kfind( "a", "" ) == 1 ); |
|---|
| 549 |
assert( kfind( "", "a" ) == 0 ); |
|---|
| 550 |
|
|---|
| 551 |
// exact match |
|---|
| 552 |
assert( kfind( "abc", "abc" ) == 0 ); |
|---|
| 553 |
|
|---|
| 554 |
// simple substring match |
|---|
| 555 |
assert( kfind( "abc", "a" ) == 0 ); |
|---|
| 556 |
assert( kfind( "abca", "a" ) == 0 ); |
|---|
| 557 |
assert( kfind( "abc", "b" ) == 1 ); |
|---|
| 558 |
assert( kfind( "abc", "c" ) == 2 ); |
|---|
| 559 |
assert( kfind( "abc", "d" ) == 3 ); |
|---|
| 560 |
|
|---|
| 561 |
// multi-char substring match |
|---|
| 562 |
assert( kfind( "abc", "ab" ) == 0 ); |
|---|
| 563 |
assert( kfind( "abcab", "ab" ) == 0 ); |
|---|
| 564 |
assert( kfind( "abc", "bc" ) == 1 ); |
|---|
| 565 |
assert( kfind( "abc", "ac" ) == 3 ); |
|---|
| 566 |
assert( kfind( "abrabracadabra", "abracadabra" ) == 3 ); |
|---|
| 567 |
} |
|---|
| 568 |
} |
|---|
| 569 |
} |
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 573 |
// KMP Reverse Find |
|---|
| 574 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
version( TangoDoc ) |
|---|
| 578 |
{ |
|---|
| 579 |
/** |
|---|
| 580 |
* Performs a linear scan of buf from $(LP)buf.length .. 0$(RB), returning |
|---|
| 581 |
* the index of the first element matching pat, or buf.length if no match |
|---|
| 582 |
* was found. Comparisons will be performed using the supplied predicate |
|---|
| 583 |
* or '==' if none is supplied. |
|---|
| 584 |
* |
|---|
| 585 |
* This function uses the KMP algorithm and offers O(M+N) performance but |
|---|
| 586 |
* must allocate a temporary buffer of size pat.sizeof to do so. If it is |
|---|
| 587 |
* available on the target system, alloca will be used for the allocation, |
|---|
| 588 |
* otherwise a standard dynamic memory allocation will occur. |
|---|
| 589 |
* |
|---|
| 590 |
* Params: |
|---|
| 591 |
* buf = The array to search. |
|---|
| 592 |
* pat = The pattern to search for. |
|---|
| 593 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 594 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 595 |
* callable type. |
|---|
| 596 |
* |
|---|
| 597 |
* Returns: |
|---|
| 598 |
* The index of the first match or buf.length if no match was found. |
|---|
| 599 |
*/ |
|---|
| 600 |
size_t krfind( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 601 |
|
|---|
| 602 |
|
|---|
| 603 |
/** |
|---|
| 604 |
* Performs a linear scan of buf from $(LP)buf.length .. 0$(RB), returning |
|---|
| 605 |
* the index of the first element matching pat, or buf.length if no match |
|---|
| 606 |
* was found. Comparisons will be performed using the supplied predicate |
|---|
| 607 |
* or '==' if none is supplied. |
|---|
| 608 |
* |
|---|
| 609 |
* This function uses the KMP algorithm and offers O(M+N) performance but |
|---|
| 610 |
* must allocate a temporary buffer of size pat.sizeof to do so. If it is |
|---|
| 611 |
* available on the target system, alloca will be used for the allocation, |
|---|
| 612 |
* otherwise a standard dynamic memory allocation will occur. |
|---|
| 613 |
* |
|---|
| 614 |
* Params: |
|---|
| 615 |
* buf = The array to search. |
|---|
| 616 |
* pat = The pattern to search for. |
|---|
| 617 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 618 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 619 |
* callable type. |
|---|
| 620 |
* |
|---|
| 621 |
* Returns: |
|---|
| 622 |
* The index of the first match or buf.length if no match was found. |
|---|
| 623 |
*/ |
|---|
| 624 |
size_t krfind( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); |
|---|
| 625 |
} |
|---|
| 626 |
else |
|---|
| 627 |
{ |
|---|
| 628 |
template krfind_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 629 |
{ |
|---|
| 630 |
static assert( isCallableType!(Pred) ); |
|---|
| 631 |
|
|---|
| 632 |
|
|---|
| 633 |
size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 634 |
{ |
|---|
| 635 |
if( buf.length == 0 ) |
|---|
| 636 |
return buf.length; |
|---|
| 637 |
|
|---|
| 638 |
size_t pos = buf.length; |
|---|
| 639 |
|
|---|
| 640 |
do |
|---|
| 641 |
{ |
|---|
| 642 |
if( pred( buf[--pos], pat ) ) |
|---|
| 643 |
return pos; |
|---|
| 644 |
} while( pos > 0 ); |
|---|
| 645 |
return buf.length; |
|---|
| 646 |
} |
|---|
| 647 |
|
|---|
| 648 |
|
|---|
| 649 |
size_t fn( Elem[] buf, Elem[] pat, Pred pred = Pred.init ) |
|---|
| 650 |
{ |
|---|
| 651 |
if( buf.length == 0 || |
|---|
| 652 |
pat.length == 0 || |
|---|
| 653 |
buf.length < pat.length ) |
|---|
| 654 |
{ |
|---|
| 655 |
return buf.length; |
|---|
| 656 |
} |
|---|
| 657 |
|
|---|
| 658 |
static if( is( alloca ) ) // always false, alloca usage should be rethought |
|---|
| 659 |
{ |
|---|
| 660 |
size_t[] func = (cast(size_t*) alloca( (pat.length + 1) * size_t.sizeof ))[0 .. pat.length + 1]; |
|---|
| 661 |
} |
|---|
| 662 |
else |
|---|
| 663 |
{ |
|---|
| 664 |
size_t[] func = new size_t[pat.length + 1]; |
|---|
| 665 |
scope( exit ) delete func; // force cleanup |
|---|
| 666 |
} |
|---|
| 667 |
|
|---|
| 668 |
func[$ - 1] = 0; |
|---|
| 669 |
|
|---|
| 670 |
// |
|---|
| 671 |
// building prefix-function |
|---|
| 672 |
// |
|---|
| 673 |
for( size_t m = 0, i = pat.length - 1; i > 0; --i ) |
|---|
| 674 |
{ |
|---|
| 675 |
while( ( m > 0 ) && !pred( pat[length - m - 1], pat[i - 1] ) ) |
|---|
| 676 |
m = func[length - m]; |
|---|
| 677 |
if( pred( pat[length - m - 1], pat[i - 1] ) ) |
|---|
| 678 |
++m; |
|---|
| 679 |
func[i - 1] = m; |
|---|
| 680 |
} |
|---|
| 681 |
|
|---|
| 682 |
// |
|---|
| 683 |
// searching |
|---|
| 684 |
// |
|---|
| 685 |
size_t m = 0; |
|---|
| 686 |
size_t i = buf.length; |
|---|
| 687 |
do |
|---|
| 688 |
{ |
|---|
| 689 |
--i; |
|---|
| 690 |
while( ( m > 0 ) && !pred( pat[length - m - 1], buf[i] ) ) |
|---|
| 691 |
m = func[length - m - 1]; |
|---|
| 692 |
if( pred( pat[length - m - 1], buf[i] ) ) |
|---|
| 693 |
{ |
|---|
| 694 |
++m; |
|---|
| 695 |
if ( m == pat.length ) |
|---|
| 696 |
{ |
|---|
| 697 |
return i; |
|---|
| 698 |
} |
|---|
| 699 |
} |
|---|
| 700 |
} while( i > 0 ); |
|---|
| 701 |
|
|---|
| 702 |
return buf.length; |
|---|
| 703 |
} |
|---|
| 704 |
} |
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
template krfind( Buf, Pat ) |
|---|
| 708 |
{ |
|---|
| 709 |
size_t krfind( Buf buf, Pat pat ) |
|---|
| 710 |
{ |
|---|
| 711 |
return krfind_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 712 |
} |
|---|
| 713 |
} |
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
template krfind( Buf, Pat, Pred ) |
|---|
| 717 |
{ |
|---|
| 718 |
size_t krfind( Buf buf, Pat pat, Pred pred ) |
|---|
| 719 |
{ |
|---|
| 720 |
return krfind_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 721 |
} |
|---|
| 722 |
} |
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
debug( UnitTest ) |
|---|
| 726 |
{ |
|---|
| 727 |
unittest |
|---|
| 728 |
{ |
|---|
| 729 |
// rfind element |
|---|
| 730 |
assert( krfind( "", 'a' ) == 0 ); |
|---|
| 731 |
assert( krfind( "abc", 'a' ) == 0 ); |
|---|
| 732 |
assert( krfind( "abc", 'b' ) == 1 ); |
|---|
| 733 |
assert( krfind( "abc", 'c' ) == 2 ); |
|---|
| 734 |
assert( krfind( "abc", 'd' ) == 3 ); |
|---|
| 735 |
|
|---|
| 736 |
// null parameters |
|---|
| 737 |
assert( krfind( "", "" ) == 0 ); |
|---|
| 738 |
assert( krfind( "a", "" ) == 1 ); |
|---|
| 739 |
assert( krfind( "", "a" ) == 0 ); |
|---|
| 740 |
|
|---|
| 741 |
// exact match |
|---|
| 742 |
assert( krfind( "abc", "abc" ) == 0 ); |
|---|
| 743 |
|
|---|
| 744 |
// simple substring match |
|---|
| 745 |
assert( krfind( "abc", "a" ) == 0 ); |
|---|
| 746 |
assert( krfind( "abca", "a" ) == 3 ); |
|---|
| 747 |
assert( krfind( "abc", "b" ) == 1 ); |
|---|
| 748 |
assert( krfind( "abc", "c" ) == 2 ); |
|---|
| 749 |
assert( krfind( "abc", "d" ) == 3 ); |
|---|
| 750 |
|
|---|
| 751 |
// multi-char substring match |
|---|
| 752 |
assert( krfind( "abc", "ab" ) == 0 ); |
|---|
| 753 |
assert( krfind( "abcab", "ab" ) == 3 ); |
|---|
| 754 |
assert( krfind( "abc", "bc" ) == 1 ); |
|---|
| 755 |
assert( krfind( "abc", "ac" ) == 3 ); |
|---|
| 756 |
assert( krfind( "abracadabrabra", "abracadabra" ) == 0 ); |
|---|
| 757 |
} |
|---|
| 758 |
} |
|---|
| 759 |
} |
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 763 |
// Find-If |
|---|
| 764 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
version( TangoDoc ) |
|---|
| 768 |
{ |
|---|
| 769 |
/** |
|---|
| 770 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 771 |
* the index of the first element where pred returns true. |
|---|
| 772 |
* |
|---|
| 773 |
* Params: |
|---|
| 774 |
* buf = The array to search. |
|---|
| 775 |
* pred = The evaluation predicate, which should return true if the |
|---|
| 776 |
* element is a valid match and false if not. This predicate |
|---|
| 777 |
* may be any callable type. |
|---|
| 778 |
* |
|---|
| 779 |
* Returns: |
|---|
| 780 |
* The index of the first match or buf.length if no match was found. |
|---|
| 781 |
*/ |
|---|
| 782 |
size_t findIf( Elem[] buf, Pred1E pred ); |
|---|
| 783 |
} |
|---|
| 784 |
else |
|---|
| 785 |
{ |
|---|
| 786 |
template findIf_( Elem, Pred ) |
|---|
| 787 |
{ |
|---|
| 788 |
static assert( isCallableType!(Pred) ); |
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
size_t fn( Elem[] buf, Pred pred ) |
|---|
| 792 |
{ |
|---|
| 793 |
foreach( size_t pos, Elem cur; buf ) |
|---|
| 794 |
{ |
|---|
| 795 |
if( pred( cur ) ) |
|---|
| 796 |
return pos; |
|---|
| 797 |
} |
|---|
| 798 |
return buf.length; |
|---|
| 799 |
} |
|---|
| 800 |
} |
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
template findIf( Buf, Pred ) |
|---|
| 804 |
{ |
|---|
| 805 |
size_t findIf( Buf buf, Pred pred ) |
|---|
| 806 |
{ |
|---|
| 807 |
return findIf_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 808 |
} |
|---|
| 809 |
} |
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 |
debug( UnitTest ) |
|---|
| 813 |
{ |
|---|
| 814 |
unittest |
|---|
| 815 |
{ |
|---|
| 816 |
assert( findIf( "bcecg", ( char c ) { return c == 'a'; } ) == 5 ); |
|---|
| 817 |
assert( findIf( "bcecg", ( char c ) { return c == 'b'; } ) == 0 ); |
|---|
| 818 |
assert( findIf( "bcecg", ( char c ) { return c == 'c'; } ) == 1 ); |
|---|
| 819 |
assert( findIf( "bcecg", ( char c ) { return c == 'd'; } ) == 5 ); |
|---|
| 820 |
assert( findIf( "bcecg", ( char c ) { return c == 'g'; } ) == 4 ); |
|---|
| 821 |
assert( findIf( "bcecg", ( char c ) { return c == 'h'; } ) == 5 ); |
|---|
| 822 |
} |
|---|
| 823 |
} |
|---|
| 824 |
} |
|---|
| 825 |
|
|---|
| 826 |
|
|---|
| 827 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 828 |
// Reverse Find-If |
|---|
| 829 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 830 |
|
|---|
| 831 |
|
|---|
| 832 |
version( TangoDoc ) |
|---|
| 833 |
{ |
|---|
| 834 |
/** |
|---|
| 835 |
* Performs a linear scan of buf from $(LP)buf.length .. 0$(RB), returning |
|---|
| 836 |
* the index of the first element where pred returns true. |
|---|
| 837 |
* |
|---|
| 838 |
* Params: |
|---|
| 839 |
* buf = The array to search. |
|---|
| 840 |
* pred = The evaluation predicate, which should return true if the |
|---|
| 841 |
* element is a valid match and false if not. This predicate |
|---|
| 842 |
* may be any callable type. |
|---|
| 843 |
* |
|---|
| 844 |
* Returns: |
|---|
| 845 |
* The index of the first match or buf.length if no match was found. |
|---|
| 846 |
*/ |
|---|
| 847 |
size_t rfindIf( Elem[] buf, Pred1E pred ); |
|---|
| 848 |
} |
|---|
| 849 |
else |
|---|
| 850 |
{ |
|---|
| 851 |
template rfindIf_( Elem, Pred ) |
|---|
| 852 |
{ |
|---|
| 853 |
static assert( isCallableType!(Pred) ); |
|---|
| 854 |
|
|---|
| 855 |
|
|---|
| 856 |
size_t fn( Elem[] buf, Pred pred ) |
|---|
| 857 |
{ |
|---|
| 858 |
if( buf.length == 0 ) |
|---|
| 859 |
return buf.length; |
|---|
| 860 |
|
|---|
| 861 |
size_t pos = buf.length; |
|---|
| 862 |
|
|---|
| 863 |
do |
|---|
| 864 |
{ |
|---|
| 865 |
if( pred( buf[--pos] ) ) |
|---|
| 866 |
return pos; |
|---|
| 867 |
} while( pos > 0 ); |
|---|
| 868 |
return buf.length; |
|---|
| 869 |
} |
|---|
| 870 |
} |
|---|
| 871 |
|
|---|
| 872 |
|
|---|
| 873 |
template rfindIf( Buf, Pred ) |
|---|
| 874 |
{ |
|---|
| 875 |
size_t rfindIf( Buf buf, Pred pred ) |
|---|
| 876 |
{ |
|---|
| 877 |
return rfindIf_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 878 |
} |
|---|
| 879 |
} |
|---|
| 880 |
|
|---|
| 881 |
|
|---|
| 882 |
debug( UnitTest ) |
|---|
| 883 |
{ |
|---|
| 884 |
unittest |
|---|
| 885 |
{ |
|---|
| 886 |
assert( rfindIf( "bcecg", ( char c ) { return c == 'a'; } ) == 5 ); |
|---|
| 887 |
assert( rfindIf( "bcecg", ( char c ) { return c == 'b'; } ) == 0 ); |
|---|
| 888 |
assert( rfindIf( "bcecg", ( char c ) { return c == 'c'; } ) == 3 ); |
|---|
| 889 |
assert( rfindIf( "bcecg", ( char c ) { return c == 'd'; } ) == 5 ); |
|---|
| 890 |
assert( rfindIf( "bcecg", ( char c ) { return c == 'g'; } ) == 4 ); |
|---|
| 891 |
assert( rfindIf( "bcecg", ( char c ) { return c == 'h'; } ) == 5 ); |
|---|
| 892 |
} |
|---|
| 893 |
} |
|---|
| 894 |
} |
|---|
| 895 |
|
|---|
| 896 |
|
|---|
| 897 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 898 |
// Find Adjacent |
|---|
| 899 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 900 |
|
|---|
| 901 |
|
|---|
| 902 |
version( TangoDoc ) |
|---|
| 903 |
{ |
|---|
| 904 |
/** |
|---|
| 905 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 906 |
* the index of the first element that compares equal to the next element |
|---|
| 907 |
* in the sequence. Comparisons will be performed using the supplied |
|---|
| 908 |
* predicate or '==' if none is supplied. |
|---|
| 909 |
* |
|---|
| 910 |
* Params: |
|---|
| 911 |
* buf = The array to scan. |
|---|
| 912 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 913 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 914 |
* callable type. |
|---|
| 915 |
* |
|---|
| 916 |
* Returns: |
|---|
| 917 |
* The index of the first match or buf.length if no match was found. |
|---|
| 918 |
*/ |
|---|
| 919 |
size_t findAdj( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 920 |
|
|---|
| 921 |
} |
|---|
| 922 |
else |
|---|
| 923 |
{ |
|---|
| 924 |
template findAdj_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 925 |
{ |
|---|
| 926 |
static assert( isCallableType!(Pred) ); |
|---|
| 927 |
|
|---|
| 928 |
|
|---|
| 929 |
size_t fn( Elem[] buf, Pred pred = Pred.init ) |
|---|
| 930 |
{ |
|---|
| 931 |
if( buf.length < 2 ) |
|---|
| 932 |
return buf.length; |
|---|
| 933 |
|
|---|
| 934 |
Elem sav = buf[0]; |
|---|
| 935 |
|
|---|
| 936 |
foreach( size_t pos, Elem cur; buf[1 .. $] ) |
|---|
| 937 |
{ |
|---|
| 938 |
if( pred( cur, sav ) ) |
|---|
| 939 |
return pos; |
|---|
| 940 |
sav = cur; |
|---|
| 941 |
} |
|---|
| 942 |
return buf.length; |
|---|
| 943 |
} |
|---|
| 944 |
} |
|---|
| 945 |
|
|---|
| 946 |
|
|---|
| 947 |
template findAdj( Buf ) |
|---|
| 948 |
{ |
|---|
| 949 |
size_t findAdj( Buf buf ) |
|---|
| 950 |
{ |
|---|
| 951 |
return findAdj_!(ElemTypeOf!(Buf)).fn( buf ); |
|---|
| 952 |
} |
|---|
| 953 |
} |
|---|
| 954 |
|
|---|
| 955 |
|
|---|
| 956 |
template findAdj( Buf, Pred ) |
|---|
| 957 |
{ |
|---|
| 958 |
size_t findAdj( Buf buf, Pred pred ) |
|---|
| 959 |
{ |
|---|
| 960 |
return findAdj_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 961 |
} |
|---|
| 962 |
} |
|---|
| 963 |
|
|---|
| 964 |
|
|---|
| 965 |
debug( UnitTest ) |
|---|
| 966 |
{ |
|---|
| 967 |
unittest |
|---|
| 968 |
{ |
|---|
| 969 |
assert( findAdj( "aabcdef" ) == 0 ); |
|---|
| 970 |
assert( findAdj( "abcddef" ) == 3 ); |
|---|
| 971 |
assert( findAdj( "abcdeff" ) == 5 ); |
|---|
| 972 |
assert( findAdj( "abcdefg" ) == 7 ); |
|---|
| 973 |
} |
|---|
| 974 |
} |
|---|
| 975 |
} |
|---|
| 976 |
|
|---|
| 977 |
|
|---|
| 978 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 979 |
// Contains |
|---|
| 980 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 981 |
|
|---|
| 982 |
|
|---|
| 983 |
version( TangoDoc ) |
|---|
| 984 |
{ |
|---|
| 985 |
/** |
|---|
| 986 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 987 |
* true if an element matching pat is found. Comparisons will be performed |
|---|
| 988 |
* using the supplied predicate or '<' if none is supplied. |
|---|
| 989 |
* |
|---|
| 990 |
* Params: |
|---|
| 991 |
* buf = The array to search. |
|---|
| 992 |
* pat = The pattern to search for. |
|---|
| 993 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 994 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 995 |
* callable type. |
|---|
| 996 |
* |
|---|
| 997 |
* Returns: |
|---|
| 998 |
* True if an element equivalent to pat is found, false if not. |
|---|
| 999 |
*/ |
|---|
| 1000 |
equals_t contains( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 1001 |
|
|---|
| 1002 |
|
|---|
| 1003 |
/** |
|---|
| 1004 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 1005 |
* true if a sequence matching pat is found. Comparisons will be performed |
|---|
| 1006 |
* using the supplied predicate or '<' if none is supplied. |
|---|
| 1007 |
* |
|---|
| 1008 |
* Params: |
|---|
| 1009 |
* buf = The array to search. |
|---|
| 1010 |
* pat = The pattern to search for. |
|---|
| 1011 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 1012 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 1013 |
* callable type. |
|---|
| 1014 |
* |
|---|
| 1015 |
* Returns: |
|---|
| 1016 |
* True if an element equivalent to pat is found, false if not. |
|---|
| 1017 |
*/ |
|---|
| 1018 |
equals_t contains( Elem[] buf, Elem[] pat, Pred2E pred = Pred2E.init ); |
|---|
| 1019 |
} |
|---|
| 1020 |
else |
|---|
| 1021 |
{ |
|---|
| 1022 |
template contains( Buf, Pat ) |
|---|
| 1023 |
{ |
|---|
| 1024 |
equals_t contains( Buf buf, Pat pat ) |
|---|
| 1025 |
{ |
|---|
| 1026 |
return cast(equals_t)(find( buf, pat ) != buf.length); |
|---|
| 1027 |
} |
|---|
| 1028 |
} |
|---|
| 1029 |
|
|---|
| 1030 |
|
|---|
| 1031 |
template contains( Buf, Pat, Pred ) |
|---|
| 1032 |
{ |
|---|
| 1033 |
equals_t contains( Buf buf, Pat pat, Pred pred ) |
|---|
| 1034 |
{ |
|---|
| 1035 |
return cast(equals_t)(find( buf, pat, pred ) != buf.length); |
|---|
| 1036 |
} |
|---|
| 1037 |
} |
|---|
| 1038 |
|
|---|
| 1039 |
|
|---|
| 1040 |
debug( UnitTest ) |
|---|
| 1041 |
{ |
|---|
| 1042 |
unittest |
|---|
| 1043 |
{ |
|---|
| 1044 |
// find element |
|---|
| 1045 |
assert( !contains( "", 'a' ) ); |
|---|
| 1046 |
assert( contains( "abc", 'a' ) ); |
|---|
| 1047 |
assert( contains( "abc", 'b' ) ); |
|---|
| 1048 |
assert( contains( "abc", 'c' ) ); |
|---|
| 1049 |
assert( !contains( "abc", 'd' ) ); |
|---|
| 1050 |
|
|---|
| 1051 |
// null parameters |
|---|
| 1052 |
assert( !contains( "", "" ) ); |
|---|
| 1053 |
assert( !contains( "a", "" ) ); |
|---|
| 1054 |
assert( !contains( "", "a" ) ); |
|---|
| 1055 |
|
|---|
| 1056 |
// exact match |
|---|
| 1057 |
assert( contains( "abc", "abc" ) ); |
|---|
| 1058 |
|
|---|
| 1059 |
// simple substring match |
|---|
| 1060 |
assert( contains( "abc", "a" ) ); |
|---|
| 1061 |
assert( contains( "abca", "a" ) ); |
|---|
| 1062 |
assert( contains( "abc", "b" ) ); |
|---|
| 1063 |
assert( contains( "abc", "c" ) ); |
|---|
| 1064 |
assert( !contains( "abc", "d" ) ); |
|---|
| 1065 |
|
|---|
| 1066 |
// multi-char substring match |
|---|
| 1067 |
assert( contains( "abc", "ab" ) ); |
|---|
| 1068 |
assert( contains( "abcab", "ab" ) ); |
|---|
| 1069 |
assert( contains( "abc", "bc" ) ); |
|---|
| 1070 |
assert( !contains( "abc", "ac" ) ); |
|---|
| 1071 |
assert( contains( "abrabracadabra", "abracadabra" ) ); |
|---|
| 1072 |
} |
|---|
| 1073 |
} |
|---|
| 1074 |
} |
|---|
| 1075 |
|
|---|
| 1076 |
|
|---|
| 1077 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1078 |
// Mismatch |
|---|
| 1079 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1080 |
|
|---|
| 1081 |
|
|---|
| 1082 |
version( TangoDoc ) |
|---|
| 1083 |
{ |
|---|
| 1084 |
/** |
|---|
| 1085 |
* Performs a parallel linear scan of bufA and bufB from $(LB)0 .. N$(RP) |
|---|
| 1086 |
* where N = min$(LP)bufA.length, bufB.length$(RP), returning the index of |
|---|
| 1087 |
* the first element in bufA which does not match the corresponding element |
|---|
| 1088 |
* in bufB or N if no mismatch occurs. Comparisons will be performed using |
|---|
| 1089 |
* the supplied predicate or '==' if none is supplied. |
|---|
| 1090 |
* |
|---|
| 1091 |
* Params: |
|---|
| 1092 |
* bufA = The array to evaluate. |
|---|
| 1093 |
* bufB = The array to match against. |
|---|
| 1094 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 1095 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 1096 |
* callable type. |
|---|
| 1097 |
* |
|---|
| 1098 |
* Returns: |
|---|
| 1099 |
* The index of the first mismatch or N if the first N elements of bufA |
|---|
| 1100 |
* and bufB match, where N = min$(LP)bufA.length, bufB.length$(RP). |
|---|
| 1101 |
*/ |
|---|
| 1102 |
size_t mismatch( Elem[] bufA, Elem[] bufB, Pred2E pred = Pred2E.init ); |
|---|
| 1103 |
|
|---|
| 1104 |
} |
|---|
| 1105 |
else |
|---|
| 1106 |
{ |
|---|
| 1107 |
template mismatch_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 1108 |
{ |
|---|
| 1109 |
static assert( isCallableType!(Pred) ); |
|---|
| 1110 |
|
|---|
| 1111 |
|
|---|
| 1112 |
size_t fn( Elem[] bufA, Elem[] bufB, Pred pred = Pred.init ) |
|---|
| 1113 |
{ |
|---|
| 1114 |
size_t posA = 0, |
|---|
| 1115 |
posB = 0; |
|---|
| 1116 |
|
|---|
| 1117 |
while( posA < bufA.length && posB < bufB.length ) |
|---|
| 1118 |
{ |
|---|
| 1119 |
if( !pred( bufB[posB], bufA[posA] ) ) |
|---|
| 1120 |
break; |
|---|
| 1121 |
++posA, ++posB; |
|---|
| 1122 |
} |
|---|
| 1123 |
return posA; |
|---|
| 1124 |
} |
|---|
| 1125 |
} |
|---|
| 1126 |
|
|---|
| 1127 |
|
|---|
| 1128 |
template mismatch( BufA, BufB ) |
|---|
| 1129 |
{ |
|---|
| 1130 |
size_t mismatch( BufA bufA, BufB bufB ) |
|---|
| 1131 |
{ |
|---|
| 1132 |
return mismatch_!(ElemTypeOf!(BufA)).fn( bufA, bufB ); |
|---|
| 1133 |
} |
|---|
| 1134 |
} |
|---|
| 1135 |
|
|---|
| 1136 |
|
|---|
| 1137 |
template mismatch( BufA, BufB, Pred ) |
|---|
| 1138 |
{ |
|---|
| 1139 |
size_t mismatch( BufA bufA, BufB bufB, Pred pred ) |
|---|
| 1140 |
{ |
|---|
| 1141 |
return mismatch_!(ElemTypeOf!(BufA), Pred).fn( bufA, bufB, pred ); |
|---|
| 1142 |
} |
|---|
| 1143 |
} |
|---|
| 1144 |
|
|---|
| 1145 |
debug( UnitTest ) |
|---|
| 1146 |
{ |
|---|
| 1147 |
unittest |
|---|
| 1148 |
{ |
|---|
| 1149 |
assert( mismatch( "a", "abcdefg" ) == 1 ); |
|---|
| 1150 |
assert( mismatch( "abcdefg", "a" ) == 1 ); |
|---|
| 1151 |
|
|---|
| 1152 |
assert( mismatch( "x", "abcdefg" ) == 0 ); |
|---|
| 1153 |
assert( mismatch( "abcdefg", "x" ) == 0 ); |
|---|
| 1154 |
|
|---|
| 1155 |
assert( mismatch( "xbcdefg", "abcdefg" ) == 0 ); |
|---|
| 1156 |
assert( mismatch( "abcdefg", "xbcdefg" ) == 0 ); |
|---|
| 1157 |
|
|---|
| 1158 |
assert( mismatch( "abcxefg", "abcdefg" ) == 3 ); |
|---|
| 1159 |
assert( mismatch( "abcdefg", "abcxefg" ) == 3 ); |
|---|
| 1160 |
|
|---|
| 1161 |
assert( mismatch( "abcdefx", "abcdefg" ) == 6 ); |
|---|
| 1162 |
assert( mismatch( "abcdefg", "abcdefx" ) == 6 ); |
|---|
| 1163 |
} |
|---|
| 1164 |
} |
|---|
| 1165 |
} |
|---|
| 1166 |
|
|---|
| 1167 |
|
|---|
| 1168 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1169 |
// Count |
|---|
| 1170 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1171 |
|
|---|
| 1172 |
|
|---|
| 1173 |
version( TangoDoc ) |
|---|
| 1174 |
{ |
|---|
| 1175 |
/** |
|---|
| 1176 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 1177 |
* a count of the number of elements matching pat. Comparisons will be |
|---|
| 1178 |
* performed using the supplied predicate or '==' if none is supplied. |
|---|
| 1179 |
* |
|---|
| 1180 |
* Params: |
|---|
| 1181 |
* buf = The array to scan. |
|---|
| 1182 |
* pat = The pattern to match. |
|---|
| 1183 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 1184 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 1185 |
* callable type. |
|---|
| 1186 |
* |
|---|
| 1187 |
* Returns: |
|---|
| 1188 |
* The number of elements matching pat. |
|---|
| 1189 |
*/ |
|---|
| 1190 |
size_t count( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 1191 |
|
|---|
| 1192 |
} |
|---|
| 1193 |
else |
|---|
| 1194 |
{ |
|---|
| 1195 |
template count_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 1196 |
{ |
|---|
| 1197 |
static assert( isCallableType!(Pred) ); |
|---|
| 1198 |
|
|---|
| 1199 |
|
|---|
| 1200 |
size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 1201 |
{ |
|---|
| 1202 |
size_t cnt = 0; |
|---|
| 1203 |
|
|---|
| 1204 |
foreach( size_t pos, Elem cur; buf ) |
|---|
| 1205 |
{ |
|---|
| 1206 |
if( pred( cur, pat ) ) |
|---|
| 1207 |
++cnt; |
|---|
| 1208 |
} |
|---|
| 1209 |
return cnt; |
|---|
| 1210 |
} |
|---|
| 1211 |
} |
|---|
| 1212 |
|
|---|
| 1213 |
|
|---|
| 1214 |
template count( Buf, Pat ) |
|---|
| 1215 |
{ |
|---|
| 1216 |
size_t count( Buf buf, Pat pat ) |
|---|
| 1217 |
{ |
|---|
| 1218 |
return count_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 1219 |
} |
|---|
| 1220 |
} |
|---|
| 1221 |
|
|---|
| 1222 |
|
|---|
| 1223 |
template count( Buf, Pat, Pred ) |
|---|
| 1224 |
{ |
|---|
| 1225 |
size_t count( Buf buf, Pat pat, Pred pred ) |
|---|
| 1226 |
{ |
|---|
| 1227 |
return count_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 1228 |
} |
|---|
| 1229 |
} |
|---|
| 1230 |
|
|---|
| 1231 |
|
|---|
| 1232 |
debug( UnitTest ) |
|---|
| 1233 |
{ |
|---|
| 1234 |
unittest |
|---|
| 1235 |
{ |
|---|
| 1236 |
assert( count( "gbbbi", 'a' ) == 0 ); |
|---|
| 1237 |
assert( count( "gbbbi", 'g' ) == 1 ); |
|---|
| 1238 |
assert( count( "gbbbi", 'b' ) == 3 ); |
|---|
| 1239 |
assert( count( "gbbbi", 'i' ) == 1 ); |
|---|
| 1240 |
assert( count( "gbbbi", 'd' ) == 0 ); |
|---|
| 1241 |
} |
|---|
| 1242 |
} |
|---|
| 1243 |
} |
|---|
| 1244 |
|
|---|
| 1245 |
|
|---|
| 1246 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1247 |
// Count-If |
|---|
| 1248 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1249 |
|
|---|
| 1250 |
|
|---|
| 1251 |
version( TangoDoc ) |
|---|
| 1252 |
{ |
|---|
| 1253 |
/** |
|---|
| 1254 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), returning |
|---|
| 1255 |
* a count of the number of elements where pred returns true. |
|---|
| 1256 |
* |
|---|
| 1257 |
* Params: |
|---|
| 1258 |
* buf = The array to scan. |
|---|
| 1259 |
* pred = The evaluation predicate, which should return true if the |
|---|
| 1260 |
* element is a valid match and false if not. This predicate |
|---|
| 1261 |
* may be any callable type. |
|---|
| 1262 |
* |
|---|
| 1263 |
* Returns: |
|---|
| 1264 |
* The number of elements where pred returns true. |
|---|
| 1265 |
*/ |
|---|
| 1266 |
size_t countIf( Elem[] buf, Pred1E pred = Pred1E.init ); |
|---|
| 1267 |
|
|---|
| 1268 |
} |
|---|
| 1269 |
else |
|---|
| 1270 |
{ |
|---|
| 1271 |
template countIf_( Elem, Pred ) |
|---|
| 1272 |
{ |
|---|
| 1273 |
static assert( isCallableType!(Pred) ); |
|---|
| 1274 |
|
|---|
| 1275 |
|
|---|
| 1276 |
size_t fn( Elem[] buf, Pred pred ) |
|---|
| 1277 |
{ |
|---|
| 1278 |
size_t cnt = 0; |
|---|
| 1279 |
|
|---|
| 1280 |
foreach( size_t pos, Elem cur; buf ) |
|---|
| 1281 |
{ |
|---|
| 1282 |
if( pred( cur ) ) |
|---|
| 1283 |
++cnt; |
|---|
| 1284 |
} |
|---|
| 1285 |
return cnt; |
|---|
| 1286 |
} |
|---|
| 1287 |
} |
|---|
| 1288 |
|
|---|
| 1289 |
|
|---|
| 1290 |
template countIf( Buf, Pred ) |
|---|
| 1291 |
{ |
|---|
| 1292 |
size_t countIf( Buf buf, Pred pred ) |
|---|
| 1293 |
{ |
|---|
| 1294 |
return countIf_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 1295 |
} |
|---|
| 1296 |
} |
|---|
| 1297 |
|
|---|
| 1298 |
|
|---|
| 1299 |
debug( UnitTest ) |
|---|
| 1300 |
{ |
|---|
| 1301 |
unittest |
|---|
| 1302 |
{ |
|---|
| 1303 |
assert( countIf( "gbbbi", ( char c ) { return c == 'a'; } ) == 0 ); |
|---|
| 1304 |
assert( countIf( "gbbbi", ( char c ) { return c == 'g'; } ) == 1 ); |
|---|
| 1305 |
assert( countIf( "gbbbi", ( char c ) { return c == 'b'; } ) == 3 ); |
|---|
| 1306 |
assert( countIf( "gbbbi", ( char c ) { return c == 'i'; } ) == 1 ); |
|---|
| 1307 |
assert( countIf( "gbbbi", ( char c ) { return c == 'd'; } ) == 0 ); |
|---|
| 1308 |
} |
|---|
| 1309 |
} |
|---|
| 1310 |
} |
|---|
| 1311 |
|
|---|
| 1312 |
|
|---|
| 1313 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1314 |
// Replace |
|---|
| 1315 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1316 |
|
|---|
| 1317 |
|
|---|
| 1318 |
version( TangoDoc ) |
|---|
| 1319 |
{ |
|---|
| 1320 |
/** |
|---|
| 1321 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), replacing |
|---|
| 1322 |
* occurrences of pat with val. Comparisons will be performed using the |
|---|
| 1323 |
* supplied predicate or '==' if none is supplied. |
|---|
| 1324 |
* |
|---|
| 1325 |
* Params: |
|---|
| 1326 |
* buf = The array to scan. |
|---|
| 1327 |
* pat = The pattern to match. |
|---|
| 1328 |
* val = The value to substitute. |
|---|
| 1329 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 1330 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 1331 |
* callable type. |
|---|
| 1332 |
* |
|---|
| 1333 |
* Returns: |
|---|
| 1334 |
* The number of elements replaced. |
|---|
| 1335 |
*/ |
|---|
| 1336 |
size_t replace( Elem[] buf, Elem pat, Elem val, Pred2E pred = Pred2E.init ); |
|---|
| 1337 |
|
|---|
| 1338 |
} |
|---|
| 1339 |
else |
|---|
| 1340 |
{ |
|---|
| 1341 |
template replace_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 1342 |
{ |
|---|
| 1343 |
static assert( isCallableType!(Pred) ); |
|---|
| 1344 |
|
|---|
| 1345 |
|
|---|
| 1346 |
size_t fn( Elem[] buf, Elem pat, Elem val, Pred pred = Pred.init ) |
|---|
| 1347 |
{ |
|---|
| 1348 |
size_t cnt = 0; |
|---|
| 1349 |
|
|---|
| 1350 |
foreach( size_t pos, ref Elem cur; buf ) |
|---|
| 1351 |
{ |
|---|
| 1352 |
if( pred( cur, pat ) ) |
|---|
| 1353 |
{ |
|---|
| 1354 |
cur = val; |
|---|
| 1355 |
++cnt; |
|---|
| 1356 |
} |
|---|
| 1357 |
} |
|---|
| 1358 |
return cnt; |
|---|
| 1359 |
} |
|---|
| 1360 |
} |
|---|
| 1361 |
|
|---|
| 1362 |
|
|---|
| 1363 |
template replace( Buf, Elem ) |
|---|
| 1364 |
{ |
|---|
| 1365 |
size_t replace( Buf buf, Elem pat, Elem val ) |
|---|
| 1366 |
{ |
|---|
| 1367 |
return replace_!(ElemTypeOf!(Buf)).fn( buf, pat, val ); |
|---|
| 1368 |
} |
|---|
| 1369 |
} |
|---|
| 1370 |
|
|---|
| 1371 |
|
|---|
| 1372 |
template replace( Buf, Elem, Pred ) |
|---|
| 1373 |
{ |
|---|
| 1374 |
size_t replace( Buf buf, Elem pat, Elem val, Pred pred ) |
|---|
| 1375 |
{ |
|---|
| 1376 |
return replace_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, val, pred ); |
|---|
| 1377 |
} |
|---|
| 1378 |
} |
|---|
| 1379 |
|
|---|
| 1380 |
|
|---|
| 1381 |
debug( UnitTest ) |
|---|
| 1382 |
{ |
|---|
| 1383 |
unittest |
|---|
| 1384 |
{ |
|---|
| 1385 |
assert( replace( "gbbbi".dup, 'a', 'b' ) == 0 ); |
|---|
| 1386 |
assert( replace( "gbbbi".dup, 'g', 'h' ) == 1 ); |
|---|
| 1387 |
assert( replace( "gbbbi".dup, 'b', 'c' ) == 3 ); |
|---|
| 1388 |
assert( replace( "gbbbi".dup, 'i', 'j' ) == 1 ); |
|---|
| 1389 |
assert( replace( "gbbbi".dup, 'd', 'e' ) == 0 ); |
|---|
| 1390 |
} |
|---|
| 1391 |
} |
|---|
| 1392 |
} |
|---|
| 1393 |
|
|---|
| 1394 |
|
|---|
| 1395 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1396 |
// Replace-If |
|---|
| 1397 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1398 |
|
|---|
| 1399 |
|
|---|
| 1400 |
version( TangoDoc ) |
|---|
| 1401 |
{ |
|---|
| 1402 |
/** |
|---|
| 1403 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), replacing |
|---|
| 1404 |
* elements where pred returns true with val. |
|---|
| 1405 |
* |
|---|
| 1406 |
* Params: |
|---|
| 1407 |
* buf = The array to scan. |
|---|
| 1408 |
* val = The value to substitute. |
|---|
| 1409 |
* pred = The evaluation predicate, which should return true if the |
|---|
| 1410 |
* element is a valid match and false if not. This predicate |
|---|
| 1411 |
* may be any callable type. |
|---|
| 1412 |
* |
|---|
| 1413 |
* Returns: |
|---|
| 1414 |
* The number of elements replaced. |
|---|
| 1415 |
*/ |
|---|
| 1416 |
size_t replaceIf( Elem[] buf, Elem val, Pred2E pred = Pred2E.init ); |
|---|
| 1417 |
|
|---|
| 1418 |
} |
|---|
| 1419 |
else |
|---|
| 1420 |
{ |
|---|
| 1421 |
template replaceIf_( Elem, Pred ) |
|---|
| 1422 |
{ |
|---|
| 1423 |
static assert( isCallableType!(Pred) ); |
|---|
| 1424 |
|
|---|
| 1425 |
|
|---|
| 1426 |
size_t fn( Elem[] buf, Elem val, Pred pred ) |
|---|
| 1427 |
{ |
|---|
| 1428 |
size_t cnt = 0; |
|---|
| 1429 |
|
|---|
| 1430 |
foreach( size_t pos, ref Elem cur; buf ) |
|---|
| 1431 |
{ |
|---|
| 1432 |
if( pred( cur ) ) |
|---|
| 1433 |
{ |
|---|
| 1434 |
cur = val; |
|---|
| 1435 |
++cnt; |
|---|
| 1436 |
} |
|---|
| 1437 |
} |
|---|
| 1438 |
return cnt; |
|---|
| 1439 |
} |
|---|
| 1440 |
} |
|---|
| 1441 |
|
|---|
| 1442 |
|
|---|
| 1443 |
template replaceIf( Buf, Elem, Pred ) |
|---|
| 1444 |
{ |
|---|
| 1445 |
size_t replaceIf( Buf buf, Elem val, Pred pred ) |
|---|
| 1446 |
{ |
|---|
| 1447 |
return replaceIf_!(ElemTypeOf!(Buf), Pred).fn( buf, val, pred ); |
|---|
| 1448 |
} |
|---|
| 1449 |
} |
|---|
| 1450 |
|
|---|
| 1451 |
|
|---|
| 1452 |
debug( UnitTest ) |
|---|
| 1453 |
{ |
|---|
| 1454 |
unittest |
|---|
| 1455 |
{ |
|---|
| 1456 |
assert( replaceIf( "gbbbi".dup, 'b', ( char c ) { return c == 'a'; } ) == 0 ); |
|---|
| 1457 |
assert( replaceIf( "gbbbi".dup, 'h', ( char c ) { return c == 'g'; } ) == 1 ); |
|---|
| 1458 |
assert( replaceIf( "gbbbi".dup, 'c', ( char c ) { return c == 'b'; } ) == 3 ); |
|---|
| 1459 |
assert( replaceIf( "gbbbi".dup, 'j', ( char c ) { return c == 'i'; } ) == 1 ); |
|---|
| 1460 |
assert( replaceIf( "gbbbi".dup, 'e', ( char c ) { return c == 'd'; } ) == 0 ); |
|---|
| 1461 |
} |
|---|
| 1462 |
} |
|---|
| 1463 |
} |
|---|
| 1464 |
|
|---|
| 1465 |
|
|---|
| 1466 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1467 |
// Remove |
|---|
| 1468 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1469 |
|
|---|
| 1470 |
|
|---|
| 1471 |
version( TangoDoc ) |
|---|
| 1472 |
{ |
|---|
| 1473 |
/** |
|---|
| 1474 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), moving all |
|---|
| 1475 |
* elements matching pat to the end of the sequence. The relative order of |
|---|
| 1476 |
* elements not matching pat will be preserved. Comparisons will be |
|---|
| 1477 |
* performed using the supplied predicate or '==' if none is supplied. |
|---|
| 1478 |
* |
|---|
| 1479 |
* Params: |
|---|
| 1480 |
* buf = The array to scan. This parameter is not marked 'ref' |
|---|
| 1481 |
* to allow temporary slices to be modified. As buf is not resized |
|---|
| 1482 |
* in any way, omitting the 'ref' qualifier has no effect on the |
|---|
| 1483 |
* result of this operation, even though it may be viewed as a |
|---|
| 1484 |
* side-effect. |
|---|
| 1485 |
* pat = The pattern to match against. |
|---|
| 1486 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 1487 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 1488 |
* callable type. |
|---|
| 1489 |
* |
|---|
| 1490 |
* Returns: |
|---|
| 1491 |
* The number of elements that do not match pat. |
|---|
| 1492 |
*/ |
|---|
| 1493 |
size_t remove( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 1494 |
|
|---|
| 1495 |
/** |
|---|
| 1496 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), moving all |
|---|
| 1497 |
* elements matching pat to the end of the sequence. The relative order of |
|---|
| 1498 |
* elements not matching pat will be preserved. Comparisons will be |
|---|
| 1499 |
* performed '=='. |
|---|
| 1500 |
* |
|---|
| 1501 |
* Params: |
|---|
| 1502 |
* buf = The array to scan. This parameter is not marked 'ref' |
|---|
| 1503 |
* to allow temporary slices to be modified. As buf is not resized |
|---|
| 1504 |
* in any way, omitting the 'ref' qualifier has no effect on the |
|---|
| 1505 |
* result of this operation, even though it may be viewed as a |
|---|
| 1506 |
* side-effect. |
|---|
| 1507 |
* pat = The pattern to match against. |
|---|
| 1508 |
* |
|---|
| 1509 |
* Returns: |
|---|
| 1510 |
* The number of elements that do not match pat. |
|---|
| 1511 |
*/ |
|---|
| 1512 |
size_t remove( Elem[] buf, Elem pat ); |
|---|
| 1513 |
} |
|---|
| 1514 |
else |
|---|
| 1515 |
{ |
|---|
| 1516 |
template remove_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 1517 |
{ |
|---|
| 1518 |
static assert( isCallableType!(Pred) ); |
|---|
| 1519 |
|
|---|
| 1520 |
|
|---|
| 1521 |
size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 1522 |
{ |
|---|
| 1523 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 1524 |
// not inline the reference-based version. |
|---|
| 1525 |
void exch( size_t p1, size_t p2 ) |
|---|
| 1526 |
{ |
|---|
| 1527 |
Elem t = buf[p1]; |
|---|
| 1528 |
buf[p1] = buf[p2]; |
|---|
| 1529 |
buf[p2] = t; |
|---|
| 1530 |
} |
|---|
| 1531 |
|
|---|
| 1532 |
size_t cnt = 0; |
|---|
| 1533 |
|
|---|
| 1534 |
for( size_t pos = 0, len = buf.length; pos < len; ++pos ) |
|---|
| 1535 |
{ |
|---|
| 1536 |
if( pred( buf[pos], pat ) ) |
|---|
| 1537 |
++cnt; |
|---|
| 1538 |
else |
|---|
| 1539 |
exch( pos, pos - cnt ); |
|---|
| 1540 |
} |
|---|
| 1541 |
return buf.length - cnt; |
|---|
| 1542 |
} |
|---|
| 1543 |
} |
|---|
| 1544 |
|
|---|
| 1545 |
|
|---|
| 1546 |
template remove( Buf, Pat ) |
|---|
| 1547 |
{ |
|---|
| 1548 |
size_t remove( Buf buf, Pat pat ) |
|---|
| 1549 |
{ |
|---|
| 1550 |
return remove_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 1551 |
} |
|---|
| 1552 |
} |
|---|
| 1553 |
|
|---|
| 1554 |
|
|---|
| 1555 |
template remove( Buf, Pat, Pred ) |
|---|
| 1556 |
{ |
|---|
| 1557 |
size_t remove( Buf buf, Pat pat, Pred pred ) |
|---|
| 1558 |
{ |
|---|
| 1559 |
return remove_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 1560 |
} |
|---|
| 1561 |
} |
|---|
| 1562 |
|
|---|
| 1563 |
|
|---|
| 1564 |
debug( UnitTest ) |
|---|
| 1565 |
{ |
|---|
| 1566 |
unittest |
|---|
| 1567 |
{ |
|---|
| 1568 |
void test( char[] buf, char pat, size_t num ) |
|---|
| 1569 |
{ |
|---|
| 1570 |
assert( remove( buf, pat ) == num ); |
|---|
| 1571 |
foreach( pos, cur; buf ) |
|---|
| 1572 |
{ |
|---|
| 1573 |
assert( pos < num ? cur != pat : cur == pat ); |
|---|
| 1574 |
} |
|---|
| 1575 |
} |
|---|
| 1576 |
|
|---|
| 1577 |
test( "abcdefghij".dup, 'x', 10 ); |
|---|
| 1578 |
test( "xabcdefghi".dup, 'x', 9 ); |
|---|
| 1579 |
test( "abcdefghix".dup, 'x', 9 ); |
|---|
| 1580 |
test( "abxxcdefgh".dup, 'x', 8 ); |
|---|
| 1581 |
test( "xaxbcdxxex".dup, 'x', 5 ); |
|---|
| 1582 |
} |
|---|
| 1583 |
} |
|---|
| 1584 |
} |
|---|
| 1585 |
|
|---|
| 1586 |
|
|---|
| 1587 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1588 |
// Remove-If |
|---|
| 1589 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1590 |
|
|---|
| 1591 |
|
|---|
| 1592 |
version( TangoDoc ) |
|---|
| 1593 |
{ |
|---|
| 1594 |
/** |
|---|
| 1595 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), moving all |
|---|
| 1596 |
* elements that satisfy pred to the end of the sequence. The relative |
|---|
| 1597 |
* order of elements that do not satisfy pred will be preserved. |
|---|
| 1598 |
* |
|---|
| 1599 |
* Params: |
|---|
| 1600 |
* buf = The array to scan. This parameter is not marked 'ref' |
|---|
| 1601 |
* to allow temporary slices to be modified. As buf is not resized |
|---|
| 1602 |
* in any way, omitting the 'ref' qualifier has no effect on the |
|---|
| 1603 |
* result of this operation, even though it may be viewed as a |
|---|
| 1604 |
* side-effect. |
|---|
| 1605 |
* pred = The evaluation predicate, which should return true if the |
|---|
| 1606 |
* element satisfies the condition and false if not. This |
|---|
| 1607 |
* predicate may be any callable type. |
|---|
| 1608 |
* |
|---|
| 1609 |
* Returns: |
|---|
| 1610 |
* The number of elements that do not satisfy pred. |
|---|
| 1611 |
*/ |
|---|
| 1612 |
size_t removeIf( Elem[] buf, Pred1E pred ); |
|---|
| 1613 |
} |
|---|
| 1614 |
else |
|---|
| 1615 |
{ |
|---|
| 1616 |
template removeIf_( Elem, Pred ) |
|---|
| 1617 |
{ |
|---|
| 1618 |
static assert( isCallableType!(Pred) ); |
|---|
| 1619 |
|
|---|
| 1620 |
|
|---|
| 1621 |
size_t fn( Elem[] buf, Pred pred ) |
|---|
| 1622 |
{ |
|---|
| 1623 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 1624 |
// not inline the reference-based version. |
|---|
| 1625 |
void exch( size_t p1, size_t p2 ) |
|---|
| 1626 |
{ |
|---|
| 1627 |
Elem t = buf[p1]; |
|---|
| 1628 |
buf[p1] = buf[p2]; |
|---|
| 1629 |
buf[p2] = t; |
|---|
| 1630 |
} |
|---|
| 1631 |
|
|---|
| 1632 |
size_t cnt = 0; |
|---|
| 1633 |
|
|---|
| 1634 |
for( size_t pos = 0, len = buf.length; pos < len; ++pos ) |
|---|
| 1635 |
{ |
|---|
| 1636 |
if( pred( buf[pos] ) ) |
|---|
| 1637 |
++cnt; |
|---|
| 1638 |
else |
|---|
| 1639 |
exch( pos, pos - cnt ); |
|---|
| 1640 |
} |
|---|
| 1641 |
return buf.length - cnt; |
|---|
| 1642 |
} |
|---|
| 1643 |
} |
|---|
| 1644 |
|
|---|
| 1645 |
|
|---|
| 1646 |
template removeIf( Buf, Pred ) |
|---|
| 1647 |
{ |
|---|
| 1648 |
size_t removeIf( Buf buf, Pred pred ) |
|---|
| 1649 |
{ |
|---|
| 1650 |
return removeIf_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 1651 |
} |
|---|
| 1652 |
} |
|---|
| 1653 |
|
|---|
| 1654 |
|
|---|
| 1655 |
debug( UnitTest ) |
|---|
| 1656 |
{ |
|---|
| 1657 |
unittest |
|---|
| 1658 |
{ |
|---|
| 1659 |
void test( char[] buf, bool delegate( char ) dg, size_t num ) |
|---|
| 1660 |
{ |
|---|
| 1661 |
assert( removeIf( buf, dg ) == num ); |
|---|
| 1662 |
foreach( pos, cur; buf ) |
|---|
| 1663 |
{ |
|---|
| 1664 |
assert( pos < num ? !dg( cur ) : dg( cur ) ); |
|---|
| 1665 |
} |
|---|
| 1666 |
} |
|---|
| 1667 |
|
|---|
| 1668 |
test( "abcdefghij".dup, ( char c ) { return c == 'x'; }, 10 ); |
|---|
| 1669 |
test( "xabcdefghi".dup, ( char c ) { return c == 'x'; }, 9 ); |
|---|
| 1670 |
test( "abcdefghix".dup, ( char c ) { return c == 'x'; }, 9 ); |
|---|
| 1671 |
test( "abxxcdefgh".dup, ( char c ) { return c == 'x'; }, 8 ); |
|---|
| 1672 |
test( "xaxbcdxxex".dup, ( char c ) { return c == 'x'; }, 5 ); |
|---|
| 1673 |
} |
|---|
| 1674 |
} |
|---|
| 1675 |
} |
|---|
| 1676 |
|
|---|
| 1677 |
|
|---|
| 1678 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1679 |
// Unique |
|---|
| 1680 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1681 |
|
|---|
| 1682 |
|
|---|
| 1683 |
version( TangoDoc ) |
|---|
| 1684 |
{ |
|---|
| 1685 |
/** |
|---|
| 1686 |
* Performs a linear scan of buf from $(LB)0 .. buf.length$(RP), moving all |
|---|
| 1687 |
* but the first element of each consecutive group of duplicate elements to |
|---|
| 1688 |
* the end of the sequence. The relative order of all remaining elements |
|---|
| 1689 |
* will be preserved. Comparisons will be performed using the supplied |
|---|
| 1690 |
* predicate or '==' if none is supplied. |
|---|
| 1691 |
* |
|---|
| 1692 |
* Params: |
|---|
| 1693 |
* buf = The array to scan. This parameter is not marked 'ref' |
|---|
| 1694 |
* to allow temporary slices to be modified. As buf is not resized |
|---|
| 1695 |
* in any way, omitting the 'ref' qualifier has no effect on the |
|---|
| 1696 |
* result of this operation, even though it may be viewed as a |
|---|
| 1697 |
* side-effect. |
|---|
| 1698 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 1699 |
* equal to e2 and false if not. This predicate may be any |
|---|
| 1700 |
* callable type. |
|---|
| 1701 |
* |
|---|
| 1702 |
* Returns: |
|---|
| 1703 |
* The number of distinct sub-sequences in buf. |
|---|
| 1704 |
*/ |
|---|
| 1705 |
size_t distinct( Elem[] buf, Pred2E pred = Pred2E.init ); |
|---|
| 1706 |
} |
|---|
| 1707 |
else |
|---|
| 1708 |
{ |
|---|
| 1709 |
template distinct_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 1710 |
{ |
|---|
| 1711 |
static assert( isCallableType!(Pred) ); |
|---|
| 1712 |
|
|---|
| 1713 |
|
|---|
| 1714 |
size_t fn( Elem[] buf, Pred pred = Pred.init ) |
|---|
| 1715 |
{ |
|---|
| 1716 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 1717 |
// not inline the reference-based version. |
|---|
| 1718 |
void exch( size_t p1, size_t p2 ) |
|---|
| 1719 |
{ |
|---|
| 1720 |
Elem t = buf[p1]; |
|---|
| 1721 |
buf[p1] = buf[p2]; |
|---|
| 1722 |
buf[p2] = t; |
|---|
| 1723 |
} |
|---|
| 1724 |
|
|---|
| 1725 |
if( buf.length < 2 ) |
|---|
| 1726 |
return buf.length; |
|---|
| 1727 |
|
|---|
| 1728 |
size_t cnt = 0; |
|---|
| 1729 |
Elem pat = buf[0]; |
|---|
| 1730 |
|
|---|
| 1731 |
for( size_t pos = 1, len = buf.length; pos < len; ++pos ) |
|---|
| 1732 |
{ |
|---|
| 1733 |
if( pred( buf[pos], pat ) ) |
|---|
| 1734 |
++cnt; |
|---|
| 1735 |
else |
|---|
| 1736 |
{ |
|---|
| 1737 |
pat = buf[pos]; |
|---|
| 1738 |
exch( pos, pos - cnt ); |
|---|
| 1739 |
} |
|---|
| 1740 |
} |
|---|
| 1741 |
return buf.length - cnt; |
|---|
| 1742 |
} |
|---|
| 1743 |
} |
|---|
| 1744 |
|
|---|
| 1745 |
|
|---|
| 1746 |
template distinct( Buf ) |
|---|
| 1747 |
{ |
|---|
| 1748 |
size_t distinct( Buf buf ) |
|---|
| 1749 |
{ |
|---|
| 1750 |
return distinct_!(ElemTypeOf!(Buf)).fn( buf ); |
|---|
| 1751 |
} |
|---|
| 1752 |
} |
|---|
| 1753 |
|
|---|
| 1754 |
|
|---|
| 1755 |
template distinct( Buf, Pred ) |
|---|
| 1756 |
{ |
|---|
| 1757 |
size_t distinct( Buf buf, Pred pred ) |
|---|
| 1758 |
{ |
|---|
| 1759 |
return distinct_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 1760 |
} |
|---|
| 1761 |
} |
|---|
| 1762 |
|
|---|
| 1763 |
|
|---|
| 1764 |
debug( UnitTest ) |
|---|
| 1765 |
{ |
|---|
| 1766 |
unittest |
|---|
| 1767 |
{ |
|---|
| 1768 |
void test( char[] buf, char[] pat ) |
|---|
| 1769 |
{ |
|---|
| 1770 |
assert( distinct( buf ) == pat.length ); |
|---|
| 1771 |
foreach( pos, cur; pat ) |
|---|
| 1772 |
{ |
|---|
| 1773 |
assert( buf[pos] == cur ); |
|---|
| 1774 |
} |
|---|
| 1775 |
} |
|---|
| 1776 |
|
|---|
| 1777 |
test( "abcdefghij".dup, "abcdefghij" ); |
|---|
| 1778 |
test( "aabcdefghi".dup, "abcdefghi" ); |
|---|
| 1779 |
test( "bcdefghijj".dup, "bcdefghij" ); |
|---|
| 1780 |
test( "abccdefghi".dup, "abcdefghi" ); |
|---|
| 1781 |
test( "abccdddefg".dup, "abcdefg" ); |
|---|
| 1782 |
} |
|---|
| 1783 |
} |
|---|
| 1784 |
} |
|---|
| 1785 |
|
|---|
| 1786 |
|
|---|
| 1787 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1788 |
// Shuffle |
|---|
| 1789 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1790 |
|
|---|
| 1791 |
|
|---|
| 1792 |
version( TangoDoc ) |
|---|
| 1793 |
{ |
|---|
| 1794 |
/** |
|---|
| 1795 |
* Performs a linear scan of buf from $(LB)2 .. buf.length$(RP), exchanging |
|---|
| 1796 |
* each element with an element in the range $(LB)0 .. pos$(RP), where pos |
|---|
| 1797 |
* represents the current array position. |
|---|
| 1798 |
* |
|---|
| 1799 |
* Params: |
|---|
| 1800 |
* buf = The array to shuffle. |
|---|
| 1801 |
* oper = The randomize operation, which should return a number in the |
|---|
| 1802 |
* range $(LB)0 .. N$(RP) for any supplied value N. This routine |
|---|
| 1803 |
* may be any callable type. |
|---|
| 1804 |
*/ |
|---|
| 1805 |
void shuffle( Elem[] buf, Oper1A oper = Oper1A.init ); |
|---|
| 1806 |
|
|---|
| 1807 |
} |
|---|
| 1808 |
else |
|---|
| 1809 |
{ |
|---|
| 1810 |
template shuffle_( Elem, Oper ) |
|---|
| 1811 |
{ |
|---|
| 1812 |
static assert( isCallableType!(Oper) ); |
|---|
| 1813 |
|
|---|
| 1814 |
|
|---|
| 1815 |
void fn( Elem[] buf, Oper oper ) |
|---|
| 1816 |
{ |
|---|
| 1817 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 1818 |
// not inline the reference-based version. |
|---|
| 1819 |
void exch( size_t p1, size_t p2 ) |
|---|
| 1820 |
{ |
|---|
| 1821 |
Elem t = buf[p1]; |
|---|
| 1822 |
buf[p1] = buf[p2]; |
|---|
| 1823 |
buf[p2] = t; |
|---|
| 1824 |
} |
|---|
| 1825 |
|
|---|
| 1826 |
for( size_t pos = buf.length - 1; pos > 0; --pos ) |
|---|
| 1827 |
{ |
|---|
| 1828 |
exch( pos, oper( pos + 1 ) ); |
|---|
| 1829 |
} |
|---|
| 1830 |
} |
|---|
| 1831 |
} |
|---|
| 1832 |
|
|---|
| 1833 |
|
|---|
| 1834 |
template shuffle( Buf, Oper = RandOper!() ) |
|---|
| 1835 |
{ |
|---|
| 1836 |
void shuffle( Buf buf, Oper oper = Oper.init ) |
|---|
| 1837 |
{ |
|---|
| 1838 |
return shuffle_!(ElemTypeOf!(Buf), Oper).fn( buf, oper ); |
|---|
| 1839 |
} |
|---|
| 1840 |
} |
|---|
| 1841 |
|
|---|
| 1842 |
|
|---|
| 1843 |
debug( UnitTest ) |
|---|
| 1844 |
{ |
|---|
| 1845 |
unittest |
|---|
| 1846 |
{ |
|---|
| 1847 |
char[] buf = "abcdefghijklmnopqrstuvwxyz"; |
|---|
| 1848 |
char[] tmp = buf.dup; |
|---|
| 1849 |
|
|---|
| 1850 |
assert( tmp == buf ); |
|---|
| 1851 |
shuffle( tmp ); |
|---|
| 1852 |
assert( tmp != buf ); |
|---|
| 1853 |
} |
|---|
| 1854 |
} |
|---|
| 1855 |
} |
|---|
| 1856 |
|
|---|
| 1857 |
|
|---|
| 1858 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1859 |
// Partition |
|---|
| 1860 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1861 |
|
|---|
| 1862 |
|
|---|
| 1863 |
version( TangoDoc ) |
|---|
| 1864 |
{ |
|---|
| 1865 |
/** |
|---|
| 1866 |
* Partitions buf such that all elements that satisfy pred will be placed |
|---|
| 1867 |
* before the elements that do not satisfy pred. The algorithm is not |
|---|
| 1868 |
* required to be stable. |
|---|
| 1869 |
* |
|---|
| 1870 |
* Params: |
|---|
| 1871 |
* buf = The array to partition. This parameter is not marked 'ref' |
|---|
| 1872 |
* to allow temporary slices to be sorted. As buf is not resized |
|---|
| 1873 |
* in any way, omitting the 'ref' qualifier has no effect on |
|---|
| 1874 |
* the result of this operation, even though it may be viewed |
|---|
| 1875 |
* as a side-effect. |
|---|
| 1876 |
* pred = The evaluation predicate, which should return true if the |
|---|
| 1877 |
* element satisfies the condition and false if not. This |
|---|
| 1878 |
* predicate may be any callable type. |
|---|
| 1879 |
* |
|---|
| 1880 |
* Returns: |
|---|
| 1881 |
* The number of elements that satisfy pred. |
|---|
| 1882 |
*/ |
|---|
| 1883 |
size_t partition( Elem[] buf, Pred1E pred ); |
|---|
| 1884 |
} |
|---|
| 1885 |
else |
|---|
| 1886 |
{ |
|---|
| 1887 |
template partition_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 1888 |
{ |
|---|
| 1889 |
static assert( isCallableType!(Pred ) ); |
|---|
| 1890 |
|
|---|
| 1891 |
|
|---|
| 1892 |
size_t fn( Elem[] buf, Pred pred ) |
|---|
| 1893 |
{ |
|---|
| 1894 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 1895 |
// not inline the reference-based version. |
|---|
| 1896 |
void exch( size_t p1, size_t p2 ) |
|---|
| 1897 |
{ |
|---|
| 1898 |
Elem t = buf[p1]; |
|---|
| 1899 |
buf[p1] = buf[p2]; |
|---|
| 1900 |
buf[p2] = t; |
|---|
| 1901 |
} |
|---|
| 1902 |
|
|---|
| 1903 |
if( buf.length < 2 ) |
|---|
| 1904 |
return 0; |
|---|
| 1905 |
|
|---|
| 1906 |
size_t l = 0, |
|---|
| 1907 |
r = buf.length, |
|---|
| 1908 |
i = l, |
|---|
| 1909 |
j = r - 1; |
|---|
| 1910 |
|
|---|
| 1911 |
while( true ) |
|---|
| 1912 |
{ |
|---|
| 1913 |
while( i < r && pred( buf[i] ) ) |
|---|
| 1914 |
++i; |
|---|
| 1915 |
while( j > l && !pred( buf[j] ) ) |
|---|
| 1916 |
--j; |
|---|
| 1917 |
if( i >= j ) |
|---|
| 1918 |
break; |
|---|
| 1919 |
exch( i++, j-- ); |
|---|
| 1920 |
} |
|---|
| 1921 |
return i; |
|---|
| 1922 |
} |
|---|
| 1923 |
} |
|---|
| 1924 |
|
|---|
| 1925 |
|
|---|
| 1926 |
template partition( Buf, Pred ) |
|---|
| 1927 |
{ |
|---|
| 1928 |
size_t partition( Buf buf, Pred pred ) |
|---|
| 1929 |
{ |
|---|
| 1930 |
return partition_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 1931 |
} |
|---|
| 1932 |
} |
|---|
| 1933 |
|
|---|
| 1934 |
|
|---|
| 1935 |
debug( UnitTest ) |
|---|
| 1936 |
{ |
|---|
| 1937 |
unittest |
|---|
| 1938 |
{ |
|---|
| 1939 |
void test( char[] buf, bool delegate( char ) dg, size_t num ) |
|---|
| 1940 |
{ |
|---|
| 1941 |
assert( partition( buf, dg ) == num ); |
|---|
| 1942 |
for( size_t pos = 0; pos < buf.length; ++pos ) |
|---|
| 1943 |
{ |
|---|
| 1944 |
assert( pos < num ? dg( buf[pos] ) : !dg( buf[pos] ) ); |
|---|
| 1945 |
} |
|---|
| 1946 |
} |
|---|
| 1947 |
|
|---|
| 1948 |
test( "abcdefg".dup, ( char c ) { return c < 'a'; }, 0 ); |
|---|
| 1949 |
test( "gfedcba".dup, ( char c ) { return c < 'a'; }, 0 ); |
|---|
| 1950 |
test( "abcdefg".dup, ( char c ) { return c < 'h'; }, 7 ); |
|---|
| 1951 |
test( "gfedcba".dup, ( char c ) { return c < 'h'; }, 7 ); |
|---|
| 1952 |
test( "abcdefg".dup, ( char c ) { return c < 'd'; }, 3 ); |
|---|
| 1953 |
test( "gfedcba".dup, ( char c ) { return c < 'd'; }, 3 ); |
|---|
| 1954 |
test( "bbdaabc".dup, ( char c ) { return c < 'c'; }, 5 ); |
|---|
| 1955 |
} |
|---|
| 1956 |
} |
|---|
| 1957 |
} |
|---|
| 1958 |
|
|---|
| 1959 |
|
|---|
| 1960 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1961 |
// Select |
|---|
| 1962 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 1963 |
|
|---|
| 1964 |
|
|---|
| 1965 |
version( TangoDoc ) |
|---|
| 1966 |
{ |
|---|
| 1967 |
/** |
|---|
| 1968 |
* Partitions buf with num - 1 as a pivot such that the first num elements |
|---|
| 1969 |
* will be less than or equal to the remaining elements in the array. |
|---|
| 1970 |
* Comparisons will be performed using the supplied predicate or '<' if |
|---|
| 1971 |
* none is supplied. The algorithm is not required to be stable. |
|---|
| 1972 |
* |
|---|
| 1973 |
* Params: |
|---|
| 1974 |
* buf = The array to partition. This parameter is not marked 'ref' |
|---|
| 1975 |
* to allow temporary slices to be sorted. As buf is not resized |
|---|
| 1976 |
* in any way, omitting the 'ref' qualifier has no effect on |
|---|
| 1977 |
* the result of this operation, even though it may be viewed |
|---|
| 1978 |
* as a side-effect. |
|---|
| 1979 |
* num = The number of elements which are considered significant in |
|---|
| 1980 |
* this array, where num - 1 is the pivot around which partial |
|---|
| 1981 |
* sorting will occur. For example, if num is buf.length / 2 |
|---|
| 1982 |
* then select will effectively partition the array around its |
|---|
| 1983 |
* median value, with the elements in the first half of the array |
|---|
| 1984 |
* evaluating as less than or equal to the elements in the second |
|---|
| 1985 |
* half. |
|---|
| 1986 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 1987 |
* less than e2 and false if not. This predicate may be any |
|---|
| 1988 |
* callable type. |
|---|
| 1989 |
* |
|---|
| 1990 |
* Returns: |
|---|
| 1991 |
* The index of the pivot point, which will be the lesser of num - 1 and |
|---|
| 1992 |
* buf.length. |
|---|
| 1993 |
*/ |
|---|
| 1994 |
size_t select( Elem[] buf, Num num, Pred2E pred = Pred2E.init ); |
|---|
| 1995 |
} |
|---|
| 1996 |
else |
|---|
| 1997 |
{ |
|---|
| 1998 |
template select_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 1999 |
{ |
|---|
| 2000 |
static assert( isCallableType!(Pred ) ); |
|---|
| 2001 |
|
|---|
| 2002 |
|
|---|
| 2003 |
size_t fn( Elem[] buf, size_t num, Pred pred = Pred.init ) |
|---|
| 2004 |
{ |
|---|
| 2005 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 2006 |
// not inline the reference-based version. |
|---|
| 2007 |
void exch( size_t p1, size_t p2 ) |
|---|
| 2008 |
{ |
|---|
| 2009 |
Elem t = buf[p1]; |
|---|
| 2010 |
buf[p1] = buf[p2]; |
|---|
| 2011 |
buf[p2] = t; |
|---|
| 2012 |
} |
|---|
| 2013 |
|
|---|
| 2014 |
if( buf.length < 2 ) |
|---|
| 2015 |
return buf.length; |
|---|
| 2016 |
|
|---|
| 2017 |
size_t l = 0, |
|---|
| 2018 |
r = buf.length - 1, |
|---|
| 2019 |
k = num; |
|---|
| 2020 |
|
|---|
| 2021 |
while( r > l ) |
|---|
| 2022 |
{ |
|---|
| 2023 |
size_t i = l, |
|---|
| 2024 |
j = r - 1; |
|---|
| 2025 |
Elem v = buf[r]; |
|---|
| 2026 |
|
|---|
| 2027 |
while( true ) |
|---|
| 2028 |
{ |
|---|
| 2029 |
while( i < r && pred( buf[i], v ) ) |
|---|
| 2030 |
++i; |
|---|
| 2031 |
while( j > l && pred( v, buf[j] ) ) |
|---|
| 2032 |
--j; |
|---|
| 2033 |
if( i >= j ) |
|---|
| 2034 |
break; |
|---|
| 2035 |
exch( i++, j-- ); |
|---|
| 2036 |
} |
|---|
| 2037 |
exch( i, r ); |
|---|
| 2038 |
if( i >= k ) |
|---|
| 2039 |
r = i - 1; |
|---|
| 2040 |
if( i <= k ) |
|---|
| 2041 |
l = i + 1; |
|---|
| 2042 |
} |
|---|
| 2043 |
return num - 1; |
|---|
| 2044 |
} |
|---|
| 2045 |
} |
|---|
| 2046 |
|
|---|
| 2047 |
|
|---|
| 2048 |
template select( Buf, Num ) |
|---|
| 2049 |
{ |
|---|
| 2050 |
size_t select( Buf buf, Num num ) |
|---|
| 2051 |
{ |
|---|
| 2052 |
return select_!(ElemTypeOf!(Buf)).fn( buf, num ); |
|---|
| 2053 |
} |
|---|
| 2054 |
} |
|---|
| 2055 |
|
|---|
| 2056 |
|
|---|
| 2057 |
template select( Buf, Num, Pred ) |
|---|
| 2058 |
{ |
|---|
| 2059 |
size_t select( Buf buf, Num num, Pred pred ) |
|---|
| 2060 |
{ |
|---|
| 2061 |
return select_!(ElemTypeOf!(Buf), Pred).fn( buf, num, pred ); |
|---|
| 2062 |
} |
|---|
| 2063 |
} |
|---|
| 2064 |
|
|---|
| 2065 |
|
|---|
| 2066 |
debug( UnitTest ) |
|---|
| 2067 |
{ |
|---|
| 2068 |
unittest |
|---|
| 2069 |
{ |
|---|
| 2070 |
char[] buf = "efedcaabca".dup; |
|---|
| 2071 |
size_t num = buf.length / 2; |
|---|
| 2072 |
size_t pos = select( buf, num ); |
|---|
| 2073 |
|
|---|
| 2074 |
assert( pos == num - 1 ); |
|---|
| 2075 |
foreach( cur; buf[0 .. pos] ) |
|---|
| 2076 |
assert( cur <= buf[pos] ); |
|---|
| 2077 |
foreach( cur; buf[pos .. $] ) |
|---|
| 2078 |
assert( cur >= buf[pos] ); |
|---|
| 2079 |
} |
|---|
| 2080 |
} |
|---|
| 2081 |
} |
|---|
| 2082 |
|
|---|
| 2083 |
|
|---|
| 2084 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2085 |
// Sort |
|---|
| 2086 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2087 |
|
|---|
| 2088 |
|
|---|
| 2089 |
version( TangoDoc ) |
|---|
| 2090 |
{ |
|---|
| 2091 |
/** |
|---|
| 2092 |
* Sorts buf using the supplied predicate or '<' if none is supplied. The |
|---|
| 2093 |
* algorithm is not required to be stable. The current implementation is |
|---|
| 2094 |
* based on quicksort, but uses a three-way partitioning scheme to improve |
|---|
| 2095 |
* performance for ranges containing duplicate values (Bentley and McIlroy, |
|---|
| 2096 |
* 1993). |
|---|
| 2097 |
* |
|---|
| 2098 |
* Params: |
|---|
| 2099 |
* buf = The array to sort. This parameter is not marked 'ref' to |
|---|
| 2100 |
* allow temporary slices to be sorted. As buf is not resized |
|---|
| 2101 |
* in any way, omitting the 'ref' qualifier has no effect on |
|---|
| 2102 |
* the result of this operation, even though it may be viewed |
|---|
| 2103 |
* as a side-effect. |
|---|
| 2104 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2105 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2106 |
* callable type. |
|---|
| 2107 |
*/ |
|---|
| 2108 |
void sort( Elem, Pred2E = IsLess!(Elem) )( Elem[] buf, Pred2E pred = Pred2E.init ); |
|---|
| 2109 |
} |
|---|
| 2110 |
else |
|---|
| 2111 |
{ |
|---|
| 2112 |
template sort_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2113 |
{ |
|---|
| 2114 |
static assert( isCallableType!(Pred ) ); |
|---|
| 2115 |
|
|---|
| 2116 |
|
|---|
| 2117 |
void fn( Elem[] buf, Pred pred = Pred.init ) |
|---|
| 2118 |
{ |
|---|
| 2119 |
bool equiv( Elem p1, Elem p2 ) |
|---|
| 2120 |
{ |
|---|
| 2121 |
return !pred( p1, p2 ) && !pred( p2, p1 ); |
|---|
| 2122 |
} |
|---|
| 2123 |
|
|---|
| 2124 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 2125 |
// not inline the reference-based version. |
|---|
| 2126 |
void exch( size_t p1, size_t p2 ) |
|---|
| 2127 |
{ |
|---|
| 2128 |
Elem t = buf[p1]; |
|---|
| 2129 |
buf[p1] = buf[p2]; |
|---|
| 2130 |
buf[p2] = t; |
|---|
| 2131 |
} |
|---|
| 2132 |
|
|---|
| 2133 |
// NOTE: This algorithm operates on the inclusive range [l .. r]. |
|---|
| 2134 |
void insertionSort( size_t l, size_t r ) |
|---|
| 2135 |
{ |
|---|
| 2136 |
for( size_t i = r; i > l; --i ) |
|---|
| 2137 |
{ |
|---|
| 2138 |
// swap the min element to buf[0] to act as a sentinel |
|---|
| 2139 |
if( pred( buf[i], buf[i - 1] ) ) |
|---|
| 2140 |
exch( i, i - 1 ); |
|---|
| 2141 |
} |
|---|
| 2142 |
for( size_t i = l + 2; i <= r; ++i ) |
|---|
| 2143 |
{ |
|---|
| 2144 |
size_t j = i; |
|---|
| 2145 |
Elem v = buf[i]; |
|---|
| 2146 |
|
|---|
| 2147 |
// don't need to test (j != l) because of the sentinel |
|---|
| 2148 |
while( pred( v, buf[j - 1] ) ) |
|---|
| 2149 |
{ |
|---|
| 2150 |
buf[j] = buf[j - 1]; |
|---|
| 2151 |
j--; |
|---|
| 2152 |
} |
|---|
| 2153 |
buf[j] = v; |
|---|
| 2154 |
} |
|---|
| 2155 |
} |
|---|
| 2156 |
|
|---|
| 2157 |
size_t medianOf( size_t l, size_t m, size_t r ) |
|---|
| 2158 |
{ |
|---|
| 2159 |
if( pred( buf[m], buf[l] ) ) |
|---|
| 2160 |
{ |
|---|
| 2161 |
if( pred( buf[r], buf[m] ) ) |
|---|
| 2162 |
return m; |
|---|
| 2163 |
else |
|---|
| 2164 |
{ |
|---|
| 2165 |
if( pred( buf[r], buf[l] ) ) |
|---|
| 2166 |
return r; |
|---|
| 2167 |
else |
|---|
| 2168 |
return l; |
|---|
| 2169 |
} |
|---|
| 2170 |
} |
|---|
| 2171 |
else |
|---|
| 2172 |
{ |
|---|
| 2173 |
if( pred( buf[r], buf[m] ) ) |
|---|
| 2174 |
{ |
|---|
| 2175 |
if( pred( buf[r], buf[l] ) ) |
|---|
| 2176 |
return l; |
|---|
| 2177 |
else |
|---|
| 2178 |
return r; |
|---|
| 2179 |
} |
|---|
| 2180 |
else |
|---|
| 2181 |
return m; |
|---|
| 2182 |
} |
|---|
| 2183 |
} |
|---|
| 2184 |
|
|---|
| 2185 |
// NOTE: This algorithm operates on the inclusive range [l .. r]. |
|---|
| 2186 |
void quicksort( size_t l, size_t r, size_t d ) |
|---|
| 2187 |
{ |
|---|
| 2188 |
if( r <= l ) |
|---|
| 2189 |
return; |
|---|
| 2190 |
|
|---|
| 2191 |
// HEURISTIC: Use insertion sort for sufficiently small arrays. |
|---|
| 2192 |
enum { MIN_LENGTH = 80 } |
|---|
| 2193 |
if( r - l < MIN_LENGTH ) |
|---|
| 2194 |
return insertionSort( l, r ); |
|---|
| 2195 |
|
|---|
| 2196 |
// HEURISTIC: If the recursion depth is too great, assume this |
|---|
| 2197 |
// is a worst-case array and fail to heap sort. |
|---|
| 2198 |
if( d-- == 0 ) |
|---|
| 2199 |
{ |
|---|
| 2200 |
makeHeap( buf[l .. r+1], pred ); |
|---|
| 2201 |
sortHeap( buf[l .. r+1], pred ); |
|---|
| 2202 |
return; |
|---|
| 2203 |
} |
|---|
| 2204 |
|
|---|
| 2205 |
// HEURISTIC: Use the median-of-3 value as a pivot. Swap this |
|---|
| 2206 |
// into r so quicksort remains untouched. |
|---|
| 2207 |
exch( r, medianOf( l, l + (r - l) / 2, r ) ); |
|---|
| 2208 |
|
|---|
| 2209 |
// This implementation of quicksort improves upon the classic |
|---|
| 2210 |
// algorithm by partitioning the array into three parts, one |
|---|
| 2211 |
// each for keys smaller than, equal to, and larger than the |
|---|
| 2212 |
// partitioning element, v: |
|---|
| 2213 |
// |
|---|
| 2214 |
// |--less than v--|--equal to v--|--greater than v--[v] |
|---|
| 2215 |
// l j i r |
|---|
| 2216 |
// |
|---|
| 2217 |
// This approach sorts ranges containing duplicate elements |
|---|
| 2218 |
// more quickly. During processing, the following situation |
|---|
| 2219 |
// is maintained: |
|---|
| 2220 |
// |
|---|
| 2221 |
// |--equal--|--less--|--[###]--|--greater--|--equal--[v] |
|---|
| 2222 |
// l p i j q r |
|---|
| 2223 |
// |
|---|
| 2224 |
// Please note that this implementation varies from the typical |
|---|
| 2225 |
// algorithm by replacing the use of signed index values with |
|---|
| 2226 |
// unsigned values. |
|---|
| 2227 |
|
|---|
| 2228 |
Elem v = buf[r]; |
|---|
| 2229 |
size_t i = l, |
|---|
| 2230 |
j = r, |
|---|
| 2231 |
p = l, |
|---|
| 2232 |
q = r; |
|---|
| 2233 |
|
|---|
| 2234 |
while( true ) |
|---|
| 2235 |
{ |
|---|
| 2236 |
while( pred( buf[i], v ) ) |
|---|
| 2237 |
++i; |
|---|
| 2238 |
while( pred( v, buf[--j] ) ) |
|---|
| 2239 |
if( j == l ) break; |
|---|
| 2240 |
if( i >= j ) |
|---|
| 2241 |
break; |
|---|
| 2242 |
exch( i, j ); |
|---|
| 2243 |
if( equiv( buf[i], v ) ) |
|---|
| 2244 |
exch( p++, i ); |
|---|
| 2245 |
if( equiv( v, buf[j] ) ) |
|---|
| 2246 |
exch( --q, j ); |
|---|
| 2247 |
++i; |
|---|
| 2248 |
} |
|---|
| 2249 |
exch( i, r ); |
|---|
| 2250 |
if( p < i ) |
|---|
| 2251 |
{ |
|---|
| 2252 |
j = i - 1; |
|---|
| 2253 |
for( size_t k = l; k < p; k++, j-- ) |
|---|
| 2254 |
exch( k, j ); |
|---|
| 2255 |
quicksort( l, j, d ); |
|---|
| 2256 |
} |
|---|
| 2257 |
if( ++i < q ) |
|---|
| 2258 |
{ |
|---|
| 2259 |
for( size_t k = r - 1; k >= q; k--, i++ ) |
|---|
| 2260 |
exch( k, i ); |
|---|
| 2261 |
quicksort( i, r, d ); |
|---|
| 2262 |
} |
|---|
| 2263 |
} |
|---|
| 2264 |
|
|---|
| 2265 |
size_t maxDepth( size_t x ) |
|---|
| 2266 |
{ |
|---|
| 2267 |
size_t d = 0; |
|---|
| 2268 |
|
|---|
| 2269 |
do |
|---|
| 2270 |
{ |
|---|
| 2271 |
++d; |
|---|
| 2272 |
x /= 2; |
|---|
| 2273 |
} while( x > 1 ); |
|---|
| 2274 |
return d * 2; // same as "floor( log( x ) / log( 2 ) ) * 2" |
|---|
| 2275 |
} |
|---|
| 2276 |
|
|---|
| 2277 |
if( buf.length > 1 ) |
|---|
| 2278 |
{ |
|---|
| 2279 |
quicksort( 0, buf.length - 1, maxDepth( buf.length ) ); |
|---|
| 2280 |
} |
|---|
| 2281 |
} |
|---|
| 2282 |
} |
|---|
| 2283 |
|
|---|
| 2284 |
|
|---|
| 2285 |
template sort( Buf ) |
|---|
| 2286 |
{ |
|---|
| 2287 |
void sort( Buf buf ) |
|---|
| 2288 |
{ |
|---|
| 2289 |
return sort_!(ElemTypeOf!(Buf)).fn( buf ); |
|---|
| 2290 |
} |
|---|
| 2291 |
} |
|---|
| 2292 |
|
|---|
| 2293 |
|
|---|
| 2294 |
template sort( Buf, Pred ) |
|---|
| 2295 |
{ |
|---|
| 2296 |
void sort( Buf buf, Pred pred ) |
|---|
| 2297 |
{ |
|---|
| 2298 |
return sort_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 2299 |
} |
|---|
| 2300 |
} |
|---|
| 2301 |
|
|---|
| 2302 |
|
|---|
| 2303 |
debug( UnitTest ) |
|---|
| 2304 |
{ |
|---|
| 2305 |
unittest |
|---|
| 2306 |
{ |
|---|
| 2307 |
void test( char[] buf ) |
|---|
| 2308 |
{ |
|---|
| 2309 |
sort( buf ); |
|---|
| 2310 |
char sav = buf[0]; |
|---|
| 2311 |
foreach( cur; buf ) |
|---|
| 2312 |
{ |
|---|
| 2313 |
assert( cur >= sav ); |
|---|
| 2314 |
sav = cur; |
|---|
| 2315 |
} |
|---|
| 2316 |
} |
|---|
| 2317 |
|
|---|
| 2318 |
test( "mkcvalsidivjoaisjdvmzlksvdjioawmdsvmsdfefewv".dup ); |
|---|
| 2319 |
test( "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf".dup ); |
|---|
| 2320 |
test( "the quick brown fox jumped over the lazy dog".dup ); |
|---|
| 2321 |
test( "abcdefghijklmnopqrstuvwxyz".dup ); |
|---|
| 2322 |
test( "zyxwvutsrqponmlkjihgfedcba".dup ); |
|---|
| 2323 |
} |
|---|
| 2324 |
} |
|---|
| 2325 |
} |
|---|
| 2326 |
|
|---|
| 2327 |
|
|---|
| 2328 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2329 |
// Lower Bound |
|---|
| 2330 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2331 |
|
|---|
| 2332 |
|
|---|
| 2333 |
version( TangoDoc ) |
|---|
| 2334 |
{ |
|---|
| 2335 |
/** |
|---|
| 2336 |
* Performs a binary search of buf, returning the index of the first |
|---|
| 2337 |
* location where pat may be inserted without disrupting sort order. If |
|---|
| 2338 |
* the sort order of pat precedes all elements in buf then 0 will be |
|---|
| 2339 |
* returned. If the sort order of pat succeeds the largest element in buf |
|---|
| 2340 |
* then buf.length will be returned. Comparisons will be performed using |
|---|
| 2341 |
* the supplied predicate or '<' if none is supplied. |
|---|
| 2342 |
* |
|---|
| 2343 |
* Params: |
|---|
| 2344 |
* buf = The sorted array to search. |
|---|
| 2345 |
* pat = The pattern to search for. |
|---|
| 2346 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2347 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2348 |
* callable type. |
|---|
| 2349 |
* |
|---|
| 2350 |
* Returns: |
|---|
| 2351 |
* The index of the first match or buf.length if no match was found. |
|---|
| 2352 |
*/ |
|---|
| 2353 |
size_t lbound( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 2354 |
} |
|---|
| 2355 |
else |
|---|
| 2356 |
{ |
|---|
| 2357 |
template lbound_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2358 |
{ |
|---|
| 2359 |
static assert( isCallableType!(Pred) ); |
|---|
| 2360 |
|
|---|
| 2361 |
|
|---|
| 2362 |
size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 2363 |
{ |
|---|
| 2364 |
size_t beg = 0, |
|---|
| 2365 |
end = buf.length, |
|---|
| 2366 |
mid = end / 2; |
|---|
| 2367 |
|
|---|
| 2368 |
while( beg < end ) |
|---|
| 2369 |
{ |
|---|
| 2370 |
if( pred( buf[mid], pat ) ) |
|---|
| 2371 |
beg = mid + 1; |
|---|
| 2372 |
else |
|---|
| 2373 |
end = mid; |
|---|
| 2374 |
mid = beg + ( end - beg ) / 2; |
|---|
| 2375 |
} |
|---|
| 2376 |
return mid; |
|---|
| 2377 |
} |
|---|
| 2378 |
} |
|---|
| 2379 |
|
|---|
| 2380 |
|
|---|
| 2381 |
template lbound( Buf, Pat ) |
|---|
| 2382 |
{ |
|---|
| 2383 |
size_t lbound( Buf buf, Pat pat ) |
|---|
| 2384 |
{ |
|---|
| 2385 |
return lbound_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 2386 |
} |
|---|
| 2387 |
} |
|---|
| 2388 |
|
|---|
| 2389 |
|
|---|
| 2390 |
template lbound( Buf, Pat, Pred ) |
|---|
| 2391 |
{ |
|---|
| 2392 |
size_t lbound( Buf buf, Pat pat, Pred pred ) |
|---|
| 2393 |
{ |
|---|
| 2394 |
return lbound_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 2395 |
} |
|---|
| 2396 |
} |
|---|
| 2397 |
|
|---|
| 2398 |
|
|---|
| 2399 |
debug( UnitTest ) |
|---|
| 2400 |
{ |
|---|
| 2401 |
unittest |
|---|
| 2402 |
{ |
|---|
| 2403 |
assert( lbound( "bcefg", 'a' ) == 0 ); |
|---|
| 2404 |
assert( lbound( "bcefg", 'h' ) == 5 ); |
|---|
| 2405 |
assert( lbound( "bcefg", 'd' ) == 2 ); |
|---|
| 2406 |
assert( lbound( "bcefg", 'e' ) == 2 ); |
|---|
| 2407 |
} |
|---|
| 2408 |
} |
|---|
| 2409 |
} |
|---|
| 2410 |
|
|---|
| 2411 |
|
|---|
| 2412 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2413 |
// Upper Bound |
|---|
| 2414 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2415 |
|
|---|
| 2416 |
|
|---|
| 2417 |
version( TangoDoc ) |
|---|
| 2418 |
{ |
|---|
| 2419 |
/** |
|---|
| 2420 |
* Performs a binary search of buf, returning the index of the first |
|---|
| 2421 |
* location beyond where pat may be inserted without disrupting sort order. |
|---|
| 2422 |
* If the sort order of pat precedes all elements in buf then 0 will be |
|---|
| 2423 |
* returned. If the sort order of pat succeeds the largest element in buf |
|---|
| 2424 |
* then buf.length will be returned. Comparisons will be performed using |
|---|
| 2425 |
* the supplied predicate or '<' if none is supplied. |
|---|
| 2426 |
* |
|---|
| 2427 |
* Params: |
|---|
| 2428 |
* buf = The sorted array to search. |
|---|
| 2429 |
* pat = The pattern to search for. |
|---|
| 2430 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2431 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2432 |
* callable type. |
|---|
| 2433 |
* |
|---|
| 2434 |
* Returns: |
|---|
| 2435 |
* The index of the first match or buf.length if no match was found. |
|---|
| 2436 |
*/ |
|---|
| 2437 |
size_t ubound( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 2438 |
} |
|---|
| 2439 |
else |
|---|
| 2440 |
{ |
|---|
| 2441 |
template ubound_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2442 |
{ |
|---|
| 2443 |
static assert( isCallableType!(Pred) ); |
|---|
| 2444 |
|
|---|
| 2445 |
|
|---|
| 2446 |
size_t fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 2447 |
{ |
|---|
| 2448 |
size_t beg = 0, |
|---|
| 2449 |
end = buf.length, |
|---|
| 2450 |
mid = end / 2; |
|---|
| 2451 |
|
|---|
| 2452 |
while( beg < end ) |
|---|
| 2453 |
{ |
|---|
| 2454 |
if( !pred( pat, buf[mid] ) ) |
|---|
| 2455 |
beg = mid + 1; |
|---|
| 2456 |
else |
|---|
| 2457 |
end = mid; |
|---|
| 2458 |
mid = beg + ( end - beg ) / 2; |
|---|
| 2459 |
} |
|---|
| 2460 |
return mid; |
|---|
| 2461 |
} |
|---|
| 2462 |
} |
|---|
| 2463 |
|
|---|
| 2464 |
|
|---|
| 2465 |
template ubound( Buf, Pat ) |
|---|
| 2466 |
{ |
|---|
| 2467 |
size_t ubound( Buf buf, Pat pat ) |
|---|
| 2468 |
{ |
|---|
| 2469 |
return ubound_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 2470 |
} |
|---|
| 2471 |
} |
|---|
| 2472 |
|
|---|
| 2473 |
|
|---|
| 2474 |
template ubound( Buf, Pat, Pred ) |
|---|
| 2475 |
{ |
|---|
| 2476 |
size_t ubound( Buf buf, Pat pat, Pred pred ) |
|---|
| 2477 |
{ |
|---|
| 2478 |
return ubound_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 2479 |
} |
|---|
| 2480 |
} |
|---|
| 2481 |
|
|---|
| 2482 |
|
|---|
| 2483 |
debug( UnitTest ) |
|---|
| 2484 |
{ |
|---|
| 2485 |
unittest |
|---|
| 2486 |
{ |
|---|
| 2487 |
assert( ubound( "bcefg", 'a' ) == 0 ); |
|---|
| 2488 |
assert( ubound( "bcefg", 'h' ) == 5 ); |
|---|
| 2489 |
assert( ubound( "bcefg", 'd' ) == 2 ); |
|---|
| 2490 |
assert( ubound( "bcefg", 'e' ) == 3 ); |
|---|
| 2491 |
} |
|---|
| 2492 |
} |
|---|
| 2493 |
} |
|---|
| 2494 |
|
|---|
| 2495 |
|
|---|
| 2496 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2497 |
// Binary Search |
|---|
| 2498 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2499 |
|
|---|
| 2500 |
|
|---|
| 2501 |
version( TangoDoc ) |
|---|
| 2502 |
{ |
|---|
| 2503 |
/** |
|---|
| 2504 |
* Performs a binary search of buf, returning true if an element equivalent |
|---|
| 2505 |
* to pat is found. Comparisons will be performed using the supplied |
|---|
| 2506 |
* predicate or '<' if none is supplied. |
|---|
| 2507 |
* |
|---|
| 2508 |
* Params: |
|---|
| 2509 |
* buf = The sorted array to search. |
|---|
| 2510 |
* pat = The pattern to search for. |
|---|
| 2511 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2512 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2513 |
* callable type. |
|---|
| 2514 |
* |
|---|
| 2515 |
* Returns: |
|---|
| 2516 |
* True if an element equivalent to pat is found, false if not. |
|---|
| 2517 |
*/ |
|---|
| 2518 |
bool bsearch( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init ); |
|---|
| 2519 |
} |
|---|
| 2520 |
else |
|---|
| 2521 |
{ |
|---|
| 2522 |
template bsearch_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2523 |
{ |
|---|
| 2524 |
static assert( isCallableType!(Pred) ); |
|---|
| 2525 |
|
|---|
| 2526 |
|
|---|
| 2527 |
bool fn( Elem[] buf, Elem pat, Pred pred = Pred.init ) |
|---|
| 2528 |
{ |
|---|
| 2529 |
size_t pos = lbound( buf, pat, pred ); |
|---|
| 2530 |
return pos < buf.length && !( pat < buf[pos] ); |
|---|
| 2531 |
} |
|---|
| 2532 |
} |
|---|
| 2533 |
|
|---|
| 2534 |
|
|---|
| 2535 |
template bsearch( Buf, Pat ) |
|---|
| 2536 |
{ |
|---|
| 2537 |
bool bsearch( Buf buf, Pat pat ) |
|---|
| 2538 |
{ |
|---|
| 2539 |
return bsearch_!(ElemTypeOf!(Buf)).fn( buf, pat ); |
|---|
| 2540 |
} |
|---|
| 2541 |
} |
|---|
| 2542 |
|
|---|
| 2543 |
|
|---|
| 2544 |
template bsearch( Buf, Pat, Pred ) |
|---|
| 2545 |
{ |
|---|
| 2546 |
bool bsearch( Buf buf, Pat pat, Pred pred ) |
|---|
| 2547 |
{ |
|---|
| 2548 |
return bsearch_!(ElemTypeOf!(Buf), Pred).fn( buf, pat, pred ); |
|---|
| 2549 |
} |
|---|
| 2550 |
} |
|---|
| 2551 |
|
|---|
| 2552 |
|
|---|
| 2553 |
debug( UnitTest ) |
|---|
| 2554 |
{ |
|---|
| 2555 |
unittest |
|---|
| 2556 |
{ |
|---|
| 2557 |
assert( !bsearch( "bcefg", 'a' ) ); |
|---|
| 2558 |
assert( bsearch( "bcefg", 'b' ) ); |
|---|
| 2559 |
assert( bsearch( "bcefg", 'c' ) ); |
|---|
| 2560 |
assert( !bsearch( "bcefg", 'd' ) ); |
|---|
| 2561 |
assert( bsearch( "bcefg", 'e' ) ); |
|---|
| 2562 |
assert( bsearch( "bcefg", 'f' ) ); |
|---|
| 2563 |
assert( bsearch( "bcefg", 'g' ) ); |
|---|
| 2564 |
assert( !bsearch( "bcefg", 'h' ) ); |
|---|
| 2565 |
} |
|---|
| 2566 |
} |
|---|
| 2567 |
} |
|---|
| 2568 |
|
|---|
| 2569 |
|
|---|
| 2570 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2571 |
// Includes |
|---|
| 2572 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2573 |
|
|---|
| 2574 |
|
|---|
| 2575 |
version( TangoDoc ) |
|---|
| 2576 |
{ |
|---|
| 2577 |
/** |
|---|
| 2578 |
* Performs a parallel linear scan of setA and setB from $(LB)0 .. N$(RP) |
|---|
| 2579 |
* where N = min$(LP)setA.length, setB.length$(RP), returning true if setA |
|---|
| 2580 |
* includes all elements in setB and false if not. Both setA and setB are |
|---|
| 2581 |
* required to be sorted, and duplicates in setB require an equal number of |
|---|
| 2582 |
* duplicates in setA. Comparisons will be performed using the supplied |
|---|
| 2583 |
* predicate or '<' if none is supplied. |
|---|
| 2584 |
* |
|---|
| 2585 |
* Params: |
|---|
| 2586 |
* setA = The sorted array to evaluate. |
|---|
| 2587 |
* setB = The sorted array to match against. |
|---|
| 2588 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2589 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2590 |
* callable type. |
|---|
| 2591 |
* |
|---|
| 2592 |
* Returns: |
|---|
| 2593 |
* true if setA includes all elements in setB, false if not. |
|---|
| 2594 |
*/ |
|---|
| 2595 |
bool includes( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); |
|---|
| 2596 |
} |
|---|
| 2597 |
else |
|---|
| 2598 |
{ |
|---|
| 2599 |
template includes_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2600 |
{ |
|---|
| 2601 |
static assert( isCallableType!(Pred ) ); |
|---|
| 2602 |
|
|---|
| 2603 |
|
|---|
| 2604 |
bool fn( Elem[] setA, Elem[] setB, Pred pred = Pred.init ) |
|---|
| 2605 |
{ |
|---|
| 2606 |
size_t posA = 0, |
|---|
| 2607 |
posB = 0; |
|---|
| 2608 |
|
|---|
| 2609 |
while( posA < setA.length && posB < setB.length ) |
|---|
| 2610 |
{ |
|---|
| 2611 |
if( pred( setB[posB], setA[posA] ) ) |
|---|
| 2612 |
return false; |
|---|
| 2613 |
else if( pred( setA[posA], setB[posB] ) ) |
|---|
| 2614 |
++posA; |
|---|
| 2615 |
else |
|---|
| 2616 |
++posA, ++posB; |
|---|
| 2617 |
} |
|---|
| 2618 |
return posB == setB.length; |
|---|
| 2619 |
} |
|---|
| 2620 |
} |
|---|
| 2621 |
|
|---|
| 2622 |
|
|---|
| 2623 |
template includes( BufA, BufB ) |
|---|
| 2624 |
{ |
|---|
| 2625 |
bool includes( BufA setA, BufB setB ) |
|---|
| 2626 |
{ |
|---|
| 2627 |
return includes_!(ElemTypeOf!(BufA)).fn( setA, setB ); |
|---|
| 2628 |
} |
|---|
| 2629 |
} |
|---|
| 2630 |
|
|---|
| 2631 |
|
|---|
| 2632 |
template includes( BufA, BufB, Pred ) |
|---|
| 2633 |
{ |
|---|
| 2634 |
bool includes( BufA setA, BufB setB, Pred pred ) |
|---|
| 2635 |
{ |
|---|
| 2636 |
return includes_!(ElemTypeOf!(BufA), Pred).fn( setA, setB, pred ); |
|---|
| 2637 |
} |
|---|
| 2638 |
} |
|---|
| 2639 |
|
|---|
| 2640 |
|
|---|
| 2641 |
debug( UnitTest ) |
|---|
| 2642 |
{ |
|---|
| 2643 |
unittest |
|---|
| 2644 |
{ |
|---|
| 2645 |
assert( includes( "abcdefg", "a" ) ); |
|---|
| 2646 |
assert( includes( "abcdefg", "g" ) ); |
|---|
| 2647 |
assert( includes( "abcdefg", "d" ) ); |
|---|
| 2648 |
assert( includes( "abcdefg", "abcdefg" ) ); |
|---|
| 2649 |
assert( includes( "aaaabbbcdddefgg", "abbbcdefg" ) ); |
|---|
| 2650 |
|
|---|
| 2651 |
assert( !includes( "abcdefg", "aaabcdefg" ) ); |
|---|
| 2652 |
assert( !includes( "abcdefg", "abcdefggg" ) ); |
|---|
| 2653 |
assert( !includes( "abbbcdefg", "abbbbcdefg" ) ); |
|---|
| 2654 |
} |
|---|
| 2655 |
} |
|---|
| 2656 |
} |
|---|
| 2657 |
|
|---|
| 2658 |
|
|---|
| 2659 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2660 |
// Union Of |
|---|
| 2661 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2662 |
|
|---|
| 2663 |
|
|---|
| 2664 |
version( TangoDoc ) |
|---|
| 2665 |
{ |
|---|
| 2666 |
/** |
|---|
| 2667 |
* Computes the union of setA and setB as a set operation and returns the |
|---|
| 2668 |
* retult in a new sorted array. Both setA and setB are required to be |
|---|
| 2669 |
* sorted. If either setA or setB contain duplicates, the result will |
|---|
| 2670 |
* contain the larger number of duplicates from setA and setB. When an |
|---|
| 2671 |
* overlap occurs, entries will be copied from setA. Comparisons will be |
|---|
| 2672 |
* performed using the supplied predicate or '<' if none is supplied. |
|---|
| 2673 |
* |
|---|
| 2674 |
* Params: |
|---|
| 2675 |
* setA = The first sorted array to evaluate. |
|---|
| 2676 |
* setB = The second sorted array to evaluate. |
|---|
| 2677 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2678 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2679 |
* callable type. |
|---|
| 2680 |
* |
|---|
| 2681 |
* Returns: |
|---|
| 2682 |
* A new array containing the union of setA and setB. |
|---|
| 2683 |
*/ |
|---|
| 2684 |
Elem[] unionOf( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); |
|---|
| 2685 |
} |
|---|
| 2686 |
else |
|---|
| 2687 |
{ |
|---|
| 2688 |
template unionOf_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2689 |
{ |
|---|
| 2690 |
static assert( isCallableType!(Pred ) ); |
|---|
| 2691 |
|
|---|
| 2692 |
|
|---|
| 2693 |
Elem[] fn( Elem[] setA, Elem[] setB, Pred pred = Pred.init ) |
|---|
| 2694 |
{ |
|---|
| 2695 |
size_t posA = 0, |
|---|
| 2696 |
posB = 0; |
|---|
| 2697 |
Elem[] setU; |
|---|
| 2698 |
|
|---|
| 2699 |
while( posA < setA.length && posB < setB.length ) |
|---|
| 2700 |
{ |
|---|
| 2701 |
if( pred( setA[posA], setB[posB] ) ) |
|---|
| 2702 |
setU ~= setA[posA++]; |
|---|
| 2703 |
else if( pred( setB[posB], setA[posA] ) ) |
|---|
| 2704 |
setU ~= setB[posB++]; |
|---|
| 2705 |
else |
|---|
| 2706 |
setU ~= setA[posA++], posB++; |
|---|
| 2707 |
} |
|---|
| 2708 |
setU ~= setA[posA .. $]; |
|---|
| 2709 |
setU ~= setB[posB .. $]; |
|---|
| 2710 |
return setU; |
|---|
| 2711 |
} |
|---|
| 2712 |
} |
|---|
| 2713 |
|
|---|
| 2714 |
|
|---|
| 2715 |
template unionOf( BufA, BufB ) |
|---|
| 2716 |
{ |
|---|
| 2717 |
ElemTypeOf!(BufA)[] unionOf( BufA setA, BufB setB ) |
|---|
| 2718 |
{ |
|---|
| 2719 |
return unionOf_!(ElemTypeOf!(BufA)).fn( setA, setB ); |
|---|
| 2720 |
} |
|---|
| 2721 |
} |
|---|
| 2722 |
|
|---|
| 2723 |
|
|---|
| 2724 |
template unionOf( BufA, BufB, Pred ) |
|---|
| 2725 |
{ |
|---|
| 2726 |
ElemTypeOf!(BufA)[] unionOf( BufA setA, BufB setB, Pred pred ) |
|---|
| 2727 |
{ |
|---|
| 2728 |
return unionOf_!(ElemTypeOf!(BufA), Pred).fn( setA, setB, pred ); |
|---|
| 2729 |
} |
|---|
| 2730 |
} |
|---|
| 2731 |
|
|---|
| 2732 |
|
|---|
| 2733 |
debug( UnitTest ) |
|---|
| 2734 |
{ |
|---|
| 2735 |
unittest |
|---|
| 2736 |
{ |
|---|
| 2737 |
assert( unionOf( "", "" ) == "" ); |
|---|
| 2738 |
assert( unionOf( "abc", "def" ) == "abcdef" ); |
|---|
| 2739 |
assert( unionOf( "abbbcd", "aadeefg" ) == "aabbbcdeefg" ); |
|---|
| 2740 |
} |
|---|
| 2741 |
} |
|---|
| 2742 |
} |
|---|
| 2743 |
|
|---|
| 2744 |
|
|---|
| 2745 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2746 |
// Intersection Of |
|---|
| 2747 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2748 |
|
|---|
| 2749 |
|
|---|
| 2750 |
version( TangoDoc ) |
|---|
| 2751 |
{ |
|---|
| 2752 |
/** |
|---|
| 2753 |
* Computes the intersection of setA and setB as a set operation and |
|---|
| 2754 |
* returns the retult in a new sorted array. Both setA and setB are |
|---|
| 2755 |
* required to be sorted. If either setA or setB contain duplicates, the |
|---|
| 2756 |
* result will contain the smaller number of duplicates from setA and setB. |
|---|
| 2757 |
* All entries will be copied from setA. Comparisons will be performed |
|---|
| 2758 |
* using the supplied predicate or '<' if none is supplied. |
|---|
| 2759 |
* |
|---|
| 2760 |
* Params: |
|---|
| 2761 |
* setA = The first sorted array to evaluate. |
|---|
| 2762 |
* setB = The second sorted array to evaluate. |
|---|
| 2763 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2764 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2765 |
* callable type. |
|---|
| 2766 |
* |
|---|
| 2767 |
* Returns: |
|---|
| 2768 |
* A new array containing the intersection of setA and setB. |
|---|
| 2769 |
*/ |
|---|
| 2770 |
Elem[] intersectionOf( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); |
|---|
| 2771 |
} |
|---|
| 2772 |
else |
|---|
| 2773 |
{ |
|---|
| 2774 |
template intersectionOf_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2775 |
{ |
|---|
| 2776 |
static assert( isCallableType!(Pred ) ); |
|---|
| 2777 |
|
|---|
| 2778 |
|
|---|
| 2779 |
Elem[] fn( Elem[] setA, Elem[] setB, Pred pred = Pred.init ) |
|---|
| 2780 |
{ |
|---|
| 2781 |
size_t posA = 0, |
|---|
| 2782 |
posB = 0; |
|---|
| 2783 |
Elem[] setI; |
|---|
| 2784 |
|
|---|
| 2785 |
while( posA < setA.length && posB < setB.length ) |
|---|
| 2786 |
{ |
|---|
| 2787 |
if( pred( setA[posA], setB[posB] ) ) |
|---|
| 2788 |
++posA; |
|---|
| 2789 |
else if( pred( setB[posB], setA[posA] ) ) |
|---|
| 2790 |
++posB; |
|---|
| 2791 |
else |
|---|
| 2792 |
setI ~= setA[posA++], posB++; |
|---|
| 2793 |
} |
|---|
| 2794 |
return setI; |
|---|
| 2795 |
} |
|---|
| 2796 |
} |
|---|
| 2797 |
|
|---|
| 2798 |
|
|---|
| 2799 |
template intersectionOf( BufA, BufB ) |
|---|
| 2800 |
{ |
|---|
| 2801 |
ElemTypeOf!(BufA)[] intersectionOf( BufA setA, BufB setB ) |
|---|
| 2802 |
{ |
|---|
| 2803 |
return intersectionOf_!(ElemTypeOf!(BufA)).fn( setA, setB ); |
|---|
| 2804 |
} |
|---|
| 2805 |
} |
|---|
| 2806 |
|
|---|
| 2807 |
|
|---|
| 2808 |
template intersectionOf( BufA, BufB, Pred ) |
|---|
| 2809 |
{ |
|---|
| 2810 |
ElemTypeOf!(BufA)[] intersectionOf( BufA setA, BufB setB, Pred pred ) |
|---|
| 2811 |
{ |
|---|
| 2812 |
return intersectionOf_!(ElemTypeOf!(BufA), Pred).fn( setA, setB, pred ); |
|---|
| 2813 |
} |
|---|
| 2814 |
} |
|---|
| 2815 |
|
|---|
| 2816 |
|
|---|
| 2817 |
debug( UnitTest ) |
|---|
| 2818 |
{ |
|---|
| 2819 |
unittest |
|---|
| 2820 |
{ |
|---|
| 2821 |
assert( intersectionOf( "", "" ) == "" ); |
|---|
| 2822 |
assert( intersectionOf( "abc", "def" ) == "" ); |
|---|
| 2823 |
assert( intersectionOf( "abbbcd", "aabdddeefg" ) == "abd" ); |
|---|
| 2824 |
} |
|---|
| 2825 |
} |
|---|
| 2826 |
} |
|---|
| 2827 |
|
|---|
| 2828 |
|
|---|
| 2829 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2830 |
// Missing From |
|---|
| 2831 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2832 |
|
|---|
| 2833 |
|
|---|
| 2834 |
version( TangoDoc ) |
|---|
| 2835 |
{ |
|---|
| 2836 |
/** |
|---|
| 2837 |
* Returns a new array containing all elements in setA which are not |
|---|
| 2838 |
* present in setB. Both setA and setB are required to be sorted. |
|---|
| 2839 |
* Comparisons will be performed using the supplied predicate or '<' |
|---|
| 2840 |
* if none is supplied. |
|---|
| 2841 |
* |
|---|
| 2842 |
* Params: |
|---|
| 2843 |
* setA = The first sorted array to evaluate. |
|---|
| 2844 |
* setB = The second sorted array to evaluate. |
|---|
| 2845 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2846 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2847 |
* callable type. |
|---|
| 2848 |
* |
|---|
| 2849 |
* Returns: |
|---|
| 2850 |
* A new array containing the elements in setA that are not in setB. |
|---|
| 2851 |
*/ |
|---|
| 2852 |
Elem[] missingFrom( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); |
|---|
| 2853 |
} |
|---|
| 2854 |
else |
|---|
| 2855 |
{ |
|---|
| 2856 |
template missingFrom_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2857 |
{ |
|---|
| 2858 |
static assert( isCallableType!(Pred ) ); |
|---|
| 2859 |
|
|---|
| 2860 |
|
|---|
| 2861 |
Elem[] fn( Elem[] setA, Elem[] setB, Pred pred = Pred.init ) |
|---|
| 2862 |
{ |
|---|
| 2863 |
size_t posA = 0, |
|---|
| 2864 |
posB = 0; |
|---|
| 2865 |
Elem[] setM; |
|---|
| 2866 |
|
|---|
| 2867 |
while( posA < setA.length && posB < setB.length ) |
|---|
| 2868 |
{ |
|---|
| 2869 |
if( pred( setA[posA], setB[posB] ) ) |
|---|
| 2870 |
setM ~= setA[posA++]; |
|---|
| 2871 |
else if( pred( setB[posB], setA[posA] ) ) |
|---|
| 2872 |
++posB; |
|---|
| 2873 |
else |
|---|
| 2874 |
++posA, ++posB; |
|---|
| 2875 |
} |
|---|
| 2876 |
setM ~= setA[posA .. $]; |
|---|
| 2877 |
return setM; |
|---|
| 2878 |
} |
|---|
| 2879 |
} |
|---|
| 2880 |
|
|---|
| 2881 |
|
|---|
| 2882 |
template missingFrom( BufA, BufB ) |
|---|
| 2883 |
{ |
|---|
| 2884 |
ElemTypeOf!(BufA)[] missingFrom( BufA setA, BufB setB ) |
|---|
| 2885 |
{ |
|---|
| 2886 |
return missingFrom_!(ElemTypeOf!(BufA)).fn( setA, setB ); |
|---|
| 2887 |
} |
|---|
| 2888 |
} |
|---|
| 2889 |
|
|---|
| 2890 |
|
|---|
| 2891 |
template missingFrom( BufA, BufB, Pred ) |
|---|
| 2892 |
{ |
|---|
| 2893 |
ElemTypeOf!(BufA)[] missingFrom( BufA setA, BufB setB, Pred pred ) |
|---|
| 2894 |
{ |
|---|
| 2895 |
return missingFrom_!(ElemTypeOf!(BufA), Pred).fn( setA, setB, pred ); |
|---|
| 2896 |
} |
|---|
| 2897 |
} |
|---|
| 2898 |
|
|---|
| 2899 |
|
|---|
| 2900 |
debug( UnitTest ) |
|---|
| 2901 |
{ |
|---|
| 2902 |
unittest |
|---|
| 2903 |
{ |
|---|
| 2904 |
assert( missingFrom( "", "" ) == "" ); |
|---|
| 2905 |
assert( missingFrom( "", "abc" ) == "" ); |
|---|
| 2906 |
assert( missingFrom( "abc", "" ) == "abc" ); |
|---|
| 2907 |
assert( missingFrom( "abc", "abc" ) == "" ); |
|---|
| 2908 |
assert( missingFrom( "abc", "def" ) == "abc" ); |
|---|
| 2909 |
assert( missingFrom( "abbbcd", "abd" ) == "bbc" ); |
|---|
| 2910 |
assert( missingFrom( "abcdef", "bc" ) == "adef" ); |
|---|
| 2911 |
} |
|---|
| 2912 |
} |
|---|
| 2913 |
} |
|---|
| 2914 |
|
|---|
| 2915 |
|
|---|
| 2916 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2917 |
// Difference Of |
|---|
| 2918 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 2919 |
|
|---|
| 2920 |
|
|---|
| 2921 |
version( TangoDoc ) |
|---|
| 2922 |
{ |
|---|
| 2923 |
/** |
|---|
| 2924 |
* Returns a new array containing all elements in setA which are not |
|---|
| 2925 |
* present in setB and the elements in setB which are not present in |
|---|
| 2926 |
* setA. Both setA and setB are required to be sorted. Comparisons |
|---|
| 2927 |
* will be performed using the supplied predicate or '<' if none is |
|---|
| 2928 |
* supplied. |
|---|
| 2929 |
* |
|---|
| 2930 |
* Params: |
|---|
| 2931 |
* setA = The first sorted array to evaluate. |
|---|
| 2932 |
* setB = The second sorted array to evaluate. |
|---|
| 2933 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 2934 |
* less than e2 and false if not. This predicate may be any |
|---|
| 2935 |
* callable type. |
|---|
| 2936 |
* |
|---|
| 2937 |
* Returns: |
|---|
| 2938 |
* A new array containing the elements in setA that are not in setB |
|---|
| 2939 |
* and the elements in setB that are not in setA. |
|---|
| 2940 |
*/ |
|---|
| 2941 |
Elem[] differenceOf( Elem[] setA, Elem[] setB, Pred2E pred = Pred2E.init ); |
|---|
| 2942 |
} |
|---|
| 2943 |
else |
|---|
| 2944 |
{ |
|---|
| 2945 |
template differenceOf_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 2946 |
{ |
|---|
| 2947 |
static assert( isCallableType!(Pred ) ); |
|---|
| 2948 |
|
|---|
| 2949 |
|
|---|
| 2950 |
Elem[] fn( Elem[] setA, Elem[] setB, Pred pred = Pred.init ) |
|---|
| 2951 |
{ |
|---|
| 2952 |
size_t posA = 0, |
|---|
| 2953 |
posB = 0; |
|---|
| 2954 |
Elem[] setD; |
|---|
| 2955 |
|
|---|
| 2956 |
while( posA < setA.length && posB < setB.length ) |
|---|
| 2957 |
{ |
|---|
| 2958 |
if( pred( setA[posA], setB[posB] ) ) |
|---|
| 2959 |
setD ~= setA[posA++]; |
|---|
| 2960 |
else if( pred( setB[posB], setA[posA] ) ) |
|---|
| 2961 |
setD ~= setB[posB++]; |
|---|
| 2962 |
else |
|---|
| 2963 |
++posA, ++posB; |
|---|
| 2964 |
} |
|---|
| 2965 |
setD ~= setA[posA .. $]; |
|---|
| 2966 |
setD ~= setB[posB .. $]; |
|---|
| 2967 |
return setD; |
|---|
| 2968 |
} |
|---|
| 2969 |
} |
|---|
| 2970 |
|
|---|
| 2971 |
|
|---|
| 2972 |
template differenceOf( BufA, BufB ) |
|---|
| 2973 |
{ |
|---|
| 2974 |
ElemTypeOf!(BufA)[] differenceOf( BufA setA, BufB setB ) |
|---|
| 2975 |
{ |
|---|
| 2976 |
return differenceOf_!(ElemTypeOf!(BufA)).fn( setA, setB ); |
|---|
| 2977 |
} |
|---|
| 2978 |
} |
|---|
| 2979 |
|
|---|
| 2980 |
|
|---|
| 2981 |
template differenceOf( BufA, BufB, Pred ) |
|---|
| 2982 |
{ |
|---|
| 2983 |
ElemTypeOf!(BufA)[] differenceOf( BufA setA, BufB setB, Pred pred ) |
|---|
| 2984 |
{ |
|---|
| 2985 |
return differenceOf_!(ElemTypeOf!(BufA), Pred).fn( setA, setB, pred ); |
|---|
| 2986 |
} |
|---|
| 2987 |
} |
|---|
| 2988 |
|
|---|
| 2989 |
|
|---|
| 2990 |
debug( UnitTest ) |
|---|
| 2991 |
{ |
|---|
| 2992 |
unittest |
|---|
| 2993 |
{ |
|---|
| 2994 |
assert( differenceOf( "", "" ) == "" ); |
|---|
| 2995 |
assert( differenceOf( "", "abc" ) == "abc" ); |
|---|
| 2996 |
assert( differenceOf( "abc", "" ) == "abc" ); |
|---|
| 2997 |
assert( differenceOf( "abc", "abc" ) == "" ); |
|---|
| 2998 |
assert( differenceOf( "abc", "def" ) == "abcdef" ); |
|---|
| 2999 |
assert( differenceOf( "abbbcd", "abd" ) == "bbc" ); |
|---|
| 3000 |
assert( differenceOf( "abd", "abbbcd" ) == "bbc" ); |
|---|
| 3001 |
} |
|---|
| 3002 |
} |
|---|
| 3003 |
} |
|---|
| 3004 |
|
|---|
| 3005 |
|
|---|
| 3006 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 3007 |
// Make Heap |
|---|
| 3008 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 3009 |
|
|---|
| 3010 |
|
|---|
| 3011 |
version( TangoDoc ) |
|---|
| 3012 |
{ |
|---|
| 3013 |
/** |
|---|
| 3014 |
* Converts buf to a heap using the supplied predicate or '<' if none is |
|---|
| 3015 |
* supplied. |
|---|
| 3016 |
* |
|---|
| 3017 |
* Params: |
|---|
| 3018 |
* buf = The array to convert. This parameter is not marked 'ref' to |
|---|
| 3019 |
* allow temporary slices to be sorted. As buf is not resized in |
|---|
| 3020 |
* any way, omitting the 'ref' qualifier has no effect on the |
|---|
| 3021 |
* result of this operation, even though it may be viewed as a |
|---|
| 3022 |
* side-effect. |
|---|
| 3023 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 3024 |
* less than e2 and false if not. This predicate may be any |
|---|
| 3025 |
* callable type. |
|---|
| 3026 |
*/ |
|---|
| 3027 |
void makeHeap( Elem[] buf, Pred2E pred = Pred2E.init ); |
|---|
| 3028 |
} |
|---|
| 3029 |
else |
|---|
| 3030 |
{ |
|---|
| 3031 |
template makeHeap_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 3032 |
{ |
|---|
| 3033 |
static assert( isCallableType!(Pred ) ); |
|---|
| 3034 |
|
|---|
| 3035 |
|
|---|
| 3036 |
void fn( Elem[] buf, Pred pred = Pred.init ) |
|---|
| 3037 |
{ |
|---|
| 3038 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 3039 |
// not inline the reference-based version. |
|---|
| 3040 |
void exch( size_t p1, size_t p2 ) |
|---|
| 3041 |
{ |
|---|
| 3042 |
Elem t = buf[p1]; |
|---|
| 3043 |
buf[p1] = buf[p2]; |
|---|
| 3044 |
buf[p2] = t; |
|---|
| 3045 |
} |
|---|
| 3046 |
|
|---|
| 3047 |
void fixDown( size_t pos, size_t end ) |
|---|
| 3048 |
{ |
|---|
| 3049 |
if( end <= pos ) |
|---|
| 3050 |
return; |
|---|
| 3051 |
while( pos <= ( end - 1 ) / 2 ) |
|---|
| 3052 |
{ |
|---|
| 3053 |
size_t nxt = 2 * pos + 1; |
|---|
| 3054 |
|
|---|
| 3055 |
if( nxt < end && pred( buf[nxt], buf[nxt + 1] ) ) |
|---|
| 3056 |
++nxt; |
|---|
| 3057 |
if( !pred( buf[pos], buf[nxt] ) ) |
|---|
| 3058 |
break; |
|---|
| 3059 |
exch( pos, nxt ); |
|---|
| 3060 |
pos = nxt; |
|---|
| 3061 |
} |
|---|
| 3062 |
} |
|---|
| 3063 |
|
|---|
| 3064 |
if( buf.length < 2 ) |
|---|
| 3065 |
return; |
|---|
| 3066 |
|
|---|
| 3067 |
size_t end = buf.length - 1, |
|---|
| 3068 |
pos = end / 2 + 2; |
|---|
| 3069 |
|
|---|
| 3070 |
do |
|---|
| 3071 |
{ |
|---|
| 3072 |
fixDown( --pos, end ); |
|---|
| 3073 |
} while( pos > 0 ); |
|---|
| 3074 |
} |
|---|
| 3075 |
} |
|---|
| 3076 |
|
|---|
| 3077 |
|
|---|
| 3078 |
template makeHeap( Buf ) |
|---|
| 3079 |
{ |
|---|
| 3080 |
void makeHeap( Buf buf ) |
|---|
| 3081 |
{ |
|---|
| 3082 |
return makeHeap_!(ElemTypeOf!(Buf)).fn( buf ); |
|---|
| 3083 |
} |
|---|
| 3084 |
} |
|---|
| 3085 |
|
|---|
| 3086 |
|
|---|
| 3087 |
template makeHeap( Buf, Pred ) |
|---|
| 3088 |
{ |
|---|
| 3089 |
void makeHeap( Buf buf, Pred pred ) |
|---|
| 3090 |
{ |
|---|
| 3091 |
return makeHeap_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 3092 |
} |
|---|
| 3093 |
} |
|---|
| 3094 |
|
|---|
| 3095 |
|
|---|
| 3096 |
debug( UnitTest ) |
|---|
| 3097 |
{ |
|---|
| 3098 |
unittest |
|---|
| 3099 |
{ |
|---|
| 3100 |
void basic( char[] buf ) |
|---|
| 3101 |
{ |
|---|
| 3102 |
if( buf.length < 2 ) |
|---|
| 3103 |
return; |
|---|
| 3104 |
|
|---|
| 3105 |
size_t pos = 0, |
|---|
| 3106 |
end = buf.length - 1; |
|---|
| 3107 |
|
|---|
| 3108 |
while( pos <= ( end - 1 ) / 2 ) |
|---|
| 3109 |
{ |
|---|
| 3110 |
assert( buf[pos] >= buf[2 * pos + 1] ); |
|---|
| 3111 |
if( 2 * pos + 1 < end ) |
|---|
| 3112 |
{ |
|---|
| 3113 |
assert( buf[pos] >= buf[2 * pos + 2] ); |
|---|
| 3114 |
} |
|---|
| 3115 |
++pos; |
|---|
| 3116 |
} |
|---|
| 3117 |
} |
|---|
| 3118 |
|
|---|
| 3119 |
void test( char[] buf ) |
|---|
| 3120 |
{ |
|---|
| 3121 |
makeHeap( buf ); |
|---|
| 3122 |
basic( buf ); |
|---|
| 3123 |
} |
|---|
| 3124 |
|
|---|
| 3125 |
test( "mkcvalsidivjoaisjdvmzlksvdjioawmdsvmsdfefewv".dup ); |
|---|
| 3126 |
test( "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf".dup ); |
|---|
| 3127 |
test( "the quick brown fox jumped over the lazy dog".dup ); |
|---|
| 3128 |
test( "abcdefghijklmnopqrstuvwxyz".dup ); |
|---|
| 3129 |
test( "zyxwvutsrqponmlkjihgfedcba".dup ); |
|---|
| 3130 |
test( "ba".dup ); |
|---|
| 3131 |
test( "a".dup ); |
|---|
| 3132 |
test( "".dup ); |
|---|
| 3133 |
} |
|---|
| 3134 |
} |
|---|
| 3135 |
} |
|---|
| 3136 |
|
|---|
| 3137 |
|
|---|
| 3138 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 3139 |
// Push Heap |
|---|
| 3140 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 3141 |
|
|---|
| 3142 |
|
|---|
| 3143 |
version( TangoDoc ) |
|---|
| 3144 |
{ |
|---|
| 3145 |
/** |
|---|
| 3146 |
* Adds val to buf by appending it and adjusting it up the heap. |
|---|
| 3147 |
* |
|---|
| 3148 |
* Params: |
|---|
| 3149 |
* buf = The heap to modify. This parameter is marked 'ref' because |
|---|
| 3150 |
* buf.length will be altered. |
|---|
| 3151 |
* val = The element to push onto buf. |
|---|
| 3152 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 3153 |
* less than e2 and false if not. This predicate may be any |
|---|
| 3154 |
* callable type. |
|---|
| 3155 |
*/ |
|---|
| 3156 |
void pushHeap( ref Elem[] buf, Elem val, Pred2E pred = Pred2E.init ); |
|---|
| 3157 |
} |
|---|
| 3158 |
else |
|---|
| 3159 |
{ |
|---|
| 3160 |
template pushHeap_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 3161 |
{ |
|---|
| 3162 |
static assert( isCallableType!(Pred ) ); |
|---|
| 3163 |
|
|---|
| 3164 |
|
|---|
| 3165 |
void fn( ref Elem[] buf, Elem val, Pred pred = Pred.init ) |
|---|
| 3166 |
{ |
|---|
| 3167 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 3168 |
// not inline the reference-based version. |
|---|
| 3169 |
void exch( size_t p1, size_t p2 ) |
|---|
| 3170 |
{ |
|---|
| 3171 |
Elem t = buf[p1]; |
|---|
| 3172 |
buf[p1] = buf[p2]; |
|---|
| 3173 |
buf[p2] = t; |
|---|
| 3174 |
} |
|---|
| 3175 |
|
|---|
| 3176 |
void fixUp( size_t pos ) |
|---|
| 3177 |
{ |
|---|
| 3178 |
if( pos < 1 ) |
|---|
| 3179 |
return; |
|---|
| 3180 |
size_t par = ( pos - 1 ) / 2; |
|---|
| 3181 |
while( pos > 0 && pred( buf[par], buf[pos] ) ) |
|---|
| 3182 |
{ |
|---|
| 3183 |
exch( par, pos ); |
|---|
| 3184 |
pos = par; |
|---|
| 3185 |
par = ( pos - 1 ) / 2; |
|---|
| 3186 |
} |
|---|
| 3187 |
} |
|---|
| 3188 |
|
|---|
| 3189 |
buf ~= val; |
|---|
| 3190 |
if( buf.length > 1 ) |
|---|
| 3191 |
{ |
|---|
| 3192 |
fixUp( buf.length - 1 ); |
|---|
| 3193 |
} |
|---|
| 3194 |
} |
|---|
| 3195 |
} |
|---|
| 3196 |
|
|---|
| 3197 |
|
|---|
| 3198 |
template pushHeap( Buf, Val ) |
|---|
| 3199 |
{ |
|---|
| 3200 |
void pushHeap( ref Buf buf, Val val ) |
|---|
| 3201 |
{ |
|---|
| 3202 |
return pushHeap_!(ElemTypeOf!(Buf)).fn( buf, val ); |
|---|
| 3203 |
} |
|---|
| 3204 |
} |
|---|
| 3205 |
|
|---|
| 3206 |
|
|---|
| 3207 |
template pushHeap( Buf, Val, Pred ) |
|---|
| 3208 |
{ |
|---|
| 3209 |
void pushHeap( ref Buf buf, Val val, Pred pred ) |
|---|
| 3210 |
{ |
|---|
| 3211 |
return pushHeap_!(ElemTypeOf!(Buf), Pred).fn( buf, val, pred ); |
|---|
| 3212 |
} |
|---|
| 3213 |
} |
|---|
| 3214 |
|
|---|
| 3215 |
|
|---|
| 3216 |
debug( UnitTest ) |
|---|
| 3217 |
{ |
|---|
| 3218 |
unittest |
|---|
| 3219 |
{ |
|---|
| 3220 |
void basic( char[] buf ) |
|---|
| 3221 |
{ |
|---|
| 3222 |
if( buf.length < 2 ) |
|---|
| 3223 |
return; |
|---|
| 3224 |
|
|---|
| 3225 |
size_t pos = 0, |
|---|
| 3226 |
end = buf.length - 1; |
|---|
| 3227 |
|
|---|
| 3228 |
while( pos <= ( end - 1 ) / 2 ) |
|---|
| 3229 |
{ |
|---|
| 3230 |
assert( buf[pos] >= buf[2 * pos + 1] ); |
|---|
| 3231 |
if( 2 * pos + 1 < end ) |
|---|
| 3232 |
{ |
|---|
| 3233 |
assert( buf[pos] >= buf[2 * pos + 2] ); |
|---|
| 3234 |
} |
|---|
| 3235 |
++pos; |
|---|
| 3236 |
} |
|---|
| 3237 |
} |
|---|
| 3238 |
|
|---|
| 3239 |
char[] buf; |
|---|
| 3240 |
|
|---|
| 3241 |
foreach( cur; "abcdefghijklmnopqrstuvwxyz" ) |
|---|
| 3242 |
{ |
|---|
| 3243 |
pushHeap( buf, cur ); |
|---|
| 3244 |
basic( buf ); |
|---|
| 3245 |
} |
|---|
| 3246 |
|
|---|
| 3247 |
buf.length = 0; |
|---|
| 3248 |
|
|---|
| 3249 |
foreach( cur; "zyxwvutsrqponmlkjihgfedcba" ) |
|---|
| 3250 |
{ |
|---|
| 3251 |
pushHeap( buf, cur ); |
|---|
| 3252 |
basic( buf ); |
|---|
| 3253 |
} |
|---|
| 3254 |
} |
|---|
| 3255 |
} |
|---|
| 3256 |
} |
|---|
| 3257 |
|
|---|
| 3258 |
|
|---|
| 3259 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 3260 |
// Pop Heap |
|---|
| 3261 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 3262 |
|
|---|
| 3263 |
|
|---|
| 3264 |
version( TangoDoc ) |
|---|
| 3265 |
{ |
|---|
| 3266 |
/** |
|---|
| 3267 |
* Removes the top element from buf by swapping it with the bottom element, |
|---|
| 3268 |
* adjusting it down the heap, and reducing the length of buf by one. |
|---|
| 3269 |
* |
|---|
| 3270 |
* Params: |
|---|
| 3271 |
* buf = The heap to modify. This parameter is marked 'ref' because |
|---|
| 3272 |
* buf.length will be altered. |
|---|
| 3273 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 3274 |
* less than e2 and false if not. This predicate may be any |
|---|
| 3275 |
* callable type. |
|---|
| 3276 |
*/ |
|---|
| 3277 |
void popHeap( ref Elem[] buf, Pred2E pred = Pred2E.init ); |
|---|
| 3278 |
} |
|---|
| 3279 |
else |
|---|
| 3280 |
{ |
|---|
| 3281 |
template popHeap_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 3282 |
{ |
|---|
| 3283 |
static assert( isCallableType!(Pred ) ); |
|---|
| 3284 |
|
|---|
| 3285 |
|
|---|
| 3286 |
void fn( ref Elem[] buf, Pred pred = Pred.init ) |
|---|
| 3287 |
{ |
|---|
| 3288 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 3289 |
// not inline the reference-based version. |
|---|
| 3290 |
void exch( size_t p1, size_t p2 ) |
|---|
| 3291 |
{ |
|---|
| 3292 |
Elem t = buf[p1]; |
|---|
| 3293 |
buf[p1] = buf[p2]; |
|---|
| 3294 |
buf[p2] = t; |
|---|
| 3295 |
} |
|---|
| 3296 |
|
|---|
| 3297 |
void fixDown( size_t pos, size_t end ) |
|---|
| 3298 |
{ |
|---|
| 3299 |
if( end <= pos ) |
|---|
| 3300 |
return; |
|---|
| 3301 |
while( pos <= ( end - 1 ) / 2 ) |
|---|
| 3302 |
{ |
|---|
| 3303 |
size_t nxt = 2 * pos + 1; |
|---|
| 3304 |
|
|---|
| 3305 |
if( nxt < end && pred( buf[nxt], buf[nxt + 1] ) ) |
|---|
| 3306 |
++nxt; |
|---|
| 3307 |
if( !pred( buf[pos], buf[nxt] ) ) |
|---|
| 3308 |
break; |
|---|
| 3309 |
exch( pos, nxt ); |
|---|
| 3310 |
pos = nxt; |
|---|
| 3311 |
} |
|---|
| 3312 |
} |
|---|
| 3313 |
|
|---|
| 3314 |
if( buf.length > 1 ) |
|---|
| 3315 |
{ |
|---|
| 3316 |
exch( 0, buf.length - 1 ); |
|---|
| 3317 |
fixDown( 0, buf.length - 2 ); |
|---|
| 3318 |
} |
|---|
| 3319 |
if( buf.length > 0 ) |
|---|
| 3320 |
{ |
|---|
| 3321 |
buf.length = buf.length - 1; |
|---|
| 3322 |
} |
|---|
| 3323 |
} |
|---|
| 3324 |
} |
|---|
| 3325 |
|
|---|
| 3326 |
|
|---|
| 3327 |
template popHeap( Buf ) |
|---|
| 3328 |
{ |
|---|
| 3329 |
void popHeap( ref Buf buf ) |
|---|
| 3330 |
{ |
|---|
| 3331 |
return popHeap_!(ElemTypeOf!(Buf)).fn( buf ); |
|---|
| 3332 |
} |
|---|
| 3333 |
} |
|---|
| 3334 |
|
|---|
| 3335 |
|
|---|
| 3336 |
template popHeap( Buf, Pred ) |
|---|
| 3337 |
{ |
|---|
| 3338 |
void popHeap( ref Buf buf, Pred pred ) |
|---|
| 3339 |
{ |
|---|
| 3340 |
return popHeap_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 3341 |
} |
|---|
| 3342 |
} |
|---|
| 3343 |
|
|---|
| 3344 |
|
|---|
| 3345 |
debug( UnitTest ) |
|---|
| 3346 |
{ |
|---|
| 3347 |
unittest |
|---|
| 3348 |
{ |
|---|
| 3349 |
void test( char[] buf ) |
|---|
| 3350 |
{ |
|---|
| 3351 |
if( buf.length < 2 ) |
|---|
| 3352 |
return; |
|---|
| 3353 |
|
|---|
| 3354 |
size_t pos = 0, |
|---|
| 3355 |
end = buf.length - 1; |
|---|
| 3356 |
|
|---|
| 3357 |
while( pos <= ( end - 1 ) / 2 ) |
|---|
| 3358 |
{ |
|---|
| 3359 |
assert( buf[pos] >= buf[2 * pos + 1] ); |
|---|
| 3360 |
if( 2 * pos + 1 < end ) |
|---|
| 3361 |
{ |
|---|
| 3362 |
assert( buf[pos] >= buf[2 * pos + 2] ); |
|---|
| 3363 |
} |
|---|
| 3364 |
++pos; |
|---|
| 3365 |
} |
|---|
| 3366 |
} |
|---|
| 3367 |
|
|---|
| 3368 |
char[] buf = "zyxwvutsrqponmlkjihgfedcba".dup; |
|---|
| 3369 |
|
|---|
| 3370 |
while( buf.length > 0 ) |
|---|
| 3371 |
{ |
|---|
| 3372 |
popHeap( buf ); |
|---|
| 3373 |
test( buf ); |
|---|
| 3374 |
} |
|---|
| 3375 |
} |
|---|
| 3376 |
} |
|---|
| 3377 |
} |
|---|
| 3378 |
|
|---|
| 3379 |
|
|---|
| 3380 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 3381 |
// Sort Heap |
|---|
| 3382 |
//////////////////////////////////////////////////////////////////////////////// |
|---|
| 3383 |
|
|---|
| 3384 |
|
|---|
| 3385 |
version( TangoDoc ) |
|---|
| 3386 |
{ |
|---|
| 3387 |
/** |
|---|
| 3388 |
* Sorts buf as a heap using the supplied predicate or '<' if none is |
|---|
| 3389 |
* supplied. Calling makeHeap and sortHeap on an array in succession |
|---|
| 3390 |
* has the effect of sorting the array using the heapsort algorithm. |
|---|
| 3391 |
* |
|---|
| 3392 |
* Params: |
|---|
| 3393 |
* buf = The heap to sort. This parameter is not marked 'ref' to |
|---|
| 3394 |
* allow temporary slices to be sorted. As buf is not resized |
|---|
| 3395 |
* in any way, omitting the 'ref' qualifier has no effect on |
|---|
| 3396 |
* the result of this operation, even though it may be viewed |
|---|
| 3397 |
* as a side-effect. |
|---|
| 3398 |
* pred = The evaluation predicate, which should return true if e1 is |
|---|
| 3399 |
* less than e2 and false if not. This predicate may be any |
|---|
| 3400 |
* callable type. |
|---|
| 3401 |
*/ |
|---|
| 3402 |
void sortHeap( Elem[] buf, Pred2E pred = Pred2E.init ); |
|---|
| 3403 |
} |
|---|
| 3404 |
else |
|---|
| 3405 |
{ |
|---|
| 3406 |
template sortHeap_( Elem, Pred = IsLess!(Elem) ) |
|---|
| 3407 |
{ |
|---|
| 3408 |
static assert( isCallableType!(Pred ) ); |
|---|
| 3409 |
|
|---|
| 3410 |
|
|---|
| 3411 |
void fn( Elem[] buf, Pred pred = Pred.init ) |
|---|
| 3412 |
{ |
|---|
| 3413 |
// NOTE: Indexes are passed instead of references because DMD does |
|---|
| 3414 |
// not inline the reference-based version. |
|---|
| 3415 |
void exch( size_t p1, size_t p2 ) |
|---|
| 3416 |
{ |
|---|
| 3417 |
Elem t = buf[p1]; |
|---|
| 3418 |
buf[p1] = buf[p2]; |
|---|
| 3419 |
buf[p2] = t; |
|---|
| 3420 |
} |
|---|
| 3421 |
|
|---|
| 3422 |
void fixDown( size_t pos, size_t end ) |
|---|
| 3423 |
{ |
|---|
| 3424 |
if( end <= pos ) |
|---|
| 3425 |
return; |
|---|
| 3426 |
while( pos <= ( end - 1 ) / 2 ) |
|---|
| 3427 |
{ |
|---|
| 3428 |
size_t nxt = 2 * pos + 1; |
|---|
| 3429 |
|
|---|
| 3430 |
if( nxt < end && pred( buf[nxt], buf[nxt + 1] ) ) |
|---|
| 3431 |
++nxt; |
|---|
| 3432 |
if( !pred( buf[pos], buf[nxt] ) ) |
|---|
| 3433 |
break; |
|---|
| 3434 |
exch( pos, nxt ); |
|---|
| 3435 |
pos = nxt; |
|---|
| 3436 |
} |
|---|
| 3437 |
} |
|---|
| 3438 |
|
|---|
| 3439 |
if( buf.length < 2 ) |
|---|
| 3440 |
return; |
|---|
| 3441 |
|
|---|
| 3442 |
size_t pos = buf.length - 1; |
|---|
| 3443 |
|
|---|
| 3444 |
while( pos > 0 ) |
|---|
| 3445 |
{ |
|---|
| 3446 |
exch( 0, pos ); |
|---|
| 3447 |
fixDown( 0, --pos ); |
|---|
| 3448 |
} |
|---|
| 3449 |
} |
|---|
| 3450 |
} |
|---|
| 3451 |
|
|---|
| 3452 |
|
|---|
| 3453 |
template sortHeap( Buf ) |
|---|
| 3454 |
{ |
|---|
| 3455 |
void sortHeap( Buf buf ) |
|---|
| 3456 |
{ |
|---|
| 3457 |
return sortHeap_!(ElemTypeOf!(Buf)).fn( buf ); |
|---|
| 3458 |
} |
|---|
| 3459 |
} |
|---|
| 3460 |
|
|---|
| 3461 |
|
|---|
| 3462 |
template sortHeap( Buf, Pred ) |
|---|
| 3463 |
{ |
|---|
| 3464 |
void sortHeap( Buf buf, Pred pred ) |
|---|
| 3465 |
{ |
|---|
| 3466 |
return sortHeap_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 3467 |
} |
|---|
| 3468 |
} |
|---|
| 3469 |
|
|---|
| 3470 |
|
|---|
| 3471 |
debug( UnitTest ) |
|---|
| 3472 |
{ |
|---|
| 3473 |
unittest |
|---|
| 3474 |
{ |
|---|
| 3475 |
char[] buf = "zyxwvutsrqponmlkjihgfedcba".dup; |
|---|
| 3476 |
|
|---|
| 3477 |
sortHeap( buf ); |
|---|
| 3478 |
char sav = buf[0]; |
|---|
| 3479 |
foreach( cur; buf ) |
|---|
| 3480 |
{ |
|---|
| 3481 |
assert( cur >= sav ); |
|---|
| 3482 |
sav = cur; |
|---|
| 3483 |
} |
|---|
| 3484 |
} |
|---|
| 3485 |
} |
|---|
| 3486 |
} |
|---|