]> git.lizzy.rs Git - rust.git/blob - src/libcore/ops/range.rs
Auto merge of #47956 - retep998:is-nibbles, r=BurntSushi
[rust.git] / src / libcore / ops / range.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use fmt;
12
13 /// An unbounded range (`..`).
14 ///
15 /// `RangeFull` is primarily used as a [slicing index], its shorthand is `..`.
16 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
17 ///
18 /// # Examples
19 ///
20 /// The `..` syntax is a `RangeFull`:
21 ///
22 /// ```
23 /// assert_eq!((..), std::ops::RangeFull);
24 /// ```
25 ///
26 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
27 /// a `for` loop directly. This won't compile:
28 ///
29 /// ```compile_fail,E0277
30 /// for i in .. {
31 ///    // ...
32 /// }
33 /// ```
34 ///
35 /// Used as a [slicing index], `RangeFull` produces the full array as a slice.
36 ///
37 /// ```
38 /// let arr = [0, 1, 2, 3];
39 /// assert_eq!(arr[ .. ], [0,1,2,3]);  // RangeFull
40 /// assert_eq!(arr[ ..3], [0,1,2  ]);
41 /// assert_eq!(arr[1.. ], [  1,2,3]);
42 /// assert_eq!(arr[1..3], [  1,2  ]);
43 /// ```
44 ///
45 /// [`IntoIterator`]: ../iter/trait.Iterator.html
46 /// [`Iterator`]: ../iter/trait.IntoIterator.html
47 /// [slicing index]: ../slice/trait.SliceIndex.html
48 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
49 #[stable(feature = "rust1", since = "1.0.0")]
50 pub struct RangeFull;
51
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl fmt::Debug for RangeFull {
54     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
55         write!(fmt, "..")
56     }
57 }
58
59 /// A (half-open) range bounded inclusively below and exclusively above
60 /// (`start..end`).
61 ///
62 /// The `Range` `start..end` contains all values with `x >= start` and
63 /// `x < end`.  It is empty unless `start < end`.
64 ///
65 /// # Examples
66 ///
67 /// ```
68 /// assert_eq!((3..5), std::ops::Range { start: 3, end: 5 });
69 /// assert_eq!(3 + 4 + 5, (3..6).sum());
70 ///
71 /// let arr = ['a', 'b', 'c', 'd'];
72 /// assert_eq!(arr[ .. ], ['a', 'b', 'c', 'd']);
73 /// assert_eq!(arr[ ..3], ['a', 'b', 'c',    ]);
74 /// assert_eq!(arr[1.. ], [     'b', 'c', 'd']);
75 /// assert_eq!(arr[1..3], [     'b', 'c'     ]);  // Range
76 /// ```
77 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
78 #[stable(feature = "rust1", since = "1.0.0")]
79 pub struct Range<Idx> {
80     /// The lower bound of the range (inclusive).
81     #[stable(feature = "rust1", since = "1.0.0")]
82     pub start: Idx,
83     /// The upper bound of the range (exclusive).
84     #[stable(feature = "rust1", since = "1.0.0")]
85     pub end: Idx,
86 }
87
88 #[stable(feature = "rust1", since = "1.0.0")]
89 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
90     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
91         write!(fmt, "{:?}..{:?}", self.start, self.end)
92     }
93 }
94
95 impl<Idx: PartialOrd<Idx>> Range<Idx> {
96     /// Returns `true` if `item` is contained in the range.
97     ///
98     /// # Examples
99     ///
100     /// ```
101     /// #![feature(range_contains)]
102     ///
103     /// assert!(!(3..5).contains(2));
104     /// assert!( (3..5).contains(3));
105     /// assert!( (3..5).contains(4));
106     /// assert!(!(3..5).contains(5));
107     ///
108     /// assert!(!(3..3).contains(3));
109     /// assert!(!(3..2).contains(3));
110     /// ```
111     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
112     pub fn contains(&self, item: Idx) -> bool {
113         (self.start <= item) && (item < self.end)
114     }
115
116     /// Returns `true` if the range contains no items.
117     ///
118     /// # Examples
119     ///
120     /// ```
121     /// #![feature(range_is_empty)]
122     ///
123     /// assert!(!(3..5).is_empty());
124     /// assert!( (3..3).is_empty());
125     /// assert!( (3..2).is_empty());
126     /// ```
127     ///
128     /// The range is empty if either side is incomparable:
129     ///
130     /// ```
131     /// #![feature(range_is_empty,inclusive_range_syntax)]
132     ///
133     /// use std::f32::NAN;
134     /// assert!(!(3.0..5.0).is_empty());
135     /// assert!( (3.0..NAN).is_empty());
136     /// assert!( (NAN..5.0).is_empty());
137     /// ```
138     #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
139     pub fn is_empty(&self) -> bool {
140         !(self.start < self.end)
141     }
142 }
143
144 /// A range only bounded inclusively below (`start..`).
145 ///
146 /// The `RangeFrom` `start..` contains all values with `x >= start`.
147 ///
148 /// *Note*: Currently, no overflow checking is done for the [`Iterator`]
149 /// implementation; if you use an integer range and the integer overflows, it
150 /// might panic in debug mode or create an endless loop in release mode. **This
151 /// overflow behavior might change in the future.**
152 ///
153 /// # Examples
154 ///
155 /// ```
156 /// assert_eq!((2..), std::ops::RangeFrom { start: 2 });
157 /// assert_eq!(2 + 3 + 4, (2..).take(3).sum());
158 ///
159 /// let arr = [0, 1, 2, 3];
160 /// assert_eq!(arr[ .. ], [0,1,2,3]);
161 /// assert_eq!(arr[ ..3], [0,1,2  ]);
162 /// assert_eq!(arr[1.. ], [  1,2,3]);  // RangeFrom
163 /// assert_eq!(arr[1..3], [  1,2  ]);
164 /// ```
165 ///
166 /// [`Iterator`]: ../iter/trait.IntoIterator.html
167 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
168 #[stable(feature = "rust1", since = "1.0.0")]
169 pub struct RangeFrom<Idx> {
170     /// The lower bound of the range (inclusive).
171     #[stable(feature = "rust1", since = "1.0.0")]
172     pub start: Idx,
173 }
174
175 #[stable(feature = "rust1", since = "1.0.0")]
176 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
177     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
178         write!(fmt, "{:?}..", self.start)
179     }
180 }
181
182 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
183 impl<Idx: PartialOrd<Idx>> RangeFrom<Idx> {
184     /// Returns `true` if `item` is contained in the range.
185     ///
186     /// # Examples
187     ///
188     /// ```
189     /// #![feature(range_contains)]
190     ///
191     /// assert!(!(3..).contains(2));
192     /// assert!( (3..).contains(3));
193     /// assert!( (3..).contains(1_000_000_000));
194     /// ```
195     pub fn contains(&self, item: Idx) -> bool {
196         (self.start <= item)
197     }
198 }
199
200 /// A range only bounded exclusively above (`..end`).
201 ///
202 /// The `RangeTo` `..end` contains all values with `x < end`.
203 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
204 ///
205 /// # Examples
206 ///
207 /// The `..end` syntax is a `RangeTo`:
208 ///
209 /// ```
210 /// assert_eq!((..5), std::ops::RangeTo { end: 5 });
211 /// ```
212 ///
213 /// It does not have an [`IntoIterator`] implementation, so you can't use it in
214 /// a `for` loop directly. This won't compile:
215 ///
216 /// ```compile_fail,E0277
217 /// // error[E0277]: the trait bound `std::ops::RangeTo<{integer}>:
218 /// // std::iter::Iterator` is not satisfied
219 /// for i in ..5 {
220 ///     // ...
221 /// }
222 /// ```
223 ///
224 /// When used as a [slicing index], `RangeTo` produces a slice of all array
225 /// elements before the index indicated by `end`.
226 ///
227 /// ```
228 /// let arr = [0, 1, 2, 3];
229 /// assert_eq!(arr[ .. ], [0,1,2,3]);
230 /// assert_eq!(arr[ ..3], [0,1,2  ]);  // RangeTo
231 /// assert_eq!(arr[1.. ], [  1,2,3]);
232 /// assert_eq!(arr[1..3], [  1,2  ]);
233 /// ```
234 ///
235 /// [`IntoIterator`]: ../iter/trait.Iterator.html
236 /// [`Iterator`]: ../iter/trait.IntoIterator.html
237 /// [slicing index]: ../slice/trait.SliceIndex.html
238 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
239 #[stable(feature = "rust1", since = "1.0.0")]
240 pub struct RangeTo<Idx> {
241     /// The upper bound of the range (exclusive).
242     #[stable(feature = "rust1", since = "1.0.0")]
243     pub end: Idx,
244 }
245
246 #[stable(feature = "rust1", since = "1.0.0")]
247 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
248     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
249         write!(fmt, "..{:?}", self.end)
250     }
251 }
252
253 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
254 impl<Idx: PartialOrd<Idx>> RangeTo<Idx> {
255     /// Returns `true` if `item` is contained in the range.
256     ///
257     /// # Examples
258     ///
259     /// ```
260     /// #![feature(range_contains)]
261     ///
262     /// assert!( (..5).contains(-1_000_000_000));
263     /// assert!( (..5).contains(4));
264     /// assert!(!(..5).contains(5));
265     /// ```
266     pub fn contains(&self, item: Idx) -> bool {
267         (item < self.end)
268     }
269 }
270
271 /// An range bounded inclusively below and above (`start..=end`).
272 ///
273 /// The `RangeInclusive` `start..=end` contains all values with `x >= start`
274 /// and `x <= end`.  It is empty unless `start <= end`.
275 ///
276 /// This iterator is [fused], but the specific values of `start` and `end` after
277 /// iteration has finished are **unspecified** other than that [`.is_empty()`]
278 /// will return `true` once no more values will be produced.
279 ///
280 /// [fused]: ../iter/trait.FusedIterator.html
281 /// [`.is_empty()`]: #method.is_empty
282 ///
283 /// # Examples
284 ///
285 /// ```
286 /// #![feature(inclusive_range,inclusive_range_syntax)]
287 ///
288 /// assert_eq!((3..=5), std::ops::RangeInclusive { start: 3, end: 5 });
289 /// assert_eq!(3 + 4 + 5, (3..=5).sum());
290 ///
291 /// let arr = [0, 1, 2, 3];
292 /// assert_eq!(arr[ ..=2], [0,1,2  ]);
293 /// assert_eq!(arr[1..=2], [  1,2  ]);  // RangeInclusive
294 /// ```
295 #[derive(Clone, PartialEq, Eq, Hash)]  // not Copy -- see #27186
296 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
297 pub struct RangeInclusive<Idx> {
298     /// The lower bound of the range (inclusive).
299     #[unstable(feature = "inclusive_range",
300                reason = "recently added, follows RFC",
301                issue = "28237")]
302     pub start: Idx,
303     /// The upper bound of the range (inclusive).
304     #[unstable(feature = "inclusive_range",
305                reason = "recently added, follows RFC",
306                issue = "28237")]
307     pub end: Idx,
308 }
309
310 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
311 impl<Idx: fmt::Debug> fmt::Debug for RangeInclusive<Idx> {
312     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
313         write!(fmt, "{:?}..={:?}", self.start, self.end)
314     }
315 }
316
317 impl<Idx: PartialOrd<Idx>> RangeInclusive<Idx> {
318     /// Returns `true` if `item` is contained in the range.
319     ///
320     /// # Examples
321     ///
322     /// ```
323     /// #![feature(range_contains,inclusive_range_syntax)]
324     ///
325     /// assert!(!(3..=5).contains(2));
326     /// assert!( (3..=5).contains(3));
327     /// assert!( (3..=5).contains(4));
328     /// assert!( (3..=5).contains(5));
329     /// assert!(!(3..=5).contains(6));
330     ///
331     /// assert!( (3..=3).contains(3));
332     /// assert!(!(3..=2).contains(3));
333     /// ```
334     #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
335     pub fn contains(&self, item: Idx) -> bool {
336         self.start <= item && item <= self.end
337     }
338
339     /// Returns `true` if the range contains no items.
340     ///
341     /// # Examples
342     ///
343     /// ```
344     /// #![feature(range_is_empty,inclusive_range_syntax)]
345     ///
346     /// assert!(!(3..=5).is_empty());
347     /// assert!(!(3..=3).is_empty());
348     /// assert!( (3..=2).is_empty());
349     /// ```
350     ///
351     /// The range is empty if either side is incomparable:
352     ///
353     /// ```
354     /// #![feature(range_is_empty,inclusive_range_syntax)]
355     ///
356     /// use std::f32::NAN;
357     /// assert!(!(3.0..=5.0).is_empty());
358     /// assert!( (3.0..=NAN).is_empty());
359     /// assert!( (NAN..=5.0).is_empty());
360     /// ```
361     ///
362     /// This method returns `true` after iteration has finished:
363     ///
364     /// ```
365     /// #![feature(range_is_empty,inclusive_range_syntax)]
366     ///
367     /// let mut r = 3..=5;
368     /// for _ in r.by_ref() {}
369     /// // Precise field values are unspecified here
370     /// assert!(r.is_empty());
371     /// ```
372     #[unstable(feature = "range_is_empty", reason = "recently added", issue = "48111")]
373     pub fn is_empty(&self) -> bool {
374         !(self.start <= self.end)
375     }
376 }
377
378 /// A range only bounded inclusively above (`..=end`).
379 ///
380 /// The `RangeToInclusive` `..=end` contains all values with `x <= end`.
381 /// It cannot serve as an [`Iterator`] because it doesn't have a starting point.
382 ///
383 /// # Examples
384 ///
385 /// The `..=end` syntax is a `RangeToInclusive`:
386 ///
387 /// ```
388 /// #![feature(inclusive_range,inclusive_range_syntax)]
389 /// assert_eq!((..=5), std::ops::RangeToInclusive{ end: 5 });
390 /// ```
391 ///
392 /// It does not have an [`IntoIterator`] implementation, so you can't use it in a
393 /// `for` loop directly. This won't compile:
394 ///
395 /// ```compile_fail,E0277
396 /// #![feature(inclusive_range_syntax)]
397 ///
398 /// // error[E0277]: the trait bound `std::ops::RangeToInclusive<{integer}>:
399 /// // std::iter::Iterator` is not satisfied
400 /// for i in ..=5 {
401 ///     // ...
402 /// }
403 /// ```
404 ///
405 /// When used as a [slicing index], `RangeToInclusive` produces a slice of all
406 /// array elements up to and including the index indicated by `end`.
407 ///
408 /// ```
409 /// #![feature(inclusive_range_syntax)]
410 ///
411 /// let arr = [0, 1, 2, 3];
412 /// assert_eq!(arr[ ..=2], [0,1,2  ]);  // RangeToInclusive
413 /// assert_eq!(arr[1..=2], [  1,2  ]);
414 /// ```
415 ///
416 /// [`IntoIterator`]: ../iter/trait.Iterator.html
417 /// [`Iterator`]: ../iter/trait.IntoIterator.html
418 /// [slicing index]: ../slice/trait.SliceIndex.html
419 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
420 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
421 pub struct RangeToInclusive<Idx> {
422     /// The upper bound of the range (inclusive)
423     #[unstable(feature = "inclusive_range",
424                reason = "recently added, follows RFC",
425                issue = "28237")]
426     pub end: Idx,
427 }
428
429 #[unstable(feature = "inclusive_range", reason = "recently added, follows RFC", issue = "28237")]
430 impl<Idx: fmt::Debug> fmt::Debug for RangeToInclusive<Idx> {
431     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
432         write!(fmt, "..={:?}", self.end)
433     }
434 }
435
436 #[unstable(feature = "range_contains", reason = "recently added as per RFC", issue = "32311")]
437 impl<Idx: PartialOrd<Idx>> RangeToInclusive<Idx> {
438     /// Returns `true` if `item` is contained in the range.
439     ///
440     /// # Examples
441     ///
442     /// ```
443     /// #![feature(range_contains,inclusive_range_syntax)]
444     ///
445     /// assert!( (..=5).contains(-1_000_000_000));
446     /// assert!( (..=5).contains(5));
447     /// assert!(!(..=5).contains(6));
448     /// ```
449     pub fn contains(&self, item: Idx) -> bool {
450         (item <= self.end)
451     }
452 }
453
454 // RangeToInclusive<Idx> cannot impl From<RangeTo<Idx>>
455 // because underflow would be possible with (..0).into()