@@ -1156,7 +1156,52 @@ impl<T: PartialEq> Vec<T> {
1156
1156
/// assert_eq!(vec, [1, 2, 3, 2]);
1157
1157
/// ```
1158
1158
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1159
+ #[ inline]
1159
1160
pub fn dedup ( & mut self ) {
1161
+ self . dedup_by ( |a, b| a == b)
1162
+ }
1163
+ }
1164
+
1165
+ impl < T > Vec < T > {
1166
+ /// Removes consecutive elements in the vector that resolve to the same key.
1167
+ ///
1168
+ /// If the vector is sorted, this removes all duplicates.
1169
+ ///
1170
+ /// # Examples
1171
+ ///
1172
+ /// ```
1173
+ /// #![feature(dedup_by)]
1174
+ ///
1175
+ /// let mut vec = vec![10, 20, 21, 30, 20];
1176
+ ///
1177
+ /// vec.dedup_by_key(|i| *i / 10);
1178
+ ///
1179
+ /// assert_eq!(vec, [10, 20, 30, 20]);
1180
+ /// ```
1181
+ #[ unstable( feature = "dedup_by" , reason = "recently added" , issue = "37087" ) ]
1182
+ #[ inline]
1183
+ pub fn dedup_by_key < F , K > ( & mut self , mut key : F ) where F : FnMut ( & mut T ) -> K , K : PartialEq {
1184
+ self . dedup_by ( |a, b| key ( a) == key ( b) )
1185
+ }
1186
+
1187
+ /// Removes consecutive elements in the vector that resolve to the same key.
1188
+ ///
1189
+ /// If the vector is sorted, this removes all duplicates.
1190
+ ///
1191
+ /// # Examples
1192
+ ///
1193
+ /// ```
1194
+ /// #![feature(dedup_by)]
1195
+ /// use std::ascii::AsciiExt;
1196
+ ///
1197
+ /// let mut vec = vec!["foo", "bar", "Bar", "baz", "bar"];
1198
+ ///
1199
+ /// vec.dedup_by(|a, b| a.eq_ignore_ascii_case(b));
1200
+ ///
1201
+ /// assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
1202
+ /// ```
1203
+ #[ unstable( feature = "dedup_by" , reason = "recently added" , issue = "37087" ) ]
1204
+ pub fn dedup_by < F > ( & mut self , mut same_bucket : F ) where F : FnMut ( & mut T , & mut T ) -> bool {
1160
1205
unsafe {
1161
1206
// Although we have a mutable reference to `self`, we cannot make
1162
1207
// *arbitrary* changes. The `PartialEq` comparisons could panic, so we
@@ -1228,7 +1273,7 @@ impl<T: PartialEq> Vec<T> {
1228
1273
while r < ln {
1229
1274
let p_r = p. offset ( r as isize ) ;
1230
1275
let p_wm1 = p. offset ( ( w - 1 ) as isize ) ;
1231
- if * p_r != * p_wm1 {
1276
+ if ! same_bucket ( & mut * p_r, & mut * p_wm1) {
1232
1277
if r != w {
1233
1278
let p_w = p_wm1. offset ( 1 ) ;
1234
1279
mem:: swap ( & mut * p_r, & mut * p_w) ;
0 commit comments