extend price volume slice methods

This commit is contained in:
c9s 2024-09-27 18:30:26 +08:00
parent 79b636fa02
commit 98011e1e97
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -79,6 +79,19 @@ func (slice PriceVolumeSlice) First() (PriceVolume, bool) {
return PriceVolume{}, false
}
// ElemOrLast returns the element on the index i, if i is out of range, it will return the last element
func (slice PriceVolumeSlice) ElemOrLast(i int) (PriceVolume, bool) {
if len(slice) == 0 {
return PriceVolume{}, false
}
if i > len(slice)-1 {
return slice[len(slice)-1], true
}
return slice[i], true
}
func (slice PriceVolumeSlice) IndexByQuoteVolumeDepth(requiredQuoteVolume fixedpoint.Value) int {
var totalQuoteVolume = fixedpoint.Zero
for x, pv := range slice {