matchalgorithm.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2014-2022 Ulrich Kunitz. 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 lzma
  5. import "errors"
  6. // MatchAlgorithm identifies an algorithm to find matches in the
  7. // dictionary.
  8. type MatchAlgorithm byte
  9. // Supported matcher algorithms.
  10. const (
  11. HashTable4 MatchAlgorithm = iota
  12. BinaryTree
  13. )
  14. // maStrings are used by the String method.
  15. var maStrings = map[MatchAlgorithm]string{
  16. HashTable4: "HashTable4",
  17. BinaryTree: "BinaryTree",
  18. }
  19. // String returns a string representation of the Matcher.
  20. func (a MatchAlgorithm) String() string {
  21. if s, ok := maStrings[a]; ok {
  22. return s
  23. }
  24. return "unknown"
  25. }
  26. var errUnsupportedMatchAlgorithm = errors.New(
  27. "lzma: unsupported match algorithm value")
  28. // verify checks whether the matcher value is supported.
  29. func (a MatchAlgorithm) verify() error {
  30. if _, ok := maStrings[a]; !ok {
  31. return errUnsupportedMatchAlgorithm
  32. }
  33. return nil
  34. }
  35. func (a MatchAlgorithm) new(dictCap int) (m matcher, err error) {
  36. switch a {
  37. case HashTable4:
  38. return newHashTable(dictCap, 4)
  39. case BinaryTree:
  40. return newBinTree(dictCap)
  41. }
  42. return nil, errUnsupportedMatchAlgorithm
  43. }