Closed Bug 1247412 Opened 9 years ago Closed 9 years ago

Add a `reverse` method to `mozilla::Vector`

Categories

(Core :: MFBT, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla47
Tracking Status
firefox47 --- fixed

People

(Reporter: fitzgen, Assigned: fitzgen)

References

Details

Attachments

(1 file, 1 obsolete file)

No description provided.
Attached patch Add a `reverse` method to `mozilla::Vector` (obsolete) (deleted) — Splinter Review
Attachment #8718076 - Flags: review?(jwalden+bmo)
Assignee: nobody → nfitzgerald
Status: NEW → ASSIGNED
Comment on attachment 8718076 [details] [diff] [review] Add a `reverse` method to `mozilla::Vector` Review of attachment 8718076 [details] [diff] [review]: ----------------------------------------------------------------- ::: mfbt/Vector.h @@ +517,5 @@ > ConstRange all() const { return ConstRange(begin(), end()); } > > /* mutators */ > > + void reverse() { Add MOZ_REENTRANCY_GUARD_ET_AL; since as documented earlier none of these methods are reentrant. And given that, we can slightly optimize this to avoid massive over-repetition of begin() here. @@ +521,5 @@ > + void reverse() { > + for (size_t i = 0; i < length() / 2; i++) { > + T temp(Move(begin()[i])); > + begin()[i] = Move(begin()[length() - i - 1]); > + begin()[length() - i - 1] = Move(temp); How about using mozilla::Swap instead of the manual local temp? So then you'd have T* elems = begin(); size_t len = length(); for (size_t i = 0, mid = len / 2; i < mid; i++) { Swap(elems[i], elems[len - i - 1]); } ::: mfbt/tests/TestVector.cpp @@ +167,5 @@ > > +void > +mozilla::detail::VectorTesting::testReverse() > +{ > + Vector<UniquePtr<uint8_t>> vec; Just for general sanity, could you test this on a Vector storing elements only in reserved space, and on a Vector storing elements in allocated space? Also test an empty array and an array with an even number of elements, to test the floordiv in the implementation doesn't have off-by-one issues.
Attachment #8718076 - Flags: review?(jwalden+bmo) → review+
Attachment #8718076 - Attachment is obsolete: true
Status: ASSIGNED → RESOLVED
Closed: 9 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla47
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: