|
| 1 | +/* |
| 2 | +Copyright 2016 Google Inc. All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ast |
| 18 | + |
| 19 | +import ( |
| 20 | + "sort" |
| 21 | +) |
| 22 | + |
| 23 | +// Identifier represents a variable / parameter / field name. |
| 24 | +type Identifier string |
| 25 | + |
| 26 | +// Identifiers represents an Identifier slice. |
| 27 | +type Identifiers []Identifier |
| 28 | + |
| 29 | +// IdentifierSet represents an Identifier set. |
| 30 | +type IdentifierSet map[Identifier]struct{} |
| 31 | + |
| 32 | +// NewIdentifierSet creates a new IdentifierSet. |
| 33 | +func NewIdentifierSet(idents ...Identifier) IdentifierSet { |
| 34 | + set := make(IdentifierSet) |
| 35 | + for _, ident := range idents { |
| 36 | + set[ident] = struct{}{} |
| 37 | + } |
| 38 | + return set |
| 39 | +} |
| 40 | + |
| 41 | +// Add adds an Identifier to the set. |
| 42 | +func (set IdentifierSet) Add(ident Identifier) bool { |
| 43 | + if _, ok := set[ident]; ok { |
| 44 | + return false |
| 45 | + } |
| 46 | + set[ident] = struct{}{} |
| 47 | + return true |
| 48 | +} |
| 49 | + |
| 50 | +// AddIdentifiers adds a slice of identifiers to the set. |
| 51 | +func (set IdentifierSet) AddIdentifiers(idents Identifiers) { |
| 52 | + for _, ident := range idents { |
| 53 | + set.Add(ident) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Contains returns true if an Identifier is in the set. |
| 58 | +func (set IdentifierSet) Contains(ident Identifier) bool { |
| 59 | + _, ok := set[ident] |
| 60 | + return ok |
| 61 | +} |
| 62 | + |
| 63 | +// Remove removes an Identifier from the set. |
| 64 | +func (set IdentifierSet) Remove(ident Identifier) { |
| 65 | + delete(set, ident) |
| 66 | +} |
| 67 | + |
| 68 | +// ToSlice returns an Identifiers slice from the set. |
| 69 | +func (set IdentifierSet) ToSlice() Identifiers { |
| 70 | + idents := make(Identifiers, len(set)) |
| 71 | + i := 0 |
| 72 | + for ident := range set { |
| 73 | + idents[i] = ident |
| 74 | + i++ |
| 75 | + } |
| 76 | + return idents |
| 77 | +} |
| 78 | + |
| 79 | +// ToOrderedSlice returns the elements of the current set as an ordered slice. |
| 80 | +func (set IdentifierSet) ToOrderedSlice() []Identifier { |
| 81 | + idents := set.ToSlice() |
| 82 | + sort.Sort(identifierSorter(idents)) |
| 83 | + return idents |
| 84 | +} |
| 85 | + |
| 86 | +type identifierSorter []Identifier |
| 87 | + |
| 88 | +func (s identifierSorter) Len() int { return len(s) } |
| 89 | +func (s identifierSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 90 | +func (s identifierSorter) Less(i, j int) bool { return s[i] < s[j] } |
| 91 | + |
| 92 | +// Clone returns a clone of the set. |
| 93 | +func (set IdentifierSet) Clone() IdentifierSet { |
| 94 | + newSet := make(IdentifierSet, len(set)) |
| 95 | + for k, v := range set { |
| 96 | + newSet[k] = v |
| 97 | + } |
| 98 | + return newSet |
| 99 | +} |
0 commit comments