Skip to content

Commit

Permalink
TinyVector is for PODs only...
Browse files Browse the repository at this point in the history
(Or trivially copyable. Or whatever. Who cares, C++ is an unlearnable mess,
and this is not the place to obsess with such details.)
  • Loading branch information
xparq committed Oct 3, 2023
1 parent d5e596c commit 32d3934
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions tinyvector.hpp → TinyPODVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <string.h>

template <typename T, unsigned MAX_SIZE = 15>
class TinyVector
class TinyPODVector
{
public:
typedef uint16_t size_type;
Expand Down Expand Up @@ -49,24 +49,26 @@ class TinyVector
const value_type* end() const { return items + tail; }
const value_type& back() const { return *(items + tail - 1); }

bool operator==(const TinyVector& a) const
//! NOTE: If, for example, T == const char* (or any pointer), we are
//! just comparing the pointers below!
//! The usefulness of this depends on the compiler putting all
//! the equivalent const char* string literals under the same
//! pointer, which is of course not guaranteed by the specification.
//! E.g. items added by inline functions might be a problem for this,
//! as they might be treated as different functions across different
//! translation units...

bool operator==(const TinyPODVector& a) const
{
return size() == a.size() && 0 == memcmp(items, a.items, sizeof(value_type) * tail);
}

bool operator<(const TinyVector& a) const
bool operator<(const TinyPODVector& a) const
{
if (size() < a.size())
return true;
else if (size() > a.size())
return false;

//! We are just comparing pointer values here.
//! The perfect correctness of this depends on compiler putting all
//! string literals (const char *) under the same pointer, which is
//! not guaranteed under the specification, thus inline functions
//! might be a problem for this, and might be treated as different
//! functions when looked at from different translation units!
return memcmp(items, a.items, sizeof(value_type) * tail) < 0;
}
}; // class TinyVector
}; // class TinyPODVector
4 changes: 2 additions & 2 deletions iprof.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#endif

#ifndef IPROF_DISABLE_OPTIM
# include "tinyvector.hpp" // Fast, but very limited vector impl.
# include "TinyPODVector.hpp" // Fast, but very limited POD container
#endif
#include <vector> // std::vector is needed anyway for other things
#include <map>
Expand All @@ -31,7 +31,7 @@
namespace iProf
{
#ifndef IPROF_DISABLE_OPTIM
typedef TinyVector<const char*, 15> TagList;
typedef TinyPODVector<const char*, 15> TagList;
// With 15, sizeof == 64 (15*4 + 2 + 2) for 32-bit systems,
// and should be (aligned to) 128 for 64-bit systems.
#else
Expand Down

0 comments on commit 32d3934

Please sign in to comment.