Changeset 1567
- Timestamp:
- 05/29/10 14:18:18 (15 years ago)
- Files:
-
- trunk/phobos/std/range.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/phobos/std/range.d
r1566 r1567 1190 1190 ---- 1191 1191 */ 1192 1192 1193 1193 struct Take(R) if (isInputRange!(R)) 1194 1194 { 1195 1195 private: 1196 1196 R _input; 1197 1197 size_t _maxAvailable; 1198 1198 enum bool byRef = is(typeof(&_input.front) == ElementType!(R)*); 1199 1199 1200 enum bool backByIndex = // Take.back = input[min(n,$) - 1]1201 isRandomAccessRange!(R) && (hasLength!(R) || isInfinite!(R));1202 1203 enum bool backByBack = // Take.back = input.back1204 !backByIndex && isBidirectionalRange!(R) && hasLength!(R);1205 1206 1200 public: 1207 1201 alias R Source; 1208 1202 1209 1203 static if (byRef) 1210 1204 alias ref .ElementType!(R) ElementType; 1211 1205 else 1212 1206 alias .ElementType!(R) ElementType; 1213 1207 1214 this(R input, size_t maxAvailable)1215 {1216 static if (backByBack)1217 {1218 while (input.length > maxAvailable)1219 input.popBack;1220 }1221 _input = input;1222 _maxAvailable = maxAvailable;1223 }1224 1225 1208 bool empty() 1226 1209 { 1227 1210 return _maxAvailable == 0 || _input.empty; 1228 1211 } 1229 1212 1230 1213 void popFront() 1231 1214 { 1232 1215 enforce(_maxAvailable > 0, 1233 "Atte npting to popFront() past the end of a "1216 "Attempting to popFront() past the end of a " 1234 1217 ~ Take.stringof); 1235 1218 _input.popFront; 1236 1219 --_maxAvailable; 1237 1220 } 1238 1221 1239 1222 // @@@@@@@@@@@ UGLY @@@@@@@@@@@@@@@ 1240 1223 mixin( 1241 1224 (byRef ? "ref " : "")~ 1242 1225 q{ElementType front() 1243 1226 { … … 1255 1238 } 1256 1239 } 1257 1240 else static if (hasLength!(R)) 1258 1241 { 1259 1242 @property size_t length() 1260 1243 { 1261 1244 return min(_maxAvailable, _input.length); 1262 1245 } 1263 1246 } 1264 1247 1265 static if ( backByIndex)1248 static if (isRandomAccessRange!(R) && (hasLength!(R) || isInfinite!(R))) 1266 1249 { 1267 1250 void popBack() 1268 1251 { 1269 1252 enforce(_maxAvailable > 0, 1270 "Atte npting to popBack() past the beginning of a "1253 "Attempting to popBack() past the beginning of a " 1271 1254 ~ Take.stringof); 1272 1255 --_maxAvailable; 1273 1256 } 1274 1257 1275 1258 mixin( 1276 1259 (byRef ? "ref " : "")~ 1277 1260 q{/+auto ref+/ ElementType back() 1278 1261 { 1279 1262 return _input[this.length - 1]; 1280 }});1281 }1282 else static if (backByBack)1283 {1284 invariant()1285 {1286 assert(_input.length <= _maxAvailable);1287 }1288 1289 void popBack()1290 {1291 enforce(_maxAvailable > 0,1292 "Attenpting to popBack() past the beginning of a "1293 ~ Take.stringof);1294 --_maxAvailable;1295 _input.popBack;1296 }1297 1298 mixin(1299 (byRef ? "ref " : "")~1300 q{/+auto ref+/ ElementType back()1301 {1302 return _input.back;1303 1263 }}); 1304 1264 } 1305 1265 1306 1266 static if (isRandomAccessRange!(R)) 1307 1267 { 1308 1268 mixin( 1309 1269 (byRef ? "ref " : "")~ 1310 1270 q{/+auto ref+/ ElementType opIndex(size_t index) 1311 1271 { 1312 1272 enforce(index < this.length,
