@@ -189,14 +189,6 @@ type Keyspace interface {
189
189
// keyspace. This is only guaranteed to return an upper bound.
190
190
MaxLen () int
191
191
192
- // Slice returns the sub-keyspace from index i, inclusive, to index j,
193
- // exclusive. The receiver is unmodified.
194
- Slice (i , j uint64 ) Keyspace
195
-
196
- // EveryN returns a key space that includes 1 key for every N keys in the
197
- // original keyspace. The receiver is unmodified.
198
- EveryN (n uint64 ) Keyspace
199
-
200
192
// key writes the i-th key to the buffer and returns the length.
201
193
key (buf []byte , i uint64 ) int
202
194
}
@@ -205,8 +197,8 @@ type Keyspace interface {
205
197
// disjoint keys evenly distributed across the keyspace.
206
198
func Divvy (ks Keyspace , n uint64 ) []Keyspace {
207
199
ret := make ([]Keyspace , n )
208
- for i := uint64 ( 0 ); i < n ; i ++ {
209
- ret [i ] = ks . Slice (i , ks .Count ()). EveryN ( n )
200
+ for i := range n {
201
+ ret [i ] = EveryN ( Slice (ks , i , ks .Count ()), n )
210
202
}
211
203
return ret
212
204
}
@@ -312,19 +304,6 @@ func (a alphabet) MaxLen() int {
312
304
return a .maxLength
313
305
}
314
306
315
- func (a alphabet ) Slice (i , j uint64 ) Keyspace {
316
- s := a
317
- s .headSkip += i
318
- s .tailSkip += a .Count () - j
319
- return s
320
- }
321
-
322
- func (a alphabet ) EveryN (n uint64 ) Keyspace {
323
- s := a
324
- s .increment *= n
325
- return s
326
- }
327
-
328
307
func keyCount (n , l int ) uint64 {
329
308
// The number of representable keys in the keyspace is a function of the
330
309
// length of the alphabet n and the max key length l:
@@ -491,3 +470,70 @@ func assertLE(a, b []byte) {
491
470
panic (fmt .Sprintf ("invalid key ordering: %q > %q" , a , b ))
492
471
}
493
472
}
473
+
474
+ // Slice returns the sub-keyspace from index i, inclusive, to index j,
475
+ // exclusive. The receiver is unmodified.
476
+ func Slice (ks Keyspace , i , j uint64 ) Keyspace {
477
+ if i >= ks .Count () || j > ks .Count () {
478
+ panic (errors .AssertionFailedf ("invalid slice: [%d,%d) of keyspace with %d keys" , i , j , ks .Count ()))
479
+ }
480
+ return & sliceKeyspace {ks : ks , i : i , j : j }
481
+ }
482
+
483
+ type sliceKeyspace struct {
484
+ ks Keyspace
485
+ i , j uint64
486
+ }
487
+
488
+ // Count returns the number of keys that exist within this keyspace.
489
+ func (s * sliceKeyspace ) Count () uint64 {
490
+ return s .j - s .i
491
+ }
492
+
493
+ // MaxLen returns the maximum length, in bytes, of a key within this
494
+ // keyspace. This is only guaranteed to return an upper bound.
495
+ func (s * sliceKeyspace ) MaxLen () int {
496
+ return s .ks .MaxLen ()
497
+ }
498
+
499
+ // key writes the i-th key to the buffer and returns the length.
500
+ func (s * sliceKeyspace ) key (buf []byte , i uint64 ) int {
501
+ return s .ks .key (buf , s .i + i )
502
+ }
503
+
504
+ // EveryN returns the keyspace consisting of every n-th key in the provided
505
+ // keyspace.
506
+ func EveryN (ks Keyspace , n uint64 ) Keyspace {
507
+ innerCount := ks .Count ()
508
+ c := innerCount / n
509
+ // We are returning keys at index 0, n, 2n, ... up to cn (to be determined
510
+ // whether inclusive or exclusive), such that the last key is < innerCount.
511
+ // Since cn < innerCount, it becomes inclusive and we return c+1 keys (hence
512
+ // this increment).
513
+ if innerCount % n > 0 {
514
+ c ++
515
+ }
516
+ return & everyNKeyspace {ks : ks , n : n , c : c }
517
+ }
518
+
519
+ type everyNKeyspace struct {
520
+ ks Keyspace
521
+ n uint64
522
+ c uint64
523
+ }
524
+
525
+ // Count returns the number of keys that exist within this keyspace.
526
+ func (e everyNKeyspace ) Count () uint64 {
527
+ return e .c
528
+ }
529
+
530
+ // MaxLen returns the maximum length, in bytes, of a key within this
531
+ // keyspace. This is only guaranteed to return an upper bound.
532
+ func (e everyNKeyspace ) MaxLen () int {
533
+ return e .ks .MaxLen ()
534
+ }
535
+
536
+ // key writes the i-th key to the buffer and returns the length.
537
+ func (e everyNKeyspace ) key (buf []byte , i uint64 ) int {
538
+ return e .ks .key (buf , i * e .n )
539
+ }
0 commit comments