github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/event/targetidset.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package event
    19  
    20  // TargetIDSet - Set representation of TargetIDs.
    21  type TargetIDSet map[TargetID]struct{}
    22  
    23  // IsEmpty returns true if the set is empty.
    24  func (set TargetIDSet) IsEmpty() bool {
    25  	return len(set) != 0
    26  }
    27  
    28  // Clone - returns copy of this set.
    29  func (set TargetIDSet) Clone() TargetIDSet {
    30  	setCopy := NewTargetIDSet()
    31  	for k, v := range set {
    32  		setCopy[k] = v
    33  	}
    34  	return setCopy
    35  }
    36  
    37  // add - adds TargetID to the set.
    38  func (set TargetIDSet) add(targetID TargetID) {
    39  	set[targetID] = struct{}{}
    40  }
    41  
    42  // Union - returns union with given set as new set.
    43  func (set TargetIDSet) Union(sset TargetIDSet) TargetIDSet {
    44  	nset := set.Clone()
    45  
    46  	for k := range sset {
    47  		nset.add(k)
    48  	}
    49  
    50  	return nset
    51  }
    52  
    53  // Difference - returns difference with given set as new set.
    54  func (set TargetIDSet) Difference(sset TargetIDSet) TargetIDSet {
    55  	nset := NewTargetIDSet()
    56  	for k := range set {
    57  		if _, ok := sset[k]; !ok {
    58  			nset.add(k)
    59  		}
    60  	}
    61  
    62  	return nset
    63  }
    64  
    65  // NewTargetIDSet - creates new TargetID set with given TargetIDs.
    66  func NewTargetIDSet(targetIDs ...TargetID) TargetIDSet {
    67  	set := make(TargetIDSet)
    68  	for _, targetID := range targetIDs {
    69  		set.add(targetID)
    70  	}
    71  	return set
    72  }