github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/hash/hash_slice.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package hash
    23  
    24  type HashSlice []Hash
    25  
    26  func (rs HashSlice) Len() int {
    27  	return len(rs)
    28  }
    29  
    30  func (rs HashSlice) Less(i, j int) bool {
    31  	return rs[i].Less(rs[j])
    32  }
    33  
    34  func (rs HashSlice) Swap(i, j int) {
    35  	rs[i], rs[j] = rs[j], rs[i]
    36  }
    37  
    38  func (rs HashSlice) Equals(other HashSlice) bool {
    39  	if len(rs) != len(other) {
    40  		return false
    41  	}
    42  	for i := 0; i < len(rs); i++ {
    43  		if rs[i] != other[i] {
    44  			return false
    45  		}
    46  	}
    47  	return true
    48  }
    49  
    50  func (rs HashSlice) HashSet() HashSet {
    51  	hs := make(HashSet, len(rs))
    52  	for _, h := range rs {
    53  		hs[h] = struct{}{}
    54  	}
    55  
    56  	return hs
    57  }