Skip to content

ForwardIterator.Prev() causes panic when it is at first element #1

@duncan-to-18m

Description

@duncan-to-18m
var unconfirmedMap   *treemap.TreeMap[decimal.Decimal, *wm_model.Block]  = treemap.NewWithKeyCompare[ decimal.Decimal, *wm_model.Block ] (BlockNumberCmp)
		
func BlockNumberCmp(left decimal.Decimal, right decimal.Decimal) bool {
	return left.Cmp(right) < 0
}

func DoSomething(latestFastConfirmNum decimal.Decimal)  []*wm_model.Block{
               var movedBlockList []*wm_model.Block = make([]*wm_model.Block, 0)
                it :=  unconfirmedMap.UpperBound(latestFastConfirmNum)
		if it.Valid() {
			for it.Prev(); it.Valid(); it.Prev() {
				movedBlockList = append(movedBlockList, it.Value())
			}

			for _, item := range movedBlockList {
				unconfirmedMap.Del(item.BlockNumber)
			}
		}
                return movedBlockList
   
}

Panic at it.Prev()
Besides, I would suggest Valid() function would also check if it is before the first element
If the forward iterator just expect to work forward only, I would suggest a function to convert a forward iterator to a reverse iterator

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions