Abstract
The basic operations of a dynamic array are operator[ ], push_back, and pop_back. This study is an examination of variations of dynamic arrays that support these operations at O(1) worst-case cost. In the literature, many solutions have been proposed, but little information is available on their mutual superiority. Most library implementations only guarantee O(1) amortized cost per operation. Four variations with good worst-case performance were benchmarked: (1) resizable array relying on doubling, halving, and incremental copying; (2) level-wise-allocated pile; (3) sliced array with fixed-capacity slices; and (4) block-wise-allocated pile. Let \(|{\small {\mathcal {V}}}|\) denote the size of the values of type \({\small {\mathcal {V}}}\) and \(|{\small {\mathcal {V}}}{\small {\texttt {*}}}|\) the size of the pointers to values of type \({\small {\mathcal {V}}}\), both measured in bytes. For an array of n values and a slice of S values, the space requirements of the considered variations were at most \(12 |{\small {\mathcal {V}}}| n + O(|{\small {\mathcal {V}}}{\small {\texttt {*}}}|)\), \(2 |{\small {\mathcal {V}}}| n + O(|{\small {\mathcal {V}}}{\small {\texttt {*}}}|\lg n)\), \(|{\small {\mathcal {V}}}| (n + S) + O(|{\small {\mathcal {V}}}{}{\small {\texttt {*}}}|n{\slash }S)\), and \(|{\small {\mathcal {V}}}| n + O((|{\small {\mathcal {V}}}| + |{\small {\mathcal {V}}}{\small {\texttt {*}}}| +|{\small {\mathcal {V}}}{\small {\texttt {**}}}|)\sqrt{n})\) bytes, respectively. A sliced array that uses a few per cent of extra space turned out to be a reasonable solution in practice. In general, for worst-case-efficient variations, the operations were measurably slower than those for the C++ standard-library implementation. Moreover, slicing can make the structures fragile, so measures to make them more robust are proposed.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Similar content being viewed by others
References
Austern, M.: Defining iterators and const iterators. C/C++ User’s J. 19(1), 74–79 (2001)
Bentley, J.: Programming Pearls, 2nd edn. Addison Wesley Longman Inc., Reading (2000)
Brodnik, A., Carlsson, S., Demaine, E.D., Munro, J.I., Sedgewick, R.D.: Resizable arrays in optimal time and space. In: Dehne, F., Gupta, A., Sack, J.-R., Tamassia, R. (eds.) WADS 1999. LNCS, vol. 1663, pp. 37–48. Springer, Heidelberg (1999)
The C++ Standards Committee: Standard for Programming Language C++. Working Draft N4296, ISO/IEC (2014)
Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C.: Introduction to Algorithms, 3rd edn. The MIT Press, Cambridge (2009)
The CPH STL: Department of Computer Science, University of Copenhagen (2000–2016). http://cphstl.dk/
Goodrich, M.T., Kloss II, J.G.: Tiered vectors: efficient dynamic arrays for rank-based sequences. In: Dehne, F., Gupta, A., Sack, J.-R., Tamassia, R. (eds.) WADS 1999. LNCS, vol. 1663, pp. 205–216. Springer, Heidelberg (1999)
Katajainen, J., Maniotis, A.M.: Conceptual frameworks for constructing iterators for compound data structures–electronic appendix I: component-iterator and rank-iterator classes. CPH STL Report 2012–3, Department of Computer Science, University of Copenhagen, Copenhagen (2012)
Katajainen, J., Mortensen, B.B.: Experiences with the design and implementation of space-efficient deques. In: Brodal, G.S., Frigioni, D., Marchetti-Spaccamela, A. (eds.) WAE 2001. LNCS, vol. 2141, pp. 39–50. Springer, Heidelberg (2001)
Katajainen, J., Mortensen, B.B.: Experiences with the design and implementation of space-efficient deques. CPH STL Report 2001–7, Department of Computer Science, University of Copenhagen, Copenhagen (2001)
Katajainen, J., Simonsen, B.: Adaptable component frameworks: using vector from the C++ standard library as an example. In: Jansson, P., Schupp, S. (eds.) 2009 ACM SIGPLAN Workshop on Generic Programming, pp. 13–24. ACM, New York (2009)
Katajainen, J., Simonsen, B.: Vector framework: electronic appendix. CPH STL Report 2009–4, Department of Computer Science, University of Copenhagen, Copenhagen (2009)
Kernighan, B.W., Ritchie, D.M.: The C Programming Language, 2nd edn. Prentice Hall PTR, Englewood Cliffs (1988)
Kristensen, M.D.: Vector implementation for the CPH STL. CPH STL Report 2004–2, Department of Computer Science, University of Copenhagen, Copenhagen (2004)
Mortensen, B.B.: The deque class in the Copenhagen STL: first attempt. CPH STL Report 2001–4, Department of Computer Science, University of Copenhagen, Copenhagen (2001)
Musser, D.R.: Introspective sorting and selection algorithms. Software Pract. Exper. 27(8), 983–993 (1997)
Neidhardt, M., Simonsen, B.: Extending the CPH STL with LEDA APIs. CPH STL Report 2009–8, Department of Computer Science, University of Copenhagen, Copenhagen (2009)
Plauger, P.J., Stepanov, A.A., Lee, M., Musser, D.R.: The C++ Standard Template Library. Prentice Hall PTR, Upper Saddle River (2001)
Raman, R., Raman, V., Rao, S.S.: Succinct dynamic data structures. In: Dehne, F., Sack, J.-R., Tamassia, R. (eds.) WADS 2001. LNCS, vol. 2125, pp. 426–437. Springer, Heidelberg (2001)
Sitarski, E.: Algorithm alley: HATs: hashed array trees: fast variable-length arrays. Dr. Dobb’s J. 21(11) (1996). http://www.drdobbs.com/database/algorithm-alley/184409965
Stepanov, A.A., Rose, D.E.: From Mathematics to Generic Programming. Pearson Education Inc., Upper Saddle River (2015)
Stroustrup, B.: The C++ Programming Language, 4th edn. Pearson Education Inc., Upper Saddle River (2013)
Vandervoorde, D., Josuttis, N.M.: C++ Templates: The Complete Guide. Pearson Education Inc., Boston (2003)
Williams, J.W.J.: Algorithm 232: heapsort. Commun. ACM 7(6), 347–348 (1964)
Acknowledgements
This work builds on the work of many students who implemented the prototypes of the programs discussed in this paper. From the version-control system of the CPH STL, I could extract the following names—I thank them all: Tina A. G. Andersen, Filip Bruman, Marc Framvig-Antonsen, Ulrik Schou Jörgensen, Mads D. Kristensen [14], Daniel P. Larsen, Andreas Milton Maniotis [8], Bjarke Buur Mortensen [9, 10, 15], Michael Neidhardt [17], Jan Presz, Wojciech Sikora-Kobylinski, Bo Simonsen [11, 12, 17], Jens Peter Svensson, Mikkel Thomsen, Claus Ullerlund, Bue Vedel-Larsen, and Christian Wolfgang.
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2016 Springer International Publishing Switzerland
About this paper
Cite this paper
Katajainen, J. (2016). Worst-Case-Efficient Dynamic Arrays in Practice. In: Goldberg, A., Kulikov, A. (eds) Experimental Algorithms. SEA 2016. Lecture Notes in Computer Science(), vol 9685. Springer, Cham. https://doi.org/10.1007/978-3-319-38851-9_12
Download citation
DOI: https://doi.org/10.1007/978-3-319-38851-9_12
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-319-38850-2
Online ISBN: 978-3-319-38851-9
eBook Packages: Computer ScienceComputer Science (R0)