c++

Streaming Percentiles 3.1.0 Released
Analytics c++ ckms gk javascript percentiles streaming
Published: 2019-03-29
Streaming Percentiles 3.1.0 Released
Version 3.1.0 of the streaming percentiles library has been released. The streaming percentiles library is a cross-platform, multi-language (C++ and JavaScript) implementation of a number of online (single-pass) percentile algorithms. This version of the streaming percentiles library adds support for copy construction, assignment, move construction, and move assignment on all analytics classes. This change allows you to put streaming analytics classes into STL containers, such as: 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <vector>#include <stmpct/gk. Read more...
Calculating Percentiles on Streaming Data Part 8: Parameterizing Algorithms on Measurement Type
Calculating Percentiles on Streaming Data c++ ckms gk javascript percentiles streaming
Published: 2018-12-21
Calculating Percentiles on Streaming Data Part 8: Parameterizing Algorithms on Measurement Type
This is part 8/8 of my Calculating Percentiles on Streaming Data series. As mentioned in part 6 of this series, I have published a C++ and JavaScript library which implements a number of streaming percentile algorithms on GitHub at https://github.com/sengelha/streaming-percentiles. Versions 1.x and 2.x of the C++ library required all measurements to use the type double, and usage of the algorithms looked something like this: 1 2 3 4 5 6 7 8 #include <stmpct/gk. Read more...
Calculating Percentiles on Streaming Data Part 7: Cormode-Korn-Muthukrishnan-Srivastava
Calculating Percentiles on Streaming Data c++ ckms gk javascript percentiles streaming
Published: 2018-03-29
Calculating Percentiles on Streaming Data Part 7: Cormode-Korn-Muthukrishnan-Srivastava
This is part 7/8 of my Calculating Percentiles on Streaming Data series. In 2005, Graham Cormode, Flip Korn, S. Muthukrishnan, and Divesh Srivastava published a paper called Effective Computation of Biased Quantiles over Data Streams [CKMS05]. This paper took the Greenwald-Khanna algorithm [GK01] and made the following notable changes: Generalized the algorithm to work with arbitrary targeted quantiles, where a targeted quantile is a combination of a quantile $\phi$ and a maximum allowable error $\epsilon$. Read more...
Escaping Strings in XPath 1.0
XML / XPath / XSLT c++ xpath
Published: 2008-06-03
Escaping Strings in XPath 1.0
XPath is a language for selecting nodes from an XML document. XPath is used extensively in XSLT and other XML technologies. I also vastly prefer using XPath (e.g. with XPathNavigator) over the XML DOM when manipulating XML in a non-streaming fashion. In XPath, strings must be delimited by either single or double quotes. Given a quote character used to delimit a string, one can’t represent that same quote character within the string. Read more...
STL Objects and Win32 Module Boundaries
C++ c++ stl win32
Published: 2008-01-04
STL Objects and Win32 Module Boundaries
Let’s say you have the following function: 1 2 3 4 void AppendChar(std::string& s, char ch) { s += ch; } What happens if this function is exported as an ordinal function from a DLL (not an inlined piece of code inside a header) and you call it from an EXE? It works most of the time. When it doesn’t, it corrupts your heap and causes a spectacular mess. Read more...
Converting C++ Member Functions into Function Objects
C++ c++ stl
Published: 2007-08-28
Converting C++ Member Functions into Function Objects
Let’s say you have a C++ function that takes a function object as a parameter and calls it: 1 2 3 4 5 template <typename _Fn> void call_functor(_Fn fn) { fn(); } Now let’s say you want to pass a class’s member function to call_functor() above, as in: 1 2 3 4 5 6 7 class C { void foo() { std::cout << "foo()\n"; } }; C c; call_functor(/\* What do I put here? Read more...
STL Map Use
C++ c++ stl
Published: 2007-01-25
STL Map Use
What’s wrong with the following code? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 template<typename T1, typename T2> struct my_pair { typedef T1 first_type; typedef T2 second_type; my_pair() : first(T1()), second(T2()) {} my_pair(const T1& v1, const T2& v2) : first(v1), second(v2) {} T1 first; T2 second; }; template<typename T1, typename T2> inline bool operator< ( const my_pair<T1, T2>& x, const my_pair<T1, T2>& y ) { return (x. Read more...
STL Vector Use
C++ c++ stl
Published: 2007-01-23
STL Vector Use
I recently wrote a piece of code that looked something like the following: 1 2 3 4 5 6 7 8 9 10 11 static const int NUM_TOTAL_VALUES = ...; typedef ... T; // Create vec and reserve NUM_TOTAL_VALUES spaces for later insertion std::vector<T> vec(NUM_TOTAL_VALUES); // Insert values into vec for (int i = 0; i != NUM_TOTAL_VALUES; ++i) vec.push_back(...); // vec should now have NUM_TOTAL_VALUES values in it (but doesn't! Read more...
Managed Wrappers and Hidden Interdependencies
C++ c++ managed c++
Published: 2007-01-10
Managed Wrappers and Hidden Interdependencies
Let’s say you have the following unmanaged code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #pragma unmanaged class Stream { ... }; // Conceptual stream class class StreamWriter { public: StreamWriter(Stream* pStream) : m_pStream(pStream) {} ~StreamWriter() { /* Use m_pStream in some way */ } ... private: Stream* m_pStream; }; void f() { Stream stream; StreamWriter streamWriter(&stream); // Use streamWriter // streamWriter is destroyed // stream is destroyed } Note that StreamWriter’s destructor uses m_pStream (perhaps by flushing the stream). Read more...
C++ Exception Handling
C++ c++
Published: 2006-01-30
C++ Exception Handling
Quick quiz: What is the behavior of the following code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #include <stdio.h> int main(int argc, char* argv[]) { try { *((char*) 0) = 1; } catch (...) { printf("Caught exception.\n"); } return 0; } Surprisingly, it depends! As I would expect, on many operating system / compiler combinations (such as gcc version 3. Read more...
Implementing IXmlWriter Part 14: Supporting Writing To A Stream
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2006-01-10
Implementing IXmlWriter Part 14: Supporting Writing To A Stream
This is part 14/14 of my Implementing IXmlWriter post series. Today I will add support for writing the generated XML to a C++ stream to last time’s IXmlWriter. Finally the reason why I’ve insisted on calling this series IXmlWriter (instead of StringXmlWriter) should become clear: I’ve been planning on supporting writing the generated XML to more than just a string. Specifically, today I will add the ability to write the XML to a C++ ostream object, a base class in the C++ iostream library which defines a writable stream. Read more...
Implementing IXmlWriter Part 13: Putting IXmlWriter Behind A Pimpl Firewall
Implementing IXmlWriter c++ ixmlwriter pimpl xml
Published: 2005-12-15
Implementing IXmlWriter Part 13: Putting IXmlWriter Behind A Pimpl Firewall
This is part 13/14 of my Implementing IXmlWriter post series. As the private members of IXmlWriter are getting too numerous and too likely to change by my judgment, today I will put last time’s IXmlWriter behind a compilation firewall (pimpl). The idea behind the pimpl idiom is to hide as much of the class definition as possible in order to avoid requiring users of the class to recompile if the class’s private members are changed. Read more...
Implementing IXmlWriter Part 12: Supporting Pretty-Printing
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-12-13
Implementing IXmlWriter Part 12: Supporting Pretty-Printing
This is part 12/14 of my Implementing IXmlWriter post series. Today I will add support for pretty-printing to last time’s IXmlWriter. Pretty-printing is the addition of whitespace at predetermined locations to make the resulting XML easier to read than when it is all on one line. In the .NET Framework’s System.Xml.XmlTextWriter class, it is supported by the properties Formatting, which allows you to enable or disable pretty-printing; Indentation, which allows you to specify how many whitespace characters indentation should use; and IndentChar, which allows you to specify the whitespace character to use for indentation. Read more...
Implementing IXmlWriter Part 11: Supporting Namespaces
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-12-07
Implementing IXmlWriter Part 11: Supporting Namespaces
This is part 11/14 of my Implementing IXmlWriter post series. Today I will add support for namespaces to last time’s IXmlWriter. Namespaces are defined by the W3C recommendation Namespaces in XML. Using namespaces requires two parts: a namespace declaration, which associates a prefix with a namespace name (a user-defined, ideally globally-unique string which defines the namespace, often in the form of a URL); and the assignment of XML elements and attributes to this namespace by using the aforementioned prefix. Read more...
Implementing IXmlWriter Part 10: Supporting WriteComment()
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-12-02
Implementing IXmlWriter Part 10: Supporting WriteComment()
This is part 10/14 of my Implementing IXmlWriter post series. Today I will add support for the function WriteComment() to last time’s IXmlWriter. Quoting from Section 2.5: Comments of the XML 1.0 spec: Comments MAY appear anywhere in a document outside other markup; in addition, they MAY appear within the document type declaration at places allowed by the grammar. Considering this, we should allow writing comments in virtually every WriteState that the IXmlWriter can be in. Read more...
Implementing IXmlWriter Part 9: Supporting WriteStartDocument() and WriteEndDocument()
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-11-22
Implementing IXmlWriter Part 9: Supporting WriteStartDocument() and WriteEndDocument()
This is part 9/14 of my Implementing IXmlWriter post series. Today I will add support for the functions WriteStartDocument() and WriteEndDocument() to last time’s IXmlWriter. WriteStartDocument() writes the XML declaration (i.e. <?xml version="1.0"?>) and WriteEndDocument() closes all open attributes and elements and sets the IXmlWriter back in the initial state. Adding support for these functions is straightforward. Note that I have introduced a new IXmlWriter state called WriteState_Prolog; this will be important later. Read more...
Implementing IXmlWriter Part 8: Supporting WriteStartAttribute() and WriteEndAttribute()
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-11-18
Implementing IXmlWriter Part 8: Supporting WriteStartAttribute() and WriteEndAttribute()
This is part 8/14 of my Implementing IXmlWriter post series. Today I will add support for the functions WriteStartAttribute() and WriteEndAttribute() to last time’s IXmlWriter. These functions are (obviously) used to denote the start and end of an attribute; the attribute value is written using WriteString() (this usage is analogous to WriteStartElement() and WriteEndElement()). Because WriteString() must now be aware of whether it is writing an attribute value or element content, I must keep track of the state the IXmlWriter is in — a change that affects nearly every function. Read more...
Implementing IXmlWriter Part 7: Cleaning Up
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-11-17
Implementing IXmlWriter Part 7: Cleaning Up
This is part 7/14 of my Implementing IXmlWriter post series. Wow, I can’t believe that it’s been over a month already since my last IXmlWriter post. I guess my vacation ruined my exercise plan and my blogging habits. It’s well past time to get back into both. Rather than introduce a new test case, I’m going to spend today “cleaning up” the previous version of IXmlWriter. The first cleanup method is trivial but overdue — I will separate the implementation of IXmlWriter from its interface, as a user of IXmlWriter shouldn’t be particularly concerned about its implementation. Read more...
Implementing IXmlWriter Part 6: Escaping Attribute Content
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-10-12
Implementing IXmlWriter Part 6: Escaping Attribute Content
This is part 6/14 of my Implementing IXmlWriter post series. Last time’s IXmlWriter has a serious bug: it doesn’t properly handle attribute value escaping and can lead to malformed XML. Consider the following test case: 1 2 3 4 5 6 7 8 StringXmlWriter xmlWriter; xmlWriter.WriteStartElement("root"); xmlWriter.WriteStartElement("element"); xmlWriter.WriteAttributeString("att", "\""); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); std::string strXML = xmlWriter.GetXmlString(); The previous version of IXmlWriter will generate the XML string <root><element att="""/></root>, which is invalid and will be rejected by a XML parser. Read more...
Implementing IXmlWriter Part 5: Supporting WriteAttributeString()
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-10-11
Implementing IXmlWriter Part 5: Supporting WriteAttributeString()
This is part 5/14 of my Implementing IXmlWriter post series. Today I will add support for writing attributes to yesterday’s version of IXmlWriter. To learn more about attributes, see the W3C XML 1.0 Recommendation. Writing attributes will be supported with the function WriteAttributeString(). Here’s today’s test case: 1 2 3 4 5 6 7 8 9 StringXmlWriter xmlWriter; xmlWriter.WriteStartElement("root"); xmlWriter.WriteStartElement("element"); xmlWriter.WriteAttributeString("att", "value"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); std::string strXML = xmlWriter.GetXmlString(); // strXML should be <root><element att="value"/></root> Because the changes in Implementing IXmlWriter Part 4 keep start elements unclosed until another function is called which requires them to be closed (e. Read more...
Implementing IXmlWriter Part 4: Collapsing Empty Elements
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-10-10
Implementing IXmlWriter Part 4: Collapsing Empty Elements
This is part 4/14 of my Implementing IXmlWriter post series. One of the enhancements that XML introduced over SGML was a shorthand for specifying an element with no content by adding a trailing slash at the end of an open element. For example, <br/> is equivalent to <br></br>. Let’s add this functionality to the previous version of IXmlWriter. Here’s the test case: 1 2 3 4 5 6 7 8 StringXmlWriter xmlWriter; xmlWriter. Read more...
Implementing IXmlWriter Part 3: Supporting WriteElementString()
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-10-07
Implementing IXmlWriter Part 3: Supporting WriteElementString()
This is part 3/14 of my Implementing IXmlWriter post series. Today’s addition to the previous iteration of IXmlWriter is quite trivial: supporting the WriteElementString() method. Here’s the test case: 1 2 3 4 5 6 7 StringXmlWriter xmlWriter; xmlWriter.WriteStartElement("root"); xmlWriter.WriteElementString("element", "value"); xmlWriter.WriteEndElement(); std::string strXML = xmlWriter.GetXmlString(); // strXML should be <root><element>value</element></root> Implementation is extremely simple because WriteElementString() is nothing but a convenience method which calls WriteStartElement(), WriteString(), and WriteEndElement(). Read more...
Implementing IXmlWriter Part 2: Escaping Element Content
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-10-06
Implementing IXmlWriter Part 2: Escaping Element Content
This is part 2/14 of my Implementing IXmlWriter post series. In the previous post of this series, we ended up with a simple class which could write XML elements and element content to a std::string. However, this code has a common, serious problem that was mentioned in my post Don’t Form XML Using String Concatenation: it doesn’t properly escape XML special characters such as & and <. This means that if you call WriteString() with one of these characters, your generated XML will be invalid and will not be able to be parsed by an XML parser. Read more...
Implementing IXmlWriter Part 1: The Basics
Implementing IXmlWriter c++ ixmlwriter xml
Published: 2005-09-30
Implementing IXmlWriter Part 1: The Basics
This is part 1/14 of my Implementing IXmlWriter post series. After writing my blog post Don’t Form XML Using String Concatenation, I realized that writing a C++ System.Xml.XmlWriter workalike involves some interesting challenges. Therefore, I’ve decided to write a series of blog posts about building a streaming C++ XML generator, a.k.a. IXmlWriter, step-by-step. For this series, I will follow the practice of test-driven development and write a test case followed by an implementation which passes the test case. Read more...
Don’t Form XML Using String Concatenation
XML / XPath / XSLT c++ xml
Published: 2005-09-16
Don't Form XML Using String Concatenation
It seems very common for developers to create XML using string concatenation, as in: 1 2 3 4 5 6 7 8 9 10 std::string CreateXML ( const std::string& strValue ) { std::string strXML("<tag>"); strXML += strValue; strXML += "</tag>"; return strXML; } As any experienced XML developer knows, this code has a bug: strValue must be escaped (& must be converted to &amp;, < must be converted to &lt;, etc. Read more...
Be Careful With Doubles And C++ Streams
C++ c++
Published: 2005-09-12
Be Careful With Doubles And C++ Streams
I ran across a piece of code recently that was using ostrstream to convert a double to a string. The code looked something like: 1 2 3 4 5 6 7 std::string DoubleToString(double d) { std::ostrstream ostr; ostr << d << std::ends; std::string str(ostr.str()); ostr.freeze(false); return str; } This function was used to convert doubles to strings for insertion into an XML document, which were eventually parsed in an XSLT by the XPath number() function. Read more...
Use RAII
C++ c++ win32
Published: 2005-09-09
Use RAII
This is covered by any halfway-decent C++ book, but I believe it deserves reiteration: Use the RAII idiom. I don’t think I could explain RAII any better than HackCraft does in The RAII Programming Idiom. Let me demonstrate how to use RAII with a semi-contrived example. Here’s the pre-RAII code: 1 2 3 4 5 6 7 8 9 HMODULE hm = LoadLibrary(_T("user32.dll")); if (hm != NULL) { FARPROC proc = GetProcAddress(hm, "MessageBoxW"); if (proc ! Read more...
Use Constant References For Input-Only Parameters, Pointers Otherwise
C++ c++
Published: 2005-09-06
Use Constant References For Input-Only Parameters, Pointers Otherwise
This is a style issue, so there is no right or wrong, but I suggest using a const reference for an input-only paramater to a C++ function and a pointer for an input/output or output-only parameter. This makes it slightly more obvious that the parameter might be modified by the function. Example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ReturnType Function ( const ParamType& inputParam, ParamType* inputOutputParam, ParamType* outputParam ) { // . Read more...
Write Functions Which Take Iterators, Not Collections
C++ c++ stl
Published: 2005-09-05
Write Functions Which Take Iterators, Not Collections
If my experience is typical, this is a very common construct: 1 2 3 4 5 6 7 8 9 10 11 12 13 ReturnType Function ( const std::vector<T>& container ) { typedef std::vector<T>::const_iterator iterator_t; for (iterator_t iter = container.begin(); iter != container.end(); ++iter) { // Work with *iter } } The problem with this construct is that you have forced a container choice upon the user of your function. Read more...
Prefer Iteration To Indexing
C++ c++ stl
Published: 2005-09-02
Prefer Iteration To Indexing
I’ve seen the following STL construct countless times: 1 2 3 4 std::vector<T> container; for (int i = 0; i < container.size(); ++i) { // Work with container[i] } Unless otherwise necessary, it is better to use an STL iterator because it enables you to more easily change the underlying container. You can isolate the code changes required to one line by using typedef, as in: 1 2 3 4 5 6 7 8 9 10 typedef std::vector<T> container_t; container_t container; // Or ::const_iterator as necessary for (container_t::iterator iter = container. Read more...
Using the Excel Object Model and Performance
Excel Interop c++ excel interop mfc
Published: 2004-07-20
Using the Excel Object Model and Performance
Recently I’ve had to write a bit of code which communicates with Microsoft Excel using its object model. Here are a few things I have learned from this experience. Interaction with the Excel object model seems to use some kind of inter-process communication with an Excel process that is started behind the scenes. If things are not shut down properly, this Excel process will continue to run indefinitely in the background. Read more...