Stroika Library 3.0d18
 
Loading...
Searching...
No Matches
ArraySupport.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4
5#ifndef _Stroika_Foundation_Containers_Private_ArraySupport_h_
6#define _Stroika_Foundation_Containers_Private_ArraySupport_h_
7
8#include "Stroika/Foundation/StroikaPreComp.h"
9
10#include "Stroika/Foundation/Common/Common.h"
11
12#include "Stroika/Foundation/Containers/Common.h"
13#include "Stroika/Foundation/Containers/DataStructures/Array.h"
14
15/**
16 * \file
17 * support classes for Concrete classes 'extensions' of behavior specific to the Array data structure
18 */
19
21
22 /**
23 * \brief ArrayBasedContainer is a Stroika implementation detail, but its public methods are fair game and fully supported (as used in subclasses)
24 *
25 * This mechanism allows all the array based concrete containers (such as Set_Array, Sequence_Array) to all
26 * share the same API and implementation of the API access functions (shrink_to_fit, reserve etc) but without
27 * any genericity implied in the API (just code sharing).
28 *
29 * \note bool USING_IREP:
30 * Can we just peek from ArrayBasedContainer<> into the fData_ and do the array operations? Yes, if Rep_ is not
31 * type-erased, but no otherwise. If we specify false, we can avoid putting references to the ArrayBasedContainerIRep
32 * into the vtable, and linking a bunch of often not used code. But if we've got extra template parameters to the Rep_,
33 * we cannot peek at it from this container, so must indirect.
34 *
35 * \par Example Usage
36 * \code
37 * class Association_Array : public Association_Array<KEY_TYPE, MAPPED_VALUE_TYPE> {
38 * using inherited = Association_Array<KEY_TYPE, MAPPED_VALUE_TYPE>;
39 * // BECOMES
40 * class Association_Array : public Private::ArrayBasedContainer<Association_Array<KEY_TYPE, MAPPED_VALUE_TYPE>, Association<KEY_TYPE, MAPPED_VALUE_TYPE>, true> {
41 * using inherited = Private::ArrayBasedContainer<Association_Array<KEY_TYPE, MAPPED_VALUE_TYPE>, Association<KEY_TYPE, MAPPED_VALUE_TYPE>, true>;
42 *
43 * // AND need in main public container,
44 * friend inherited; // for ArrayBasedContainer
45 * \endcode
46 */
47 template <typename THIS_CONTAINER, typename BASE_CONTAINER, bool USING_IREP>
48 class ArrayBasedContainer : public BASE_CONTAINER {
49 public:
50 /**
51 */
52 using BASE_CONTAINER::BASE_CONTAINER;
53
54 public:
55 /*
56 * \brief Return the number of allocated vector/array elements.
57 *
58 * This optional API allows pre-reserving space as an optimization.
59 *
60 * @aliases GetCapacity ();
61 */
62 nonvirtual size_t capacity () const;
63
64 public:
65 /**
66 * This optional API allows pre-reserving space as an optimization.
67 *
68 * @aliases SetCapacity ();
69 *
70 * \note Note that this does not affect the semantics of the container.
71 *
72 * \pre slotsAllocated >= size ()
73 */
74 nonvirtual void reserve (size_t slotsAlloced);
75
76 public:
77 /**
78 * \brief Reduce the space used to store the container contents.
79 *
80 * This has no semantics, no observable behavior. But depending on the representation of
81 * the concrete container, calling this may save memory.
82 */
83 nonvirtual void shrink_to_fit ();
84 };
85
86 /**
87 * \brief impl detail for array based container support (see ArrayBasedContainer docs on bool USING_IREP)
88 *
89 * \par Example Usage
90 * \code
91 * //using IImplRepBase_ = typename Association<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep; // BECOMES
92 * using IImplRepBase_ = Containers::Private::ArrayBasedContainerIRep< typename Association<KEY_TYPE, MAPPED_VALUE_TYPE>::_IRep >;
93 * \endcode
94 */
95 template <typename CONTAINER_REP_BASE_CLASS>
96 class ArrayBasedContainerIRep : public CONTAINER_REP_BASE_CLASS {
97 public:
98 virtual void shrink_to_fit () = 0;
99 virtual size_t capacity () const = 0;
100 virtual void reserve (size_t slotsAlloced) = 0;
101 };
102
103 /**
104 * \brief CRTP applied when ArrayBasedContainerIRep used
105 *
106 * \par Example Usage
107 * \code
108 * template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (IEqualsComparer<KEY_TYPE>) KEY_EQUALS_COMPARER>
109 * class Association_Array<KEY_TYPE, MAPPED_VALUE_TYPE>::Rep_
110 * : public Rep_<KEY_EQUALS_COMPARER>, IImplRepBase_,
111 * public Memory::UseBlockAllocationIfAppropriate<Rep_<KEY_EQUALS_COMPARER>> {
112 * using inherited = Rep_<KEY_EQUALS_COMPARER>;
113 * // BECOMES
114 * template <qCompilerAndStdLib_ConstraintDiffersInTemplateRedeclaration_BWA (IEqualsComparer<KEY_TYPE>) KEY_EQUALS_COMPARER>
115 * class Association_Array<KEY_TYPE, MAPPED_VALUE_TYPE>::Rep_
116 * : public Private::ArrayBasedContainerRepImpl<Rep_<KEY_EQUALS_COMPARER>, IImplRepBase_>,
117 * public Memory::UseBlockAllocationIfAppropriate<Rep_<KEY_EQUALS_COMPARER>> {
118 * using inherited = Private::ArrayBasedContainerRepImpl<Rep_<KEY_EQUALS_COMPARER>, IImplRepBase_>;
119 *
120 * // AND need in Rep_,
121 * friend inherited; // for ArrayBasedContainerRepImpl
122 * \endcode
123 */
124 template <typename THIS_CONTAINER_REP, typename BASE_CONTAINER_REP>
125 class ArrayBasedContainerRepImpl : public BASE_CONTAINER_REP {
126 public:
127 /**
128 */
129 using BASE_CONTAINER_REP::BASE_CONTAINER_REP;
130 virtual void shrink_to_fit () override;
131 virtual size_t capacity () const override;
132 virtual void reserve (size_t slotsAlloced) override;
133 };
134
135}
136
137/*
138 ********************************************************************************
139 ***************************** Implementation Details ***************************
140 ********************************************************************************
141 */
142#include "ArraySupport.inl"
143
144#endif /*_Stroika_Foundation_Containers_Private_ArraySupport_h_ */
ArrayBasedContainer is a Stroika implementation detail, but its public methods are fair game and full...
nonvirtual void shrink_to_fit()
Reduce the space used to store the container contents.
impl detail for array based container support (see ArrayBasedContainer docs on bool USING_IREP)
CRTP applied when ArrayBasedContainerIRep used.