From 98011e1e97ab9de5fba6e5a35b55300525f0eb84 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 27 Sep 2024 18:30:26 +0800 Subject: [PATCH] extend price volume slice methods --- pkg/types/price_volume_slice.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/types/price_volume_slice.go b/pkg/types/price_volume_slice.go index 4ffcda467..2f51320e0 100644 --- a/pkg/types/price_volume_slice.go +++ b/pkg/types/price_volume_slice.go @@ -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 {