2021-05-09 18:38:34 +00:00
|
|
|
// Copyright 2021 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-04-14 16:34:17 +00:00
|
|
|
package a
|
|
|
|
|
|
|
|
type Ordered interface {
|
2021-07-28 20:39:30 +00:00
|
|
|
~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
|
|
|
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
|
|
|
|
~float32 | ~float64 |
|
|
|
|
~string
|
2021-04-14 16:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List is a linked list of ordered values of type T.
|
|
|
|
type List[T Ordered] struct {
|
2021-07-28 20:39:30 +00:00
|
|
|
Next *List[T]
|
|
|
|
Val T
|
2021-04-14 16:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *List[T]) Largest() T {
|
2021-07-28 20:39:30 +00:00
|
|
|
var max T
|
|
|
|
for p := l; p != nil; p = p.Next {
|
|
|
|
if p.Val > max {
|
|
|
|
max = p.Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
2021-04-14 16:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type OrderedNum interface {
|
2021-07-28 20:39:30 +00:00
|
|
|
~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
|
|
|
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
|
|
|
|
~float32 | ~float64
|
2021-04-14 16:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListNum is a linked _List of ordered numeric values of type T.
|
|
|
|
type ListNum[T OrderedNum] struct {
|
2021-07-28 20:39:30 +00:00
|
|
|
Next *ListNum[T]
|
|
|
|
Val T
|
2021-04-14 16:34:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Clip = 5
|
|
|
|
|
|
|
|
// clippedLargest returns the largest in the list of OrderNums, but a max of 5.
|
|
|
|
func (l *ListNum[T]) ClippedLargest() T {
|
2021-07-28 20:39:30 +00:00
|
|
|
var max T
|
|
|
|
for p := l; p != nil; p = p.Next {
|
|
|
|
if p.Val > max && p.Val < Clip {
|
|
|
|
max = p.Val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max
|
2021-04-14 16:34:17 +00:00
|
|
|
}
|