| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package colltab |
| 6 |
|
| 7 |
// testWeighter is a simple Weighter that returns weights from a user-defined map. |
| 8 |
type testWeighter map[string][]Elem |
| 9 |
|
| 10 |
func (t testWeighter) Start(int, []byte) int { return 0 } |
| 11 |
func (t testWeighter) StartString(int, string) int { return 0 } |
| 12 |
func (t testWeighter) Domain() []string { return nil } |
| 13 |
func (t testWeighter) Top() uint32 { return 0 } |
| 14 |
|
| 15 |
// maxContractBytes is the maximum length of any key in the map. |
| 16 |
const maxContractBytes = 10 |
| 17 |
|
| 18 |
func (t testWeighter) AppendNext(buf []Elem, s []byte) ([]Elem, int) { |
| 19 |
n := len(s) |
| 20 |
if n > maxContractBytes { |
| 21 |
n = maxContractBytes |
| 22 |
} |
| 23 |
for i := n; i > 0; i-- { |
| 24 |
if e, ok := t[string(s[:i])]; ok { |
| 25 |
return append(buf, e...), i |
| 26 |
} |
| 27 |
} |
| 28 |
panic("incomplete testWeighter: could not find " + string(s)) |
| 29 |
} |
| 30 |
|
| 31 |
func (t testWeighter) AppendNextString(buf []Elem, s string) ([]Elem, int) { |
| 32 |
n := len(s) |
| 33 |
if n > maxContractBytes { |
| 34 |
n = maxContractBytes |
| 35 |
} |
| 36 |
for i := n; i > 0; i-- { |
| 37 |
if e, ok := t[s[:i]]; ok { |
| 38 |
return append(buf, e...), i |
| 39 |
} |
| 40 |
} |
| 41 |
panic("incomplete testWeighter: could not find " + s) |
| 42 |
} |