Skip to content

Commit b0491ef

Browse files
committed
modify DeepCopy
1 parent d2c3d3f commit b0491ef

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

queue/linked_blocking_queue.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ func (q *LinkedBlockingQueue) Remove(i interface{}) bool {
340340
return false
341341
}
342342

343+
// lower performance
343344
func (q *LinkedBlockingQueue) ContainsAll(c Collection) bool {
344345
containsAll := true
345346
c.Range(func(value interface{}) bool {
@@ -459,8 +460,15 @@ func FromSlice(s []interface{}, capacity int) (*LinkedBlockingQueue, error) {
459460
return q, nil
460461
}
461462

462-
func DeepCopy(q *LinkedBlockingQueue) *LinkedBlockingQueue {
463-
copied, _ := FromSlice(q.ToSlice(), q.capacity)
463+
func (q *LinkedBlockingQueue) DeepCopy() *LinkedBlockingQueue {
464+
copied := NewLinkedBlockingQueue(q.capacity)
465+
var n int64
466+
q.Range(func(value interface{}) bool {
467+
copied.head.PushBack(value)
468+
n++
469+
return true
470+
})
471+
atomic.StoreInt64(&copied.length, n)
464472
return copied
465473
}
466474

0 commit comments

Comments
 (0)