Bug 137374

Summary: Support insert iterators for Vectors. [Part 1]
Product: WebKit Reporter: Jer Noble <jer.noble>
Component: New BugsAssignee: Jer Noble <jer.noble>
Status: NEW ---    
Severity: Normal CC: andersca, ap, benjamin, bfulgham, cmarcelo, commit-queue, darin, dbates, gyuyoung.kim, rakuco, ryuan.choi, sam, sergio
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on:    
Bug Blocks: 137419    
Attachments:
Description Flags
Patch
none
Patch
none
Patch
none
Patch
none
Patch none

Description Jer Noble 2014-10-02 18:08:19 PDT
Support insert iterators for Vectors.
Comment 1 Jer Noble 2014-10-02 21:20:39 PDT
Created attachment 239176 [details]
Patch
Comment 2 WebKit Commit Bot 2014-10-02 21:22:40 PDT
Attachment 239176 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/VectorIterators.h:40:  difference_type is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:43:  iterator_category is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:74:  difference_type is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:77:  iterator_category is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
Total errors found: 4 in 3 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Daniel Bates 2014-10-03 10:46:20 PDT
Comment on attachment 239176 [details]
Patch

We should add unit tests for this functionality. You can see examples of unit tests in Tools/TestWebKitAPI/Tests. In particular, we have WTF-specific unit tests in Tools/TestWebKitAPI/Tests/WTF.
Comment 4 Jer Noble 2014-10-03 10:51:35 PDT
Created attachment 239213 [details]
Patch
Comment 5 Jer Noble 2014-10-03 10:52:07 PDT
(In reply to comment #3)
> (From update of attachment 239176 [details])
> We should add unit tests for this functionality. You can see examples of unit tests in Tools/TestWebKitAPI/Tests. In particular, we have WTF-specific unit tests in Tools/TestWebKitAPI/Tests/WTF.

Oh I wrote them; but somehow managed to upload a patch without them. :-/
Comment 6 Daniel Bates 2014-10-03 10:52:28 PDT
We need to add file VectorIterators.h to the Windows and CMake build systems.
Comment 7 WebKit Commit Bot 2014-10-03 10:52:42 PDT
Attachment 239213 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/VectorIterators.h:40:  difference_type is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:43:  iterator_category is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:74:  difference_type is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:77:  iterator_category is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
Total errors found: 4 in 5 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 8 Jer Noble 2014-10-03 10:59:02 PDT
Created attachment 239214 [details]
Patch
Comment 9 Anders Carlsson 2014-10-03 10:59:48 PDT
Why can't we use std::back_insert_iterator? 

http://en.cppreference.com/w/cpp/iterator/back_insert_iterator
Comment 10 WebKit Commit Bot 2014-10-03 11:01:31 PDT
Attachment 239214 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/VectorIterators.h:40:  difference_type is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:43:  iterator_category is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:74:  difference_type is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:77:  iterator_category is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
Total errors found: 4 in 5 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 11 Jer Noble 2014-10-03 11:01:52 PDT
Created attachment 239215 [details]
Patch
Comment 12 WebKit Commit Bot 2014-10-03 11:04:23 PDT
Attachment 239215 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/VectorIterators.h:40:  difference_type is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:43:  iterator_category is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:74:  difference_type is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
ERROR: Source/WTF/wtf/VectorIterators.h:77:  iterator_category is incorrectly named. Don't use underscores in your identifier names.  [readability/naming/underscores] [4]
Total errors found: 4 in 7 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 13 Jer Noble 2014-10-03 11:04:57 PDT
(In reply to comment #9)
> Why can't we use std::back_insert_iterator? 
> 
> http://en.cppreference.com/w/cpp/iterator/back_insert_iterator

Because Vector doesn't have a push_back().  Its iterator types also don't have the std standard typedefs, like value_type, etc.
Comment 14 Anders Carlsson 2014-10-03 11:35:52 PDT
(In reply to comment #13)
> (In reply to comment #9)
> > Why can't we use std::back_insert_iterator? 
> > 
> > http://en.cppreference.com/w/cpp/iterator/back_insert_iterator
> 
> Because Vector doesn't have a push_back().  Its iterator types also don't have the std standard typedefs, like value_type, etc.

I think it's much better to make our containers play well with the STL than it is to replicate STL functionality. (Our transition from OwnPtr to std::unique_ptr is an example of this).
Comment 15 Darin Adler 2014-10-03 12:41:09 PDT
(In reply to comment #14)
> (In reply to comment #13)
> > (In reply to comment #9)
> > > Why can't we use std::back_insert_iterator? 
> > > 
> > > http://en.cppreference.com/w/cpp/iterator/back_insert_iterator
> > 
> > Because Vector doesn't have a push_back().  Its iterator types also don't have the std standard typedefs, like value_type, etc.
> 
> I think it's much better to make our containers play well with the STL than it is to replicate STL functionality. (Our transition from OwnPtr to std::unique_ptr is an example of this).

I’m with Anders on this. Seems like a natural refinement to improve the Vector iterators, add push_back, and then use back_insert_iterator.
Comment 16 Jer Noble 2014-10-03 12:56:27 PDT
(In reply to comment #15)
> (In reply to comment #14)
> > (In reply to comment #13)
> > > (In reply to comment #9)
> > > > Why can't we use std::back_insert_iterator? 
> > > > 
> > > > http://en.cppreference.com/w/cpp/iterator/back_insert_iterator
> > > 
> > > Because Vector doesn't have a push_back().  Its iterator types also don't have the std standard typedefs, like value_type, etc.
> > 
> > I think it's much better to make our containers play well with the STL than it is to replicate STL functionality. (Our transition from OwnPtr to std::unique_ptr is an example of this).
> 
> I’m with Anders on this. Seems like a natural refinement to improve the Vector iterators, add push_back, and then use back_insert_iterator.

Okay, that's fine.  And back_insert_iterator was actually my primary use-case anyway.

But for insert_iterator, the default STL implementation won't work, as Vector invalidates all outstanding iterators when it does a re-alloc.  The version I posted here works around this by resetting the internal iterator in that scenario, so it may still be useful as a template specification version of std::insert_Iterator.
Comment 17 Darin Adler 2014-10-03 21:38:59 PDT
(In reply to comment #16)
> But for insert_iterator, the default STL implementation won't work, as Vector invalidates all outstanding iterators when it does a re-alloc.

How does insert_iterator work with std::vector? If the answer is that it doesn’t then we should consider whether we have a good reason that we want WTF::Vector to work with it.
Comment 18 Jer Noble 2014-10-03 21:59:57 PDT
(In reply to comment #17)
> (In reply to comment #16)
> > But for insert_iterator, the default STL implementation won't work, as Vector invalidates all outstanding iterators when it does a re-alloc.
> 
> How does insert_iterator work with std::vector? If the answer is that it doesn’t then we should consider whether we have a good reason that we want WTF::Vector to work with it.

std::vector has the same problem, in that all iterators are invalidated in the event of a re-alloc. The way you'd make a insert_iterator work with std::vector is the same as with WTF::Vector: by increasing the container's capacity with reserve()/reserveCapacity() before the insert operations.
Comment 19 Jer Noble 2014-10-03 22:54:37 PDT
Created attachment 239267 [details]
Patch

Trying the alternative approach of making Vector compatible with back_insert_iterator and insert_iterator. This requires Vector to have an insert(iterator, value) method, which causes compile-time errors due to an ambiguity between insert(iterator, value) and insert(size_t, value) when passing in a literal (e.g., 0). So Part 1 is to rename insert(size_t, value) to insertAt(size_t, value), and Part 2 will be adding a new insert(iterator, value) method.
Comment 20 Brent Fulgham 2016-08-22 11:23:56 PDT
Was this abandoned? Should we be getting it up-to-date and landed?
Comment 21 Michael Catanzaro 2016-09-17 07:05:35 PDT
Comment on attachment 239267 [details]
Patch

Hi,

Apologies that your patch was not reviewed in a timely manner. Since it's now quite old, I am removing it from the review request queue. Please consider rebasing it on trunk and resubmitting.

To increase the chances of getting a review, consider using 'Tools/Scripts/webkit-patch upload --suggest-reviewers' to CC reviewers who might be interested in this bug.