You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 line
1.2 KiB

  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. package colltab
  5. // testWeighter is a simple Weighter that returns weights from a user-defined map.
  6. type testWeighter map[string][]Elem
  7. func (t testWeighter) Start(int, []byte) int { return 0 }
  8. func (t testWeighter) StartString(int, string) int { return 0 }
  9. func (t testWeighter) Domain() []string { return nil }
  10. func (t testWeighter) Top() uint32 { return 0 }
  11. // maxContractBytes is the maximum length of any key in the map.
  12. const maxContractBytes = 10
  13. func (t testWeighter) AppendNext(buf []Elem, s []byte) ([]Elem, int) {
  14. n := len(s)
  15. if n > maxContractBytes {
  16. n = maxContractBytes
  17. }
  18. for i := n; i > 0; i-- {
  19. if e, ok := t[string(s[:i])]; ok {
  20. return append(buf, e...), i
  21. }
  22. }
  23. panic("incomplete testWeighter: could not find " + string(s))
  24. }
  25. func (t testWeighter) AppendNextString(buf []Elem, s string) ([]Elem, int) {
  26. n := len(s)
  27. if n > maxContractBytes {
  28. n = maxContractBytes
  29. }
  30. for i := n; i > 0; i-- {
  31. if e, ok := t[s[:i]]; ok {
  32. return append(buf, e...), i
  33. }
  34. }
  35. panic("incomplete testWeighter: could not find " + s)
  36. }