Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

transducer.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) 2017 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package vellum
  15. // Transducer represents the general contract of a byte-based finite transducer
  16. type Transducer interface {
  17. // all transducers are also automatons
  18. Automaton
  19. // IsMatchWithValue returns true if and only if the state is a match
  20. // additionally it returns a states final value (if any)
  21. IsMatchWithVal(int) (bool, uint64)
  22. // Accept returns the next state given the input to the specified state
  23. // additionally it returns the value associated with the transition
  24. AcceptWithVal(int, byte) (int, uint64)
  25. }
  26. // TransducerGet implements an generic Get() method which works
  27. // on any implementation of Transducer
  28. // The caller MUST check the boolean return value for a match.
  29. // Zero is a valid value regardless of match status,
  30. // and if it is NOT a match, the value collected so far is returned.
  31. func TransducerGet(t Transducer, k []byte) (bool, uint64) {
  32. var total uint64
  33. i := 0
  34. curr := t.Start()
  35. for t.CanMatch(curr) && i < len(k) {
  36. var transVal uint64
  37. curr, transVal = t.AcceptWithVal(curr, k[i])
  38. if curr == noneAddr {
  39. break
  40. }
  41. total += transVal
  42. i++
  43. }
  44. if i != len(k) {
  45. return false, total
  46. }
  47. match, finalVal := t.IsMatchWithVal(curr)
  48. return match, total + finalVal
  49. }