github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/memorystorage/memorytable/tuple.go (about)

     1  // Copyright 2022 Matrix Origin
     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  package memorytable
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  )
    22  
    23  // Tuple represents multiple ordered values
    24  type Tuple []any
    25  
    26  var _ Ordered[Tuple] = Tuple{}
    27  
    28  // Less compares two tuples
    29  func (t Tuple) Less(than Tuple) bool {
    30  	i := 0
    31  	for i < len(t) {
    32  		a := t[i]
    33  		if i >= len(than) {
    34  			return false
    35  		}
    36  		b := than[i]
    37  
    38  		// min, max
    39  		if a == Min {
    40  			return b != Min
    41  		}
    42  		if a == Max {
    43  			return false
    44  		}
    45  		if b == Min {
    46  			return false
    47  		}
    48  		if b == Max {
    49  			return a != Max
    50  		}
    51  
    52  		switch a := a.(type) {
    53  
    54  		case Text:
    55  			b := b.(Text)
    56  			if a.Less(b) {
    57  				return true
    58  			} else if b.Less(a) {
    59  				return false
    60  			}
    61  
    62  		case Bool:
    63  			b := b.(Bool)
    64  			if a.Less(b) {
    65  				return true
    66  			} else if b.Less(a) {
    67  				return false
    68  			}
    69  
    70  		case Int:
    71  			b := b.(Int)
    72  			if a.Less(b) {
    73  				return true
    74  			} else if b.Less(a) {
    75  				return false
    76  			}
    77  
    78  		case Uint:
    79  			b := b.(Uint)
    80  			if a.Less(b) {
    81  				return true
    82  			} else if b.Less(a) {
    83  				return false
    84  			}
    85  
    86  		case Float:
    87  			b := b.(Float)
    88  			if a.Less(b) {
    89  				return true
    90  			} else if b.Less(a) {
    91  				return false
    92  			}
    93  
    94  		case Bytes:
    95  			b := b.(Bytes)
    96  			if a.Less(b) {
    97  				return true
    98  			} else if b.Less(a) {
    99  				return false
   100  			}
   101  
   102  		case ID:
   103  			b := b.(ID)
   104  			if a.Less(b) {
   105  				return true
   106  			} else if b.Less(a) {
   107  				return false
   108  			}
   109  
   110  		case types.TS:
   111  			b := b.(types.TS)
   112  			if a.Less(&b) {
   113  				return true
   114  			} else if b.Less(&a) {
   115  				return false
   116  			}
   117  
   118  		case Decimal:
   119  			b := b.(Decimal)
   120  			if a.Less(b) {
   121  				return true
   122  			} else if b.Less(a) {
   123  				return false
   124  			}
   125  
   126  		default:
   127  			panic(fmt.Sprintf("unknown item type: %T %#v", a, a))
   128  		}
   129  
   130  		i++
   131  	}
   132  
   133  	return i != len(than)
   134  }