patterncppMinor
A pointer vector sorted by its member function
Viewed 0 times
functionpointeritsmembersortedvector
Problem
I'm asking for suggestions on a random accessed vector with allocated elements sorted by its key obtained from its member function.
I use it with Qt tree view where the access, add and deletion of tree items is implemented via its index or row number. And I want all the tree items are automatically sorted under a tree node.
If I use
Sorted pointer vectors can access items quickly but require moving pointers when adding and deleting an item. Compare all the advantages and disadvantages, I think this can give better performance
Am I right? Does anyone have experience on it or better suggestions?
```
#pragma once
#include
#include
#include
//! A random accessed vector with allocated elements sorted by
//! its key obtained from its member function.
//! - Duplicate elements are not allowed.
template
class SortedPtrVector
{
public:
SortedPtrVector() {}
//! Add an element, return its index.
bool Add(std::unique_ptr element, int& index)
{
if (!Find((element.get()->*MemFun)(), index))
{
m_vector.insert(m_vector.begin() + index, std::move(element));
return true;
}
return false;
}
//! Find the element with a key.
//! - Return true when found and record its index;
//! - Return false when not found and record its lower bound.
bool Find(const K& key, int& index) const
{
if (m_vector.empty())
{
index = 0;
return false;
}
index = LowerBound(key);
if (index >= m_vector.size())
return false;
if ((m_vector[index].get()->*MemFun)() == key)
return true;
return false;
}
//! Return an index to the first element which does not compare less than the key.
int LowerBound(const K&
I use it with Qt tree view where the access, add and deletion of tree items is implemented via its index or row number. And I want all the tree items are automatically sorted under a tree node.
If I use
std::set, I need to calculate the index first (such as using distance function) when adding an item under a tree node and need a binary search when deleting and accessing an item.Sorted pointer vectors can access items quickly but require moving pointers when adding and deleting an item. Compare all the advantages and disadvantages, I think this can give better performance
Am I right? Does anyone have experience on it or better suggestions?
```
#pragma once
#include
#include
#include
//! A random accessed vector with allocated elements sorted by
//! its key obtained from its member function.
//! - Duplicate elements are not allowed.
template
class SortedPtrVector
{
public:
SortedPtrVector() {}
//! Add an element, return its index.
bool Add(std::unique_ptr element, int& index)
{
if (!Find((element.get()->*MemFun)(), index))
{
m_vector.insert(m_vector.begin() + index, std::move(element));
return true;
}
return false;
}
//! Find the element with a key.
//! - Return true when found and record its index;
//! - Return false when not found and record its lower bound.
bool Find(const K& key, int& index) const
{
if (m_vector.empty())
{
index = 0;
return false;
}
index = LowerBound(key);
if (index >= m_vector.size())
return false;
if ((m_vector[index].get()->*MemFun)() == key)
return true;
return false;
}
//! Return an index to the first element which does not compare less than the key.
int LowerBound(const K&
Solution
There's probably much that can be done here, but I'll point out some things I've noticed:
-
As @David Harkness has mentioned in the comments, the method-naming looks like C# naming (uppercase, or PascalCase). While this isn't necessarily a bad thing as C++ is more flexible with naming, one problem here is that your types (particularly the class) and functions use the same naming convention, which may cause confusion for others.
I would use separate naming for both of them, such as by changing the functions to camelCase or snake_case, while keeping the types as is.
-
Since you're not overloading the default constructor, you don't have to include it. The compiler will make a default one for you.
However, with C++11, you can now use
-
It looks like
Based on the name, it should just attempt to add something. But if it's still necessary to update the index, then it should just return that. You could instead return an
-
It may also be better to have
-
-
Also,
-
As @David Harkness has mentioned in the comments, the method-naming looks like C# naming (uppercase, or PascalCase). While this isn't necessarily a bad thing as C++ is more flexible with naming, one problem here is that your types (particularly the class) and functions use the same naming convention, which may cause confusion for others.
I would use separate naming for both of them, such as by changing the functions to camelCase or snake_case, while keeping the types as is.
-
Since you're not overloading the default constructor, you don't have to include it. The compiler will make a default one for you.
However, with C++11, you can now use
default constructors:SortedPtrVector() = default;-
It looks like
Add() is doing too many things:- the actual adding
- updating a reference
- returning a
bool
Based on the name, it should just attempt to add something. But if it's still necessary to update the index, then it should just return that. You could instead return an
std::pair as done with std::map::insert, but that may not be what you want here.-
Find() shouldn't set index to 0 if the vector is empty. An index of 0 is a valid position for a non-empty container (the first position). Instead, it could return -1, which is an out-of-bounds value commonly used with not found (this is also the same value as the constant std::string::npos).It may also be better to have
Find() return an index instead of a bool. Assuming this is to somewhat resemble std::map::find, it can return a past-the-end value (an iterator) if the key isn't found.-
DeleteAt() may do something problematic if the argument isn't checked prior to calling erase() (an invalid index can cause erase() to attempt access to some other location, leading to undefined behavior).-
Size() should be const as it's not modifying any data members. You could also make it noexcept (also C++11) since it's guaranteed not to throw.Also,
unsigned is not quite the specific return type of std::size(). It should either return an std::size_t (usually assumed), std::vector>::size_type (very lengthy), or even better and shorter with C++14 (if your compiler also supports it): the type deduced by auto.auto Size() const noexcept { return m_vector.size(); }Code Snippets
SortedPtrVector() = default;auto Size() const noexcept { return m_vector.size(); }Context
StackExchange Code Review Q#20079, answer score: 7
Revisions (0)
No revisions yet.