Source file test/typeparam/listimp.dir/a.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package a
     6  
     7  type Ordered interface {
     8  	~int | ~int8 | ~int16 | ~int32 | ~int64 |
     9  		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
    10  		~float32 | ~float64 |
    11  		~string
    12  }
    13  
    14  // List is a linked list of ordered values of type T.
    15  type List[T Ordered] struct {
    16  	Next *List[T]
    17  	Val  T
    18  }
    19  
    20  func (l *List[T]) Largest() T {
    21  	var max T
    22  	for p := l; p != nil; p = p.Next {
    23  		if p.Val > max {
    24  			max = p.Val
    25  		}
    26  	}
    27  	return max
    28  }
    29  
    30  type OrderedNum interface {
    31  	~int | ~int8 | ~int16 | ~int32 | ~int64 |
    32  		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
    33  		~float32 | ~float64
    34  }
    35  
    36  // ListNum is a linked _List of ordered numeric values of type T.
    37  type ListNum[T OrderedNum] struct {
    38  	Next *ListNum[T]
    39  	Val  T
    40  }
    41  
    42  const Clip = 5
    43  
    44  // ClippedLargest returns the largest in the list of OrderNums, but a max of 5.
    45  func (l *ListNum[T]) ClippedLargest() T {
    46  	var max T
    47  	for p := l; p != nil; p = p.Next {
    48  		if p.Val > max && p.Val < Clip {
    49  			max = p.Val
    50  		}
    51  	}
    52  	return max
    53  }
    54  

View as plain text