| 1 | //Copyright 2013 Vastech SA (PTY) LTD |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package first |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "sort" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | /* |
| 24 | key: symbol string |
| 25 | */ |
| 26 | type SymbolSet map[string]bool |
| 27 | |
| 28 | func (this SymbolSet) AddSet(that SymbolSet) { |
| 29 | for id := range that { |
| 30 | this[id] = true |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func (this SymbolSet) Equal(that SymbolSet) bool { |
| 35 | if len(this) != len(that) { |
| 36 | return false |
| 37 | } |
| 38 | for symbol := range this { |
| 39 | if _, contain := that[symbol]; !contain { |
| 40 | return false |
| 41 | } |
| 42 | } |
| 43 | return true |
| 44 | } |
| 45 | |
| 46 | func (this SymbolSet) String() string { |
| 47 | buf := new(strings.Builder) |
| 48 | fmt.Fprintf(buf, "{\n") |
| 49 | keys := make([]string, 0, len(this)) |
| 50 | for key := range this { |
| 51 | keys = append(keys, key) |
| 52 | } |
| 53 | sort.Strings(keys) |
| 54 | for _, str := range keys { |
| 55 | fmt.Fprintf(buf, "\t%s\n", str) |
| 56 | } |
| 57 | fmt.Fprintf(buf, "}") |
| 58 | return buf.String() |
| 59 | } |