github.com/cloudwego/frugal@v0.1.15/internal/atm/ssa/slotset.go (about) 1 /* 2 * Copyright 2022 ByteDance Inc. 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 ssa 18 19 import ( 20 `fmt` 21 `sort` 22 `strings` 23 ) 24 25 type ( 26 SlotSet map[IrSpillSlot]struct{} 27 ) 28 29 func (self SlotSet) add(r IrSpillSlot) bool { 30 if _, ok := self[r]; ok { 31 return false 32 } else { 33 self[r] = struct{}{} 34 return true 35 } 36 } 37 38 func (self SlotSet) clone() (rs SlotSet) { 39 rs = make(SlotSet, len(self)) 40 for r := range self { rs.add(r) } 41 return 42 } 43 44 func (self SlotSet) remove(r IrSpillSlot) bool { 45 if _, ok := self[r]; !ok { 46 return false 47 } else { 48 delete(self, r) 49 return true 50 } 51 } 52 53 func (self SlotSet) String() string { 54 nb := len(self) 55 rs := make([]string, 0, nb) 56 rr := make([]IrSpillSlot, 0, nb) 57 58 /* extract all slot */ 59 for r := range self { 60 rr = append(rr, r) 61 } 62 63 /* sort by slot ID */ 64 sort.Slice(rr, func(i int, j int) bool { 65 return rr[i] < rr[j] 66 }) 67 68 /* convert every slot */ 69 for _, r := range rr { 70 rs = append(rs, r.String()) 71 } 72 73 /* join them together */ 74 return fmt.Sprintf( 75 "{%s}", 76 strings.Join(rs, ", "), 77 ) 78 }