storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/event/targetidset.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2018 MinIO, 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 event
    18  
    19  // TargetIDSet - Set representation of TargetIDs.
    20  type TargetIDSet map[TargetID]struct{}
    21  
    22  // IsEmpty returns true if the set is empty.
    23  func (set TargetIDSet) IsEmpty() bool {
    24  	return len(set) != 0
    25  }
    26  
    27  // Clone - returns copy of this set.
    28  func (set TargetIDSet) Clone() TargetIDSet {
    29  	setCopy := NewTargetIDSet()
    30  	for k, v := range set {
    31  		setCopy[k] = v
    32  	}
    33  	return setCopy
    34  }
    35  
    36  // add - adds TargetID to the set.
    37  func (set TargetIDSet) add(targetID TargetID) {
    38  	set[targetID] = struct{}{}
    39  }
    40  
    41  // Union - returns union with given set as new set.
    42  func (set TargetIDSet) Union(sset TargetIDSet) TargetIDSet {
    43  	nset := set.Clone()
    44  
    45  	for k := range sset {
    46  		nset.add(k)
    47  	}
    48  
    49  	return nset
    50  }
    51  
    52  // Difference - returns diffrence with given set as new set.
    53  func (set TargetIDSet) Difference(sset TargetIDSet) TargetIDSet {
    54  	nset := NewTargetIDSet()
    55  	for k := range set {
    56  		if _, ok := sset[k]; !ok {
    57  			nset.add(k)
    58  		}
    59  	}
    60  
    61  	return nset
    62  }
    63  
    64  // NewTargetIDSet - creates new TargetID set with given TargetIDs.
    65  func NewTargetIDSet(targetIDs ...TargetID) TargetIDSet {
    66  	set := make(TargetIDSet)
    67  	for _, targetID := range targetIDs {
    68  		set.add(targetID)
    69  	}
    70  	return set
    71  }