github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/pkg/set/string.go (about)

     1  // Copyright 2015 The rkt Authors
     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  // For how the uidshift and uidcount are generated please check:
    16  // http://cgit.freedesktop.org/systemd/systemd/commit/?id=03cfe0d51499e86b1573d1
    17  
    18  package set
    19  
    20  type String map[string]struct{}
    21  
    22  func NewString(items ...string) String {
    23  	s := String{}
    24  	s.Insert(items...)
    25  	return s
    26  }
    27  
    28  // Insert adds items to the set.
    29  func (s String) Insert(items ...string) {
    30  	for _, item := range items {
    31  		s[item] = struct{}{}
    32  	}
    33  }
    34  
    35  // Has returns true if and only if item is contained in the set.
    36  func (s String) Has(item string) bool {
    37  	_, contained := s[item]
    38  	return contained
    39  }
    40  
    41  // HasAll returns true if and only if all items are contained in the set.
    42  func (s String) HasAll(items ...string) bool {
    43  	for _, item := range items {
    44  		if !s.Has(item) {
    45  			return false
    46  		}
    47  	}
    48  	return true
    49  }
    50  
    51  // Delete removes all items from the set.
    52  func (s String) Delete(items ...string) {
    53  	for _, item := range items {
    54  		delete(s, item)
    55  	}
    56  }
    57  
    58  // ConditionalHas returns true if and only if there is any item 'source'
    59  // in the set that satisfies the conditionFunc wrt 'item'.
    60  func (s String) ConditionalHas(conditionFunc func(source, item string) bool, item string) bool {
    61  	for source := range s {
    62  		if conditionFunc(source, item) {
    63  			return true
    64  		}
    65  	}
    66  	return false
    67  }