| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 | |
| 4 | //! [0] |
| 5 | QCircularBuffer<int> integerBuffer; |
| 6 | QCircularBuffer<QString> stringBuffer; |
| 7 | //! [0] |
| 8 | |
| 9 | |
| 10 | //! [1] |
| 11 | QCircularBuffer<QString> circ(200); |
| 12 | //! [1] |
| 13 | |
| 14 | |
| 15 | //! [2] |
| 16 | QCircularBuffer<QString> circ(200, "Pass"); |
| 17 | //! [2] |
| 18 | |
| 19 | |
| 20 | //! [3] |
| 21 | QCircularBuffer<QString> circ(200, 50, "Qt"); |
| 22 | //! [3] |
| 23 | |
| 24 | |
| 25 | //! [4] |
| 26 | if (circ[0] == "Izzie") |
| 27 | circ[0] = "Elizabeth"; |
| 28 | //! [4] |
| 29 | |
| 30 | |
| 31 | //! [5] |
| 32 | for (int i = 0; i < circ.size(); ++i) { |
| 33 | if (circ.at(i) == "Jack") |
| 34 | cout << "Found Jack at position " << i << endl; |
| 35 | } |
| 36 | //! [5] |
| 37 | |
| 38 | |
| 39 | //! [6] |
| 40 | // Create a circular buffer with capacity for 3 integers |
| 41 | QCircularBuffer<int> circ(3); |
| 42 | |
| 43 | // Insert some items into the buffer |
| 44 | circ.append( 1 ); |
| 45 | circ.append( 2 ); |
| 46 | circ.append( 3 ); |
| 47 | |
| 48 | for (int i = 0; i < circ.size(); ++i) { |
| 49 | cout << circ.at(i) << endl; |
| 50 | } // Prints out 1 2 3 |
| 51 | |
| 52 | // The circular buffer is now full. Appending subsequent items |
| 53 | // will overwrite the oldest items: |
| 54 | circ.append(4); // Overwrite 1 with 4 |
| 55 | circ.append(5); // Overwrite 2 with 5 |
| 56 | |
| 57 | // The buffer now contains 3, 4 and 5 |
| 58 | |
| 59 | for (int i = 0; i < circ.size(); ++i) { |
| 60 | cout << circ.at(i) << endl; |
| 61 | } // Prints out 3 4 5 |
| 62 | //! [6] |
| 63 | |
| 64 | |
| 65 | //! [7] |
| 66 | int i = circ.indexOf("Tom"); |
| 67 | if (i != -1) |
| 68 | cout << "First occurrence of Tom is at position " << i << endl; |
| 69 | //! [7] |
| 70 | |
| 71 | |
| 72 | //! [8] |
| 73 | QCircularBuffer<QString> circ(3); |
| 74 | circ.append("one"); |
| 75 | circ.append("two"); |
| 76 | circ.append("three"); |
| 77 | // circ: ["one", "two", "three"] |
| 78 | |
| 79 | circ.append("four"); |
| 80 | // circ: ["two", "three", "four"] |
| 81 | |
| 82 | circ.append("five"); |
| 83 | // circ: ["three", "four", "five"] |
| 84 | |
| 85 | circ.append("six"); |
| 86 | // circ: ["four", "five", "six"] |
| 87 | //! [8] |
| 88 | |
| 89 | |
| 90 | //! [9] |
| 91 | QCircularBuffer<int> circ(10, 0); |
| 92 | QCircularBuffer<int>::array_range data = circ.data(); |
| 93 | for (int i = 0; i < data.second; ++i) |
| 94 | data.first[i] = 2 * i; |
| 95 | //! [9] |
| 96 | |
| 97 | |
| 98 | //! [10] |
| 99 | QCircularBuffer<QString> circ(3); |
| 100 | circ.prepend("one"); |
| 101 | circ.prepend("two"); |
| 102 | circ.prepend("three"); |
| 103 | // circ: ["three", "two", "one"] |
| 104 | |
| 105 | circ.prepend("four"); |
| 106 | // circ: ["four", "three", "two"] |
| 107 | //! [10] |
| 108 | |
| 109 | |
| 110 | //! [11] |
| 111 | QCircularBuffer<QString> circ(5); |
| 112 | circ << "alpha" << "beta" << "delta"; |
| 113 | circ.insert(2, "gamma"); |
| 114 | // circ: ["alpha", "beta", "gamma", "delta"] |
| 115 | //! [11] |
| 116 | |
| 117 | //! [12] |
| 118 | QCircularBuffer<int> circ(6); |
| 119 | circ << 1 << 2 << 3 << 4; // circ: [1, 2, 3, 4] |
| 120 | circ.insert(2, 5, 0); |
| 121 | // circ: [0, 0, 0, 0, 3, 4] |
| 122 | //! [12] |
| 123 | |
| 124 | |
| 125 | //! [13] |
| 126 | QList<double> list; |
| 127 | list << 2.718 << 1.442 << 0.4342; |
| 128 | list.insert(1, 3, 9.9); |
| 129 | // list: [2.718, 9.9, 9.9, 9.9, 1.442, 0.4342] |
| 130 | //! [13] |
| 131 | |
| 132 | |
| 133 | //! [14] |
| 134 | QCircularBuffer<QString> circ(3, 3, "No"); |
| 135 | circ.fill("Yes"); |
| 136 | // circ: ["Yes", "Yes", "Yes"] |
| 137 | |
| 138 | circ.fill("oh", 5); |
| 139 | // circ: ["oh", "oh", "oh", "oh", "oh"] |
| 140 | //! [14] |
| 141 | |
| 142 | |
| 143 | //! [15] |
| 144 | QCircularBuffer<QString> circ(5); |
| 145 | circ << "A" << "B" << "C" << "B" << "A"; |
| 146 | circ.indexOf("B"); // returns 1 |
| 147 | circ.indexOf("B", 1); // returns 1 |
| 148 | circ.indexOf("B", 2); // returns 3 |
| 149 | circ.indexOf("X"); // returns -1 |
| 150 | //! [15] |
| 151 | |
| 152 | |
| 153 | //! [16] |
| 154 | QCircularBuffer<QString> circ; |
| 155 | circ << "A" << "B" << "C" << "B" << "A"; |
| 156 | circ.lastIndexOf("B"); // returns 3 |
| 157 | circ.lastIndexOf("B", 3); // returns 3 |
| 158 | circ.lastIndexOf("B", 2); // returns 1 |
| 159 | circ.lastIndexOf("X"); // returns -1 |
| 160 | //! [16] |
| 161 | |
| 162 | |
| 163 | //! [17] |
| 164 | QCircularBuffer<QString> circ; |
| 165 | circ << "red" << "green" << "blue" << "black"; |
| 166 | |
| 167 | QList<QString> list = circ.toList(); |
| 168 | // list: ["red", "green", "blue", "black"] |
| 169 | //! [17] |
| 170 | |
| 171 | |
| 172 | //! [18] |
| 173 | QStringList list; |
| 174 | list << "Sven" << "Kim" << "Ola"; |
| 175 | |
| 176 | QCircularBuffer<QString> circ = QCircularBuffer<QString>::fromList(list); |
| 177 | // circ: ["Sven", "Kim", "Ola"] |
| 178 | //! [18] |
| 179 | |
| 180 | |
| 181 | //! [19] |
| 182 | QCircularBuffer<int> circ(6); |
| 183 | circ << 1 << 2 << 3 << 4 << 5; |
| 184 | // circ: [1, 2, 3, 4, 5] |
| 185 | //! [19] |
| 186 | |
| 187 | |
| 188 | //! [20] |
| 189 | circ.append(6); |
| 190 | // circ: [1, 2, 3, 4, 5, 6] |
| 191 | //! [20] |
| 192 | |
| 193 | |
| 194 | //! [21] |
| 195 | circ.append(7); |
| 196 | // circ: [2, 3, 4, 5, 6, 7] |
| 197 | //! [21] |