Created
May 22, 2019 07:26
-
-
Save ysqi/268d4fb866ef3e2366e8d3b58b143d6c to your computer and use it in GitHub Desktop.
fast remove item from slice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func clearExpiredTx(txs types.Transactions, now time.Time) types.Transactions { | |
for i := 0; i < len(txs); i++ { | |
if !txs[i].Expired(now) { | |
continue | |
} | |
if len(txs) == 1 { | |
return nil | |
} | |
var last *types.Transaction | |
for { | |
last = txs[len(txs)-1] | |
if !last.Expired(now) { | |
break | |
} | |
//remove last | |
txs[len(txs)-1] = nil | |
txs = txs[:len(txs)-1] | |
last = nil | |
if len(txs) == 0 { | |
break | |
} | |
} | |
if last == nil || len(txs)-1 < i { | |
return txs | |
} | |
txs[i] = last | |
txs[len(txs)-1] = nil | |
txs = txs[:len(txs)-1] | |
} | |
return txs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment