order.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package namer
  2. import (
  3. "sort"
  4. "go-common/app/tool/gengo/types"
  5. )
  6. // Orderer produces an ordering of types given a Namer.
  7. type Orderer struct {
  8. Namer
  9. }
  10. // OrderUniverse assigns a name to every type in the Universe, including Types,
  11. // Functions and Variables, and returns a list sorted by those names.
  12. func (o *Orderer) OrderUniverse(u types.Universe) []*types.Type {
  13. list := tList{
  14. namer: o.Namer,
  15. }
  16. for _, p := range u {
  17. for _, t := range p.Types {
  18. list.types = append(list.types, t)
  19. }
  20. for _, f := range p.Functions {
  21. list.types = append(list.types, f)
  22. }
  23. for _, v := range p.Variables {
  24. list.types = append(list.types, v)
  25. }
  26. }
  27. sort.Sort(list)
  28. return list.types
  29. }
  30. // OrderTypes assigns a name to every type, and returns a list sorted by those
  31. // names.
  32. func (o *Orderer) OrderTypes(typeList []*types.Type) []*types.Type {
  33. list := tList{
  34. namer: o.Namer,
  35. types: typeList,
  36. }
  37. sort.Sort(list)
  38. return list.types
  39. }
  40. type tList struct {
  41. namer Namer
  42. types []*types.Type
  43. }
  44. func (t tList) Len() int { return len(t.types) }
  45. func (t tList) Less(i, j int) bool { return t.namer.Name(t.types[i]) < t.namer.Name(t.types[j]) }
  46. func (t tList) Swap(i, j int) { t.types[i], t.types[j] = t.types[j], t.types[i] }