microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
library/std/core.qs
85lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | namespace Microsoft.Quantum.Core { |
| 5 | /// # Summary |
| 6 | /// Returns the defined start value of the given range. |
| 7 | /// |
| 8 | /// # Input |
| 9 | /// ## r |
| 10 | /// Input range. |
| 11 | /// |
| 12 | /// # Output |
| 13 | /// The defined start value of the given range. |
| 14 | /// |
| 15 | /// # Remarks |
| 16 | /// A range expression's first element is `start`, |
| 17 | /// its second element is `start+step`, third element is `start+step+step`, etc., |
| 18 | /// until `end` is passed. |
| 19 | /// |
| 20 | /// Note that the defined start value of a range is the same as the first element of the sequence, |
| 21 | /// unless the range specifies an empty sequence (for example, 2 .. 1). |
| 22 | function RangeStart(r : Range) : Int { |
| 23 | r::Start |
| 24 | } |
| 25 | |
| 26 | |
| 27 | /// # Summary |
| 28 | /// Returns the defined end value of the given range, |
| 29 | /// which is not necessarily the last element in the sequence. |
| 30 | /// |
| 31 | /// # Input |
| 32 | /// ## r |
| 33 | /// Input range. |
| 34 | /// |
| 35 | /// # Output |
| 36 | /// The defined end value of the given range. |
| 37 | /// |
| 38 | /// # Remarks |
| 39 | /// A range expression's first element is `start`, |
| 40 | /// its second element is `start+step`, third element is `start+step+step`, etc., |
| 41 | /// until `end` is passed. |
| 42 | /// |
| 43 | /// Note that the defined end value of a range can differ from the last element in the sequence specified by the range; |
| 44 | /// for example, in a range 0 .. 2 .. 5 the last element is 4 but the end value is 5. |
| 45 | function RangeEnd(r : Range) : Int { |
| 46 | r::End |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /// # Summary |
| 51 | /// Returns the integer that specifies how the next value of a range is calculated. |
| 52 | /// |
| 53 | /// # Input |
| 54 | /// ## r |
| 55 | /// Input range. |
| 56 | /// |
| 57 | /// # Output |
| 58 | /// The defined step value of the given range. |
| 59 | /// |
| 60 | /// # Remarks |
| 61 | /// A range expression's first element is `start`, |
| 62 | /// its second element is `start+step`, third element is `start+step+step`, etc., |
| 63 | /// until `end` is passed. |
| 64 | function RangeStep(r : Range) : Int { |
| 65 | r::Step |
| 66 | } |
| 67 | |
| 68 | /// # Summary |
| 69 | /// Returns a new range which is the reverse of the input range. |
| 70 | /// |
| 71 | /// # Input |
| 72 | /// ## r |
| 73 | /// Input range. |
| 74 | /// |
| 75 | /// # Output |
| 76 | /// A new range that is the reverse of the given range. |
| 77 | /// |
| 78 | /// # Remarks |
| 79 | /// Note that the reverse of a range is not simply `end`..`-step`..`start`, because |
| 80 | /// the actual last element of a range may not be the same as `end`. |
| 81 | function RangeReverse(r : Range) : Range { |
| 82 | let start = r::Start + ((r::End - r::Start) / r::Step) * r::Step; |
| 83 | start..-r::Step..r::Start |
| 84 | } |
| 85 | } |
| 86 | |