New creates a new OrderedMap. options can either be one or several InitOption[K, V], or a single integer, which is then interpreted as a capacity hint, à la make(map[K]V, capacity).
(options ...any)
| 52 | // options can either be one or several InitOption[K, V], or a single integer, |
| 53 | // which is then interpreted as a capacity hint, à la make(map[K]V, capacity). |
| 54 | func New[K comparable, V any](options ...any) *OrderedMap[K, V] { //nolint:varnamelen |
| 55 | orderedMap := &OrderedMap[K, V]{} |
| 56 | |
| 57 | var config initConfig[K, V] |
| 58 | for _, untypedOption := range options { |
| 59 | switch option := untypedOption.(type) { |
| 60 | case int: |
| 61 | if len(options) != 1 { |
| 62 | invalidOption() |
| 63 | } |
| 64 | config.capacity = option |
| 65 | |
| 66 | case InitOption[K, V]: |
| 67 | option(&config) |
| 68 | |
| 69 | default: |
| 70 | invalidOption() |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | orderedMap.initialize(config.capacity) |
| 75 | orderedMap.AddPairs(config.initialData...) |
| 76 | |
| 77 | return orderedMap |
| 78 | } |
| 79 | |
| 80 | const invalidOptionMessage = `when using orderedmap.New[K,V]() with options, either provide one or several InitOption[K, V]; or a single integer which is then interpreted as a capacity hint, à la make(map[K]V, capacity).` //nolint:lll |
| 81 |