# HG changeset patch # User Claus Gittinger # Date 1558432198 -7200 # Node ID 4029e964d0f1a228352502c3deb70fdc36c79f52 # Parent ce2a0e462ff03c95c0d9e432c16f36912fd87769 #FEATURE by cg class: OrderedCollection added: #removeLastIfAbsent: diff -r ce2a0e462ff0 -r 4029e964d0f1 OrderedCollection.st --- a/OrderedCollection.st Sat May 18 10:14:18 2019 +0200 +++ b/OrderedCollection.st Tue May 21 11:49:58 2019 +0200 @@ -1337,6 +1337,39 @@ "Modified: 12.4.1996 / 13:39:12 / cg" ! +removeLastIfAbsent:exceptionValue + "remove the last element from the collection. + Return the removed element or the value from exceptionValue if empty. + Destructive: modifies the receiver" + + |anObject + idx "{ Class: SmallInteger }" | + + idx := lastIndex. + firstIndex > idx ifTrue:[ + "collection is empty" + ^ exceptionValue value. + ]. + anObject := contentsArray basicAt:idx. + + "/ nil it (helps GC) + contentsArray basicAt:idx put:nil. + lastIndex := idx := idx - 1. + + firstIndex > idx ifTrue:[ + "reset to avoid ever growing" + firstIndex := 1. + lastIndex := 0 + ]. + ^ anObject + + " + OrderedCollection new removeLastIfAbsent:nil + " + + "Created: / 21-05-2019 / 08:47:55 / Claus Gittinger" +! + reset "logically remove all elements from the collection. That's almost the same as #removeAll, but keeps the contentsArray.