Serge Bazanski | cc25bdf | 2018-10-25 14:02:58 +0200 | [diff] [blame^] | 1 | // +build !go1.9 |
| 2 | |
| 3 | package swag |
| 4 | |
| 5 | import ( |
| 6 | "sort" |
| 7 | "sync" |
| 8 | ) |
| 9 | |
| 10 | // indexOfInitialisms is a thread-safe implementation of the sorted index of initialisms. |
| 11 | // Before go1.9, this may be implemented with a mutex on the map. |
| 12 | type indexOfInitialisms struct { |
| 13 | getMutex *sync.Mutex |
| 14 | index map[string]bool |
| 15 | } |
| 16 | |
| 17 | func newIndexOfInitialisms() *indexOfInitialisms { |
| 18 | return &indexOfInitialisms{ |
| 19 | getMutex: new(sync.Mutex), |
| 20 | index: make(map[string]bool, 50), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (m *indexOfInitialisms) load(initial map[string]bool) *indexOfInitialisms { |
| 25 | m.getMutex.Lock() |
| 26 | defer m.getMutex.Unlock() |
| 27 | for k, v := range initial { |
| 28 | m.index[k] = v |
| 29 | } |
| 30 | return m |
| 31 | } |
| 32 | |
| 33 | func (m *indexOfInitialisms) isInitialism(key string) bool { |
| 34 | m.getMutex.Lock() |
| 35 | defer m.getMutex.Unlock() |
| 36 | _, ok := m.index[key] |
| 37 | return ok |
| 38 | } |
| 39 | |
| 40 | func (m *indexOfInitialisms) add(key string) *indexOfInitialisms { |
| 41 | m.getMutex.Lock() |
| 42 | defer m.getMutex.Unlock() |
| 43 | m.index[key] = true |
| 44 | return m |
| 45 | } |
| 46 | |
| 47 | func (m *indexOfInitialisms) sorted() (result []string) { |
| 48 | m.getMutex.Lock() |
| 49 | defer m.getMutex.Unlock() |
| 50 | for k := range m.index { |
| 51 | result = append(result, k) |
| 52 | } |
| 53 | sort.Sort(sort.Reverse(byLength(result))) |
| 54 | return |
| 55 | } |