1 // Written in the D programming language. 2 3 /** 4 This module defines generic containers. 5 6 Construction: 7 8 To implement the different containers both struct and class based 9 approaches have been used. $(REF make, std,_container,util) allows for 10 uniform construction with either approach. 11 12 --- 13 import std.container; 14 // Construct a red-black tree and an array both containing the values 1, 2, 3. 15 // RedBlackTree should typically be allocated using `new` 16 RedBlackTree!int rbTree = new RedBlackTree!int(1, 2, 3); 17 // But `new` should not be used with Array 18 Array!int array = Array!int(1, 2, 3); 19 // `make` hides the differences 20 RedBlackTree!int rbTree2 = make!(RedBlackTree!int)(1, 2, 3); 21 Array!int array2 = make!(Array!int)(1, 2, 3); 22 --- 23 24 Note that $(D make) can infer the element type from the given arguments. 25 26 --- 27 import std.container; 28 auto rbTree = make!RedBlackTree(1, 2, 3); // RedBlackTree!int 29 auto array = make!Array("1", "2", "3"); // Array!string 30 --- 31 32 Reference_semantics: 33 34 All containers have reference semantics, which means that after 35 assignment both variables refer to the same underlying data. 36 37 To make a copy of a _container, use the $(D c._dup) _container primitive. 38 --- 39 import std.container, std.range; 40 Array!int originalArray = make!(Array!int)(1, 2, 3); 41 Array!int secondArray = originalArray; 42 assert(equal(originalArray[], secondArray[])); 43 44 // changing one instance changes the other one as well! 45 originalArray[0] = 12; 46 assert(secondArray[0] == 12); 47 48 // secondArray now refers to an independent copy of originalArray 49 secondArray = originalArray.dup; 50 secondArray[0] = 1; 51 // assert that originalArray has not been affected 52 assert(originalArray[0] == 12); 53 --- 54 55 $(B Attention:) If the _container is implemented as a class, using an 56 uninitialized instance can cause a null pointer dereference. 57 58 --- 59 import std.container; 60 61 RedBlackTree!int rbTree; 62 rbTree.insert(5); // null pointer dereference 63 --- 64 65 Using an uninitialized struct-based _container will work, because the struct 66 intializes itself upon use; however, up to this point the _container will not 67 have an identity and assignment does not create two references to the same 68 data. 69 70 --- 71 import std.container; 72 73 // create an uninitialized array 74 Array!int array1; 75 // array2 does _not_ refer to array1 76 Array!int array2 = array1; 77 array2.insertBack(42); 78 // thus array1 will not be affected 79 assert(array1.empty); 80 81 // after initialization reference semantics work as expected 82 array1 = array2; 83 // now affects array2 as well 84 array1.removeBack(); 85 assert(array2.empty); 86 --- 87 It is therefore recommended to always construct containers using 88 $(REF make, std,_container,util). 89 90 This is in fact necessary to put containers into another _container. 91 For example, to construct an $(D Array) of ten empty $(D Array)s, use 92 the following that calls $(D make) ten times. 93 94 --- 95 import std.container, std.range; 96 97 auto arrOfArrs = make!Array(generate!(() => make!(Array!int)).take(10)); 98 --- 99 100 Submodules: 101 102 This module consists of the following submodules: 103 104 $(UL 105 $(LI 106 The $(MREF std, _container, array) module provides 107 an array type with deterministic control of memory, not reliant on 108 the GC unlike built-in arrays. 109 ) 110 $(LI 111 The $(MREF std, _container, binaryheap) module 112 provides a binary heap implementation that can be applied to any 113 user-provided random-access range. 114 ) 115 $(LI 116 The $(MREF std, _container, dlist) module provides 117 a doubly-linked list implementation. 118 ) 119 $(LI 120 The $(MREF std, _container, rbtree) module 121 implements red-black trees. 122 ) 123 $(LI 124 The $(MREF std, _container, slist) module 125 implements singly-linked lists. 126 ) 127 $(LI 128 The $(MREF std, _container, util) module contains 129 some generic tools commonly used by _container implementations. 130 ) 131 ) 132 133 The_primary_range_of_a_container: 134 135 While some _containers offer direct access to their elements e.g. via 136 $(D opIndex), $(D c.front) or $(D c.back), access 137 and modification of a _container's contents is generally done through 138 its primary $(MREF_ALTTEXT range, std, range) type, 139 which is aliased as $(D C.Range). For example, the primary range type of 140 $(D Array!int) is $(D Array!int.Range). 141 142 If the documentation of a member function of a _container takes 143 a parameter of type $(D Range), then it refers to the primary range type of 144 this _container. Oftentimes $(D Take!Range) will be used, in which case 145 the range refers to a span of the elements in the _container. Arguments to 146 these parameters $(B must) be obtained from the same _container instance 147 as the one being worked with. It is important to note that many generic range 148 algorithms return the same range type as their input range. 149 150 --- 151 import std.algorithm.comparison : equal; 152 import std.algorithm.iteration : find; 153 import std.container; 154 import std.range : take; 155 156 auto array = make!Array(1, 2, 3); 157 158 // `find` returns an Array!int.Range advanced to the element "2" 159 array.linearRemove(array[].find(2)); 160 161 assert(array[].equal([1])); 162 163 array = make!Array(1, 2, 3); 164 165 // the range given to `linearRemove` is a Take!(Array!int.Range) 166 // spanning just the element "2" 167 array.linearRemove(array[].find(2).take(1)); 168 169 assert(array[].equal([1, 3])); 170 --- 171 172 When any $(MREF_ALTTEXT range, std, range) can be passed as an argument to 173 a member function, the documention usually refers to the parameter's templated 174 type as $(D Stuff). 175 176 --- 177 import std.algorithm.comparison : equal; 178 import std.container; 179 import std.range : iota; 180 181 auto array = make!Array(1, 2); 182 183 // the range type returned by `iota` is completely unrelated to Array, 184 // which is fine for Array.insertBack: 185 array.insertBack(iota(3, 10)); 186 187 assert(array[].equal([1, 2, 3, 4, 5, 6, 7, 8, 9])); 188 --- 189 190 Container_primitives: 191 192 Containers do not form a class hierarchy, instead they implement a 193 common set of primitives (see table below). These primitives each guarantee 194 a specific worst case complexity and thus allow generic code to be written 195 independently of the _container implementation. 196 197 For example the primitives $(D c.remove(r)) and $(D c.linearRemove(r)) both 198 remove the sequence of elements in range $(D r) from the _container $(D c). 199 The primitive $(D c.remove(r)) guarantees 200 $(BIGOH n$(SUBSCRIPT r) log n$(SUBSCRIPT c)) complexity in the worst case and 201 $(D c.linearRemove(r)) relaxes this guarantee to $(BIGOH n$(SUBSCRIPT c)). 202 203 Since a sequence of elements can be removed from a $(MREF_ALTTEXT doubly linked list,std,_container,dlist) 204 in constant time, $(D DList) provides the primitive $(D c.remove(r)) 205 as well as $(D c.linearRemove(r)). On the other hand 206 $(MREF_ALTTEXT Array, std,_container, array) only offers $(D c.linearRemove(r)). 207 208 The following table describes the common set of primitives that containers 209 implement. A _container need not implement all primitives, but if a 210 primitive is implemented, it must support the syntax described in the $(B 211 syntax) column with the semantics described in the $(B description) column, and 212 it must not have a worst-case complexity worse than denoted in big-O notation in 213 the $(BIGOH ·) column. Below, $(D C) means a _container type, $(D c) is 214 a value of _container type, $(D n$(SUBSCRIPT x)) represents the effective length of 215 value $(D x), which could be a single element (in which case $(D n$(SUBSCRIPT x)) is 216 $(D 1)), a _container, or a range. 217 218 $(BOOKTABLE Container primitives, 219 $(TR 220 $(TH Syntax) 221 $(TH $(BIGOH ·)) 222 $(TH Description) 223 ) 224 $(TR 225 $(TDNW $(D C(x))) 226 $(TDNW $(D n$(SUBSCRIPT x))) 227 $(TD Creates a _container of type $(D C) from either another _container or a range. 228 The created _container must not be a null reference even if x is empty.) 229 ) 230 $(TR 231 $(TDNW $(D c.dup)) 232 $(TDNW $(D n$(SUBSCRIPT c))) 233 $(TD Returns a duplicate of the _container.) 234 ) 235 $(TR 236 $(TDNW $(D c ~ x)) 237 $(TDNW $(D n$(SUBSCRIPT c) + n$(SUBSCRIPT x))) 238 $(TD Returns the concatenation of $(D c) and $(D r). $(D x) may be a single 239 element or an input range.) 240 ) 241 $(TR 242 $(TDNW $(D x ~ c)) 243 $(TDNW $(D n$(SUBSCRIPT c) + n$(SUBSCRIPT x))) 244 $(TD Returns the concatenation of $(D x) and $(D c). $(D x) may be a 245 single element or an input range type.) 246 ) 247 $(LEADINGROWN 3, Iteration 248 ) 249 $(TR 250 $(TD $(D c.Range)) 251 $(TD) 252 $(TD The primary range type associated with the _container.) 253 ) 254 $(TR 255 $(TD $(D c[])) 256 $(TDNW $(D log n$(SUBSCRIPT c))) 257 $(TD Returns a range 258 iterating over the entire _container, in a _container-defined order.) 259 ) 260 $(TR 261 $(TDNW $(D c[a .. b])) 262 $(TDNW $(D log n$(SUBSCRIPT c))) 263 $(TD Fetches a portion of the _container from key $(D a) to key $(D b).) 264 ) 265 $(LEADINGROWN 3, Capacity 266 ) 267 $(TR 268 $(TD $(D c.empty)) 269 $(TD $(D 1)) 270 $(TD Returns $(D true) if the _container has no elements, $(D false) otherwise.) 271 ) 272 $(TR 273 $(TD $(D c.length)) 274 $(TDNW $(D log n$(SUBSCRIPT c))) 275 $(TD Returns the number of elements in the _container.) 276 ) 277 $(TR 278 $(TDNW $(D c.length = n)) 279 $(TDNW $(D n$(SUBSCRIPT c) + n)) 280 $(TD Forces the number of elements in the _container to $(D n). 281 If the _container ends up growing, the added elements are initialized 282 in a _container-dependent manner (usually with $(D T.init)).) 283 ) 284 $(TR 285 $(TD $(D c.capacity)) 286 $(TDNW $(D log n$(SUBSCRIPT c))) 287 $(TD Returns the maximum number of elements that can be stored in the 288 _container without triggering a reallocation.) 289 ) 290 $(TR 291 $(TD $(D c.reserve(x))) 292 $(TD $(D n$(SUBSCRIPT c))) 293 $(TD Forces $(D capacity) to at least $(D x) without reducing it.) 294 ) 295 $(LEADINGROWN 3, Access 296 ) 297 $(TR 298 $(TDNW $(D c.front)) 299 $(TDNW $(D log n$(SUBSCRIPT c))) 300 $(TD Returns the first element of the _container, in a _container-defined order.) 301 ) 302 $(TR 303 $(TDNW $(D c.moveFront)) 304 $(TDNW $(D log n$(SUBSCRIPT c))) 305 $(TD Destructively reads and returns the first element of the 306 _container. The slot is not removed from the _container; it is left 307 initialized with $(D T.init). This routine need not be defined if $(D 308 front) returns a $(D ref).) 309 ) 310 $(TR 311 $(TDNW $(D c.front = v)) 312 $(TDNW $(D log n$(SUBSCRIPT c))) 313 $(TD Assigns $(D v) to the first element of the _container.) 314 ) 315 $(TR 316 $(TDNW $(D c.back)) 317 $(TDNW $(D log n$(SUBSCRIPT c))) 318 $(TD Returns the last element of the _container, in a _container-defined order.) 319 ) 320 $(TR 321 $(TDNW $(D c.moveBack)) 322 $(TDNW $(D log n$(SUBSCRIPT c))) 323 $(TD Destructively reads and returns the last element of the 324 _container. The slot is not removed from the _container; it is left 325 initialized with $(D T.init). This routine need not be defined if $(D 326 front) returns a $(D ref).) 327 ) 328 $(TR 329 $(TDNW $(D c.back = v)) 330 $(TDNW $(D log n$(SUBSCRIPT c))) 331 $(TD Assigns $(D v) to the last element of the _container.) 332 ) 333 $(TR 334 $(TDNW $(D c[x])) 335 $(TDNW $(D log n$(SUBSCRIPT c))) 336 $(TD Provides indexed access into the _container. The index type is 337 _container-defined. A _container may define several index types (and 338 consequently overloaded indexing).) 339 ) 340 $(TR 341 $(TDNW $(D c.moveAt(x))) 342 $(TDNW $(D log n$(SUBSCRIPT c))) 343 $(TD Destructively reads and returns the value at position $(D x). The slot 344 is not removed from the _container; it is left initialized with $(D 345 T.init).) 346 ) 347 $(TR 348 $(TDNW $(D c[x] = v)) 349 $(TDNW $(D log n$(SUBSCRIPT c))) 350 $(TD Sets element at specified index into the _container.) 351 ) 352 $(TR 353 $(TDNW $(D c[x] $(I op)= v)) 354 $(TDNW $(D log n$(SUBSCRIPT c))) 355 $(TD Performs read-modify-write operation at specified index into the 356 _container.) 357 ) 358 $(LEADINGROWN 3, Operations 359 ) 360 $(TR 361 $(TDNW $(D e in c)) 362 $(TDNW $(D log n$(SUBSCRIPT c))) 363 $(TD Returns nonzero if e is found in $(D c).) 364 ) 365 $(TR 366 $(TDNW $(D c.lowerBound(v))) 367 $(TDNW $(D log n$(SUBSCRIPT c))) 368 $(TD Returns a range of all elements strictly less than $(D v).) 369 ) 370 $(TR 371 $(TDNW $(D c.upperBound(v))) 372 $(TDNW $(D log n$(SUBSCRIPT c))) 373 $(TD Returns a range of all elements strictly greater than $(D v).) 374 ) 375 $(TR 376 $(TDNW $(D c.equalRange(v))) 377 $(TDNW $(D log n$(SUBSCRIPT c))) 378 $(TD Returns a range of all elements in $(D c) that are equal to $(D v).) 379 ) 380 $(LEADINGROWN 3, Modifiers 381 ) 382 $(TR 383 $(TDNW $(D c ~= x)) 384 $(TDNW $(D n$(SUBSCRIPT c) + n$(SUBSCRIPT x))) 385 $(TD Appends $(D x) to $(D c). $(D x) may be a single element or an input range type.) 386 ) 387 $(TR 388 $(TDNW $(D c.clear())) 389 $(TDNW $(D n$(SUBSCRIPT c))) 390 $(TD Removes all elements in $(D c).) 391 ) 392 $(TR 393 $(TDNW $(D c.insert(x))) 394 $(TDNW $(D n$(SUBSCRIPT x) * log n$(SUBSCRIPT c))) 395 $(TD Inserts $(D x) in $(D c) at a position (or positions) chosen by $(D c).) 396 ) 397 $(TR 398 $(TDNW $(D c.stableInsert(x))) 399 $(TDNW $(D n$(SUBSCRIPT x) * log n$(SUBSCRIPT c))) 400 $(TD Same as $(D c.insert(x)), but is guaranteed to not invalidate any ranges.) 401 ) 402 $(TR 403 $(TDNW $(D c.linearInsert(v))) 404 $(TDNW $(D n$(SUBSCRIPT c))) 405 $(TD Same as $(D c.insert(v)) but relaxes complexity to linear.) 406 ) 407 $(TR 408 $(TDNW $(D c.stableLinearInsert(v))) 409 $(TDNW $(D n$(SUBSCRIPT c))) 410 $(TD Same as $(D c.stableInsert(v)) but relaxes complexity to linear.) 411 ) 412 $(TR 413 $(TDNW $(D c.removeAny())) 414 $(TDNW $(D log n$(SUBSCRIPT c))) 415 $(TD Removes some element from $(D c) and returns it.) 416 ) 417 $(TR 418 $(TDNW $(D c.stableRemoveAny())) 419 $(TDNW $(D log n$(SUBSCRIPT c))) 420 $(TD Same as $(D c.removeAny()), but is guaranteed to not invalidate any 421 iterators.) 422 ) 423 $(TR 424 $(TDNW $(D c.insertFront(v))) 425 $(TDNW $(D log n$(SUBSCRIPT c))) 426 $(TD Inserts $(D v) at the front of $(D c).) 427 ) 428 $(TR 429 $(TDNW $(D c.stableInsertFront(v))) 430 $(TDNW $(D log n$(SUBSCRIPT c))) 431 $(TD Same as $(D c.insertFront(v)), but guarantees no ranges will be 432 invalidated.) 433 ) 434 $(TR 435 $(TDNW $(D c.insertBack(v))) 436 $(TDNW $(D log n$(SUBSCRIPT c))) 437 $(TD Inserts $(D v) at the back of $(D c).) 438 ) 439 $(TR 440 $(TDNW $(D c.stableInsertBack(v))) 441 $(TDNW $(D log n$(SUBSCRIPT c))) 442 $(TD Same as $(D c.insertBack(v)), but guarantees no ranges will be 443 invalidated.) 444 ) 445 $(TR 446 $(TDNW $(D c.removeFront())) 447 $(TDNW $(D log n$(SUBSCRIPT c))) 448 $(TD Removes the element at the front of $(D c).) 449 ) 450 $(TR 451 $(TDNW $(D c.stableRemoveFront())) 452 $(TDNW $(D log n$(SUBSCRIPT c))) 453 $(TD Same as $(D c.removeFront()), but guarantees no ranges will be 454 invalidated.) 455 ) 456 $(TR 457 $(TDNW $(D c.removeBack())) 458 $(TDNW $(D log n$(SUBSCRIPT c))) 459 $(TD Removes the value at the back of $(D c).) 460 ) 461 $(TR 462 $(TDNW $(D c.stableRemoveBack())) 463 $(TDNW $(D log n$(SUBSCRIPT c))) 464 $(TD Same as $(D c.removeBack()), but guarantees no ranges will be 465 invalidated.) 466 ) 467 $(TR 468 $(TDNW $(D c.remove(r))) 469 $(TDNW $(D n$(SUBSCRIPT r) * log n$(SUBSCRIPT c))) 470 $(TD Removes range $(D r) from $(D c).) 471 ) 472 $(TR 473 $(TDNW $(D c.stableRemove(r))) 474 $(TDNW $(D n$(SUBSCRIPT r) * log n$(SUBSCRIPT c))) 475 $(TD Same as $(D c.remove(r)), but guarantees iterators are not 476 invalidated.) 477 ) 478 $(TR 479 $(TDNW $(D c.linearRemove(r))) 480 $(TDNW $(D n$(SUBSCRIPT c))) 481 $(TD Removes range $(D r) from $(D c).) 482 ) 483 $(TR 484 $(TDNW $(D c.stableLinearRemove(r))) 485 $(TDNW $(D n$(SUBSCRIPT c))) 486 $(TD Same as $(D c.linearRemove(r)), but guarantees iterators are not 487 invalidated.) 488 ) 489 $(TR 490 $(TDNW $(D c.removeKey(k))) 491 $(TDNW $(D log n$(SUBSCRIPT c))) 492 $(TD Removes an element from $(D c) by using its key $(D k). 493 The key's type is defined by the _container.) 494 ) 495 $(TR 496 $(TDNW $(D )) 497 $(TDNW $(D )) 498 $(TD ) 499 ) 500 ) 501 502 Source: $(PHOBOSSRC std/_container/package.d) 503 504 Copyright: Red-black tree code copyright (C) 2008- by Steven Schveighoffer. Other code 505 copyright 2010- Andrei Alexandrescu. All rights reserved by the respective holders. 506 507 License: Distributed under the Boost Software License, Version 1.0. 508 (See accompanying file LICENSE_1_0.txt or copy at $(HTTP 509 boost.org/LICENSE_1_0.txt)). 510 511 Authors: Steven Schveighoffer, $(HTTP erdani.com, Andrei Alexandrescu) 512 */ 513 514 module std.container; 515 516 public import std.container.array; 517 public import std.container.binaryheap; 518 public import std.container.dlist; 519 public import std.container.rbtree; 520 public import std.container.slist; 521 522 import std.meta; 523 524 525 /* The following documentation and type $(D TotalContainer) are 526 intended for developers only. 527 528 $(D TotalContainer) is an unimplemented container that illustrates a 529 host of primitives that a container may define. It is to some extent 530 the bottom of the conceptual container hierarchy. A given container 531 most often will choose to only implement a subset of these primitives, 532 and define its own additional ones. Adhering to the standard primitive 533 names below allows generic code to work independently of containers. 534 535 Things to remember: any container must be a reference type, whether 536 implemented as a $(D class) or $(D struct). No primitive below 537 requires the container to escape addresses of elements, which means 538 that compliant containers can be defined to use reference counting or 539 other deterministic memory management techniques. 540 541 A container may choose to define additional specific operations. The 542 only requirement is that those operations bear different names than 543 the ones below, lest user code gets confused. 544 545 Complexity of operations should be interpreted as "at least as good 546 as". If an operation is required to have $(BIGOH n) complexity, it 547 could have anything lower than that, e.g. $(BIGOH log(n)). Unless 548 specified otherwise, $(D n) inside a $(BIGOH) expression stands for 549 the number of elements in the container. 550 */ 551 struct TotalContainer(T) 552 { 553 /** 554 If the container has a notion of key-value mapping, $(D KeyType) 555 defines the type of the key of the container. 556 */ 557 alias KeyType = T; 558 559 /** 560 If the container has a notion of multikey-value mapping, $(D 561 KeyTypes[k]), where $(D k) is a zero-based unsigned number, defines 562 the type of the $(D k)th key of the container. 563 564 A container may define both $(D KeyType) and $(D KeyTypes), e.g. in 565 the case it has the notion of primary/preferred key. 566 */ 567 alias KeyTypes = AliasSeq!T; 568 569 /** 570 If the container has a notion of key-value mapping, $(D ValueType) 571 defines the type of the value of the container. Typically, a map-style 572 container mapping values of type $(D K) to values of type $(D V) 573 defines $(D KeyType) to be $(D K) and $(D ValueType) to be $(D V). 574 */ 575 alias ValueType = T; 576 577 /** 578 Defines the container's primary range, which embodies one of the 579 ranges defined in $(MREF std,range). 580 581 Generally a container may define several types of ranges. 582 */ 583 struct Range 584 { 585 /++ 586 Range primitives. 587 +/ 588 @property bool empty() 589 { 590 assert(0); 591 } 592 /// Ditto 593 @property ref T front() //ref return optional 594 { 595 assert(0); 596 } 597 /// Ditto 598 @property void front(T value) //Only when front does not return by ref 599 { 600 assert(0); 601 } 602 /// Ditto 603 T moveFront() 604 { 605 assert(0); 606 } 607 /// Ditto 608 void popFront() 609 { 610 assert(0); 611 } 612 /// Ditto 613 @property ref T back() //ref return optional 614 { 615 assert(0); 616 } 617 /// Ditto 618 @property void back(T value) //Only when front does not return by ref 619 { 620 assert(0); 621 } 622 /// Ditto 623 T moveBack() 624 { 625 assert(0); 626 } 627 /// Ditto 628 void popBack() 629 { 630 assert(0); 631 } 632 /// Ditto 633 T opIndex(size_t i) //ref return optional 634 { 635 assert(0); 636 } 637 /// Ditto 638 void opIndexAssign(size_t i, T value) //Only when front does not return by ref 639 { 640 assert(0); 641 } 642 /// Ditto 643 T opIndexUnary(string op)(size_t i) //Only when front does not return by ref 644 { 645 assert(0); 646 } 647 /// Ditto 648 void opIndexOpAssign(string op)(size_t i, T value) //Only when front does not return by ref 649 { 650 assert(0); 651 } 652 /// Ditto 653 T moveAt(size_t i) 654 { 655 assert(0); 656 } 657 /// Ditto 658 @property size_t length() 659 { 660 assert(0); 661 } 662 } 663 664 /** 665 Property returning $(D true) if and only if the container has no 666 elements. 667 668 Complexity: $(BIGOH 1) 669 */ 670 @property bool empty() 671 { 672 assert(0); 673 } 674 675 /** 676 Returns a duplicate of the container. The elements themselves are not 677 transitively duplicated. 678 679 Complexity: $(BIGOH n). 680 */ 681 @property TotalContainer dup() 682 { 683 assert(0); 684 } 685 686 /** 687 Returns the number of elements in the container. 688 689 Complexity: $(BIGOH log(n)). 690 */ 691 @property size_t length() 692 { 693 assert(0); 694 } 695 696 /** 697 Returns the maximum number of elements the container can store without 698 (a) allocating memory, (b) invalidating iterators upon insertion. 699 700 Complexity: $(BIGOH log(n)). 701 */ 702 @property size_t capacity() 703 { 704 assert(0); 705 } 706 707 /** 708 Ensures sufficient capacity to accommodate $(D n) elements. 709 710 Postcondition: $(D capacity >= n) 711 712 Complexity: $(BIGOH log(e - capacity)) if $(D e > capacity), otherwise 713 $(BIGOH 1). 714 */ 715 void reserve(size_t e) 716 { 717 assert(0); 718 } 719 720 /** 721 Returns a range that iterates over all elements of the container, in a 722 container-defined order. The container should choose the most 723 convenient and fast method of iteration for $(D opSlice()). 724 725 Complexity: $(BIGOH log(n)) 726 */ 727 Range opSlice() 728 { 729 assert(0); 730 } 731 732 /** 733 Returns a range that iterates the container between two 734 specified positions. 735 736 Complexity: $(BIGOH log(n)) 737 */ 738 Range opSlice(size_t a, size_t b) 739 { 740 assert(0); 741 } 742 743 /** 744 Forward to $(D opSlice().front) and $(D opSlice().back), respectively. 745 746 Complexity: $(BIGOH log(n)) 747 */ 748 @property ref T front() //ref return optional 749 { 750 assert(0); 751 } 752 /// Ditto 753 @property void front(T value) //Only when front does not return by ref 754 { 755 assert(0); 756 } 757 /// Ditto 758 T moveFront() 759 { 760 assert(0); 761 } 762 /// Ditto 763 @property ref T back() //ref return optional 764 { 765 assert(0); 766 } 767 /// Ditto 768 @property void back(T value) //Only when front does not return by ref 769 { 770 assert(0); 771 } 772 /// Ditto 773 T moveBack() 774 { 775 assert(0); 776 } 777 778 /** 779 Indexing operators yield or modify the value at a specified index. 780 */ 781 ref T opIndex(KeyType) //ref return optional 782 { 783 assert(0); 784 } 785 /// ditto 786 void opIndexAssign(KeyType i, T value) //Only when front does not return by ref 787 { 788 assert(0); 789 } 790 /// ditto 791 T opIndexUnary(string op)(KeyType i) //Only when front does not return by ref 792 { 793 assert(0); 794 } 795 /// ditto 796 void opIndexOpAssign(string op)(KeyType i, T value) //Only when front does not return by ref 797 { 798 assert(0); 799 } 800 /// ditto 801 T moveAt(KeyType i) 802 { 803 assert(0); 804 } 805 806 /** 807 $(D k in container) returns true if the given key is in the container. 808 */ 809 bool opBinaryRight(string op)(KeyType k) if (op == "in") 810 { 811 assert(0); 812 } 813 814 /** 815 Returns a range of all elements containing $(D k) (could be empty or a 816 singleton range). 817 */ 818 Range equalRange(KeyType k) 819 { 820 assert(0); 821 } 822 823 /** 824 Returns a range of all elements with keys less than $(D k) (could be 825 empty or a singleton range). Only defined by containers that store 826 data sorted at all times. 827 */ 828 Range lowerBound(KeyType k) 829 { 830 assert(0); 831 } 832 833 /** 834 Returns a range of all elements with keys larger than $(D k) (could be 835 empty or a singleton range). Only defined by containers that store 836 data sorted at all times. 837 */ 838 Range upperBound(KeyType k) 839 { 840 assert(0); 841 } 842 843 /** 844 Returns a new container that's the concatenation of $(D this) and its 845 argument. $(D opBinaryRight) is only defined if $(D Stuff) does not 846 define $(D opBinary). 847 848 Complexity: $(BIGOH n + m), where m is the number of elements in $(D 849 stuff) 850 */ 851 TotalContainer opBinary(string op)(Stuff rhs) if (op == "~") 852 { 853 assert(0); 854 } 855 856 /// ditto 857 TotalContainer opBinaryRight(string op)(Stuff lhs) if (op == "~") 858 { 859 assert(0); 860 } 861 862 /** 863 Forwards to $(D insertAfter(this[], stuff)). 864 */ 865 void opOpAssign(string op)(Stuff stuff) if (op == "~") 866 { 867 assert(0); 868 } 869 870 /** 871 Removes all contents from the container. The container decides how $(D 872 capacity) is affected. 873 874 Postcondition: $(D empty) 875 876 Complexity: $(BIGOH n) 877 */ 878 void clear() 879 { 880 assert(0); 881 } 882 883 /** 884 Sets the number of elements in the container to $(D newSize). If $(D 885 newSize) is greater than $(D length), the added elements are added to 886 unspecified positions in the container and initialized with $(D 887 .init). 888 889 Complexity: $(BIGOH abs(n - newLength)) 890 891 Postcondition: $(D _length == newLength) 892 */ 893 @property void length(size_t newLength) 894 { 895 assert(0); 896 } 897 898 /** 899 Inserts $(D stuff) in an unspecified position in the 900 container. Implementations should choose whichever insertion means is 901 the most advantageous for the container, but document the exact 902 behavior. $(D stuff) can be a value convertible to the element type of 903 the container, or a range of values convertible to it. 904 905 The $(D stable) version guarantees that ranges iterating over the 906 container are never invalidated. Client code that counts on 907 non-invalidating insertion should use $(D stableInsert). Such code would 908 not compile against containers that don't support it. 909 910 Returns: The number of elements added. 911 912 Complexity: $(BIGOH m * log(n)), where $(D m) is the number of 913 elements in $(D stuff) 914 */ 915 size_t insert(Stuff)(Stuff stuff) 916 { 917 assert(0); 918 } 919 ///ditto 920 size_t stableInsert(Stuff)(Stuff stuff) 921 { 922 assert(0); 923 } 924 925 /** 926 Same as $(D insert(stuff)) and $(D stableInsert(stuff)) respectively, 927 but relax the complexity constraint to linear. 928 */ 929 size_t linearInsert(Stuff)(Stuff stuff) 930 { 931 assert(0); 932 } 933 ///ditto 934 size_t stableLinearInsert(Stuff)(Stuff stuff) 935 { 936 assert(0); 937 } 938 939 /** 940 Picks one value in an unspecified position in the container, removes 941 it from the container, and returns it. Implementations should pick the 942 value that's the most advantageous for the container. The stable version 943 behaves the same, but guarantees that ranges iterating over the container 944 are never invalidated. 945 946 Precondition: $(D !empty) 947 948 Returns: The element removed. 949 950 Complexity: $(BIGOH log(n)). 951 */ 952 T removeAny() 953 { 954 assert(0); 955 } 956 /// ditto 957 T stableRemoveAny() 958 { 959 assert(0); 960 } 961 962 /** 963 Inserts $(D value) to the front or back of the container. $(D stuff) 964 can be a value convertible to the container's element type or a range 965 of values convertible to it. The stable version behaves the same, but 966 guarantees that ranges iterating over the container are never 967 invalidated. 968 969 Returns: The number of elements inserted 970 971 Complexity: $(BIGOH log(n)). 972 */ 973 size_t insertFront(Stuff)(Stuff stuff) 974 { 975 assert(0); 976 } 977 /// ditto 978 size_t stableInsertFront(Stuff)(Stuff stuff) 979 { 980 assert(0); 981 } 982 /// ditto 983 size_t insertBack(Stuff)(Stuff stuff) 984 { 985 assert(0); 986 } 987 /// ditto 988 size_t stableInsertBack(T value) 989 { 990 assert(0); 991 } 992 993 /** 994 Removes the value at the front or back of the container. The stable 995 version behaves the same, but guarantees that ranges iterating over 996 the container are never invalidated. The optional parameter $(D 997 howMany) instructs removal of that many elements. If $(D howMany > n), 998 all elements are removed and no exception is thrown. 999 1000 Precondition: $(D !empty) 1001 1002 Complexity: $(BIGOH log(n)). 1003 */ 1004 void removeFront() 1005 { 1006 assert(0); 1007 } 1008 /// ditto 1009 void stableRemoveFront() 1010 { 1011 assert(0); 1012 } 1013 /// ditto 1014 void removeBack() 1015 { 1016 assert(0); 1017 } 1018 /// ditto 1019 void stableRemoveBack() 1020 { 1021 assert(0); 1022 } 1023 1024 /** 1025 Removes $(D howMany) values at the front or back of the 1026 container. Unlike the unparameterized versions above, these functions 1027 do not throw if they could not remove $(D howMany) elements. Instead, 1028 if $(D howMany > n), all elements are removed. The returned value is 1029 the effective number of elements removed. The stable version behaves 1030 the same, but guarantees that ranges iterating over the container are 1031 never invalidated. 1032 1033 Returns: The number of elements removed 1034 1035 Complexity: $(BIGOH howMany * log(n)). 1036 */ 1037 size_t removeFront(size_t howMany) 1038 { 1039 assert(0); 1040 } 1041 /// ditto 1042 size_t stableRemoveFront(size_t howMany) 1043 { 1044 assert(0); 1045 } 1046 /// ditto 1047 size_t removeBack(size_t howMany) 1048 { 1049 assert(0); 1050 } 1051 /// ditto 1052 size_t stableRemoveBack(size_t howMany) 1053 { 1054 assert(0); 1055 } 1056 1057 /** 1058 Removes all values corresponding to key $(D k). 1059 1060 Complexity: $(BIGOH m * log(n)), where $(D m) is the number of 1061 elements with the same key. 1062 1063 Returns: The number of elements removed. 1064 */ 1065 size_t removeKey(KeyType k) 1066 { 1067 assert(0); 1068 } 1069 1070 /** 1071 Inserts $(D stuff) before, after, or instead range $(D r), which must 1072 be a valid range previously extracted from this container. $(D stuff) 1073 can be a value convertible to the container's element type or a range 1074 of objects convertible to it. The stable version behaves the same, but 1075 guarantees that ranges iterating over the container are never 1076 invalidated. 1077 1078 Returns: The number of values inserted. 1079 1080 Complexity: $(BIGOH n + m), where $(D m) is the length of $(D stuff) 1081 */ 1082 size_t insertBefore(Stuff)(Range r, Stuff stuff) 1083 { 1084 assert(0); 1085 } 1086 /// ditto 1087 size_t stableInsertBefore(Stuff)(Range r, Stuff stuff) 1088 { 1089 assert(0); 1090 } 1091 /// ditto 1092 size_t insertAfter(Stuff)(Range r, Stuff stuff) 1093 { 1094 assert(0); 1095 } 1096 /// ditto 1097 size_t stableInsertAfter(Stuff)(Range r, Stuff stuff) 1098 { 1099 assert(0); 1100 } 1101 /// ditto 1102 size_t replace(Stuff)(Range r, Stuff stuff) 1103 { 1104 assert(0); 1105 } 1106 /// ditto 1107 size_t stableReplace(Stuff)(Range r, Stuff stuff) 1108 { 1109 assert(0); 1110 } 1111 1112 /** 1113 Removes all elements belonging to $(D r), which must be a range 1114 obtained originally from this container. The stable version behaves the 1115 same, but guarantees that ranges iterating over the container are 1116 never invalidated. 1117 1118 Returns: A range spanning the remaining elements in the container that 1119 initially were right after $(D r). 1120 1121 Complexity: $(BIGOH m * log(n)), where $(D m) is the number of 1122 elements in $(D r) 1123 */ 1124 Range remove(Range r) 1125 { 1126 assert(0); 1127 } 1128 /// ditto 1129 Range stableRemove(Range r) 1130 { 1131 assert(0); 1132 } 1133 1134 /** 1135 Same as $(D remove) above, but has complexity relaxed to linear. 1136 1137 Returns: A range spanning the remaining elements in the container that 1138 initially were right after $(D r). 1139 1140 Complexity: $(BIGOH n) 1141 */ 1142 Range linearRemove(Range r) 1143 { 1144 assert(0); 1145 } 1146 /// ditto 1147 Range stableLinearRemove(Range r) 1148 { 1149 assert(0); 1150 } 1151 } 1152 1153 @safe unittest 1154 { 1155 TotalContainer!int test; 1156 } 1157