SInter returns the intersection of multiple sets. The parameter 'keys' is a variadic parameter, meaning it can accept any number of arguments. These arguments are the keys of the sets that are to be intersected.
(keys ...string)
| 155 | // it can accept any number of arguments. These arguments are the keys |
| 156 | // of the sets that are to be intersected. |
| 157 | func (s *SetStructure) SInter(keys ...string) ([]string, error) { |
| 158 | // if there are no keys, then there's no intersection |
| 159 | if len(keys) == 0 { |
| 160 | return nil, nil |
| 161 | } |
| 162 | // All elements of 'first' are stored in 'mem' map because it's checked |
| 163 | // against each of the subsequent sets. |
| 164 | first, err := s.checkAndGetSet(keys[0], false) |
| 165 | if err != nil { |
| 166 | return nil, err |
| 167 | } |
| 168 | |
| 169 | mem := make(map[string]struct{}) |
| 170 | for k := range *first { |
| 171 | mem[k] = struct{}{} |
| 172 | } |
| 173 | |
| 174 | var members []string |
| 175 | // For each other key, we get its set and its members. |
| 176 | for _, key := range keys[1:] { |
| 177 | fs, err := s.checkAndGetSet(key, false) |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | // We check if each member of the current set is in 'mem'. |
| 182 | // If yes, we add it to 'members' array, and remove it from 'mem' map. |
| 183 | for k := range *fs { |
| 184 | if _, ok := mem[k]; ok { |
| 185 | delete(mem, k) |
| 186 | members = append(members, k) |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return members, nil |
| 192 | } |
| 193 | |
| 194 | // SDiff computes the difference between the first and subsequent sets. |
| 195 | // It returns keys unique to the first set and an error if applicable. |
no test coverage detected