NCBI C++ ToolKit
text_joiner.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef UTIL___TEXT_JOINER__HPP
2 #define UTIL___TEXT_JOINER__HPP
3 
4 /* $Id: text_joiner.hpp 77707 2017-05-03 12:47:35Z ivanov $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Aaron Ucko
30  *
31  */
32 
33 /// @file text_joiner.hpp
34 /// Template for collecting and joining strings with a minimum of heap churn.
35 
36 #include <corelib/ncbistd.hpp>
37 #include <util/error_codes.hpp>
38 
39 
40 /** @addtogroup String
41  *
42  * @{
43  */
44 
45 
47 
48 /// CTextJoiner<> -- template for efficiently collecting and joining strings.
49 ///
50 /// @param num_prealloc
51 /// How many strings to store locally (without turning to the heap).
52 /// @param TIn
53 /// Input string type, std::string by default. CTempString is an option
54 /// too, but users will need to ensure that its data remains valid through
55 /// any calls to Join().
56 /// @param TOut
57 /// Output string type, normally deduced from TIn.
58 
59 template <size_t num_prealloc, typename TIn = string,
60  typename TOut = basic_string<typename TIn::value_type> >
62 {
63 public:
65 
66  CTextJoiner& Add (const TIn& s);
67  void Join(TOut* result) const;
68 
69 private:
70  TIn m_MainStorage[num_prealloc];
71  unique_ptr<vector<TIn> > m_ExtraStorage;
73 };
74 
75 
76 template<size_t num_prealloc, typename TIn, typename TOut>
77 inline
80 {
81  if (s.empty()) {
82  return *this;
83  }
84 
85  if (m_MainStorageUsage < num_prealloc) {
86  m_MainStorage[m_MainStorageUsage++] = s;
87  } else if (m_ExtraStorage.get() != NULL) {
88  ERR_POST_XX_ONCE(Util_TextJoiner, 1,
89  Warning << "exceeding anticipated count "
90  << num_prealloc);
91  m_ExtraStorage->push_back(s);
92  } else {
93  m_ExtraStorage.reset(new vector<TIn>(1, s));
94  }
95 
96  return *this;
97 }
98 
99 
100 template<size_t num_prealloc, typename TIn, typename TOut>
101 inline
103 {
104  SIZE_TYPE size_needed = 0;
105  for (size_t i = 0; i < m_MainStorageUsage; ++i) {
106  size_needed += m_MainStorage[i].size();
107  }
108  if (m_ExtraStorage.get() != NULL) {
109  ITERATE (typename vector<TIn>, it, *m_ExtraStorage) {
110  size_needed += it->size();
111  }
112  }
113 
114  result->clear();
115  result->reserve(size_needed);
116  for (size_t i = 0; i < m_MainStorageUsage; ++i) {
117  result->append(m_MainStorage[i].data(), m_MainStorage[i].size());
118  }
119  if (m_ExtraStorage.get() != NULL) {
120  ITERATE (typename vector<TIn>, it, *m_ExtraStorage) {
121  result->append(it->data(), it->size());
122  }
123  }
124 }
125 
126 
128 
129 
130 /* @} */
131 
132 #endif /* UTIL___TEXT_JOINER__HPP */
CTextJoiner<> – template for efficiently collecting and joining strings.
Definition: text_joiner.hpp:62
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
string
Definition: cgiapp.hpp:687
#define NULL
Definition: ncbistd.hpp:225
#define ERR_POST_XX_ONCE(error_name, err_subcode, message)
Error posting only once during program execution with given error code name and given error subcode.
Definition: ncbidiag.hpp:643
void Warning(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1191
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
NCBI_NS_STD::string::size_type SIZE_TYPE
Definition: ncbistr.hpp:132
CTextJoiner & Add(const TIn &s)
Definition: text_joiner.hpp:79
size_t m_MainStorageUsage
Definition: text_joiner.hpp:72
void Join(TOut *result) const
TIn m_MainStorage[num_prealloc]
Definition: text_joiner.hpp:70
unique_ptr< vector< TIn > > m_ExtraStorage
Definition: text_joiner.hpp:71
Definition of all error codes used in util (xutil.lib).
int i
const struct ncbi::grid::netcache::search::fields::SIZE size
else result
Definition: token2.c:20
Modified on Thu Nov 30 04:56:14 2023 by modify_doxy.py rev. 669887