This repository was archived by the owner on Dec 22, 2021. It is now read-only.
  
  
  - 
                Notifications
    You must be signed in to change notification settings 
- Fork 70
          Bunch of bug fixes for ArrayBuffer & ListBuffer
          #434
        
      
          
     Merged
      
      
    
                
     Merged
            
            
          Conversation
  
    
      This file contains hidden or 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
    
  
  
    
    
              
                    marcelocenerine
  
              
              commented
              
                  
                    Feb 8, 2018 
                  
              
              
            
            
|  | ||
| private var array: Array[AnyRef] = initElems | ||
| private var end = initLength | ||
| private var size0 = initSize | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed end to size0 as the former is misleading
9aef2d4    to
    0be77fb      
    Compare
  
    
              
                    julienrf
  
              
              reviewed
              
                  
                    Feb 8, 2018 
                  
              
              
            
            
| def apply(n: Int) = array(n).asInstanceOf[A] | ||
| def apply(n: Int) = { | ||
| checkWithinBounds(n, n + 1) | ||
| array(n).asInstanceOf[A] | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aren’t bounds already checked by the underlying array access?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the underlying array can be larger than the ArrayBuffer in many situations:
import strawman.collection.mutable.ArrayBuffer
val a = ArrayBuffer(1)
a: strawman.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)
scala> a.remove(0)
res2: Int = 1
scala> a(0)
res3: Int = 0
scala> val b = ArrayBuffer.empty[Int]
b: strawman.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> b(0)
res4: Int = 0
scala> val c = ArrayBuffer(1, 2, 3)
c: strawman.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> c.clear()
scala> c.size
res6: Int = 0
scala> c(0)
res7: Int = 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in summary:
- when an empty ArrayBufferis created, its underlying array will have size == 16
- when an ArrayBuffer's element is removed, the underlying array remains with the same size (only the buffer's size gets decremented)
0be77fb    to
    0c6c8be      
    Compare
  
    
              
                    julienrf
  
              
              approved these changes
              
                  
                    Feb 9, 2018 
                  
              
              
            
            
| Thanks Marcelo :) | 
  
      Sign up for free
      to subscribe to this conversation on GitHub.
      Already have an account?
      Sign in.
  
      
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Buffer.patchthrows out of bound index exception) by dropping validation inView.PatchedListBuffer.patchInPlacethrows class cast exception) by clipping index/count to safe valuesArrayBuffer.patchInPlacecausing out of bound index exceptionsArrayBuffer.clearnot dereferencing cleared elements)ArrayBuffer'sapplyandupdateis within bounds. This fix uncovered other issues inaddOneandinsertNote: each fix is in a separate commit