Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use map in class_list #4363

Merged
merged 1 commit into from
Jul 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 8 additions & 19 deletions src/shogun/base/class_list.cpp.templ
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,20 @@ REPLACE template_definitions THIS

REPLACE complex_template_definitions THIS

typedef CSGObject* (*create_function)(EPrimitiveType generic);
#ifndef DOXYGEN_SHOULD_SKIP_THIS
typedef struct
{
const char* m_class_name;
create_function m_create_function;
} class_list_entry_t;
#endif
typedef CSGObject* (*CreateFunction)(EPrimitiveType generic);

static class_list_entry_t class_list[] = {
static const std::map<std::string, CreateFunction> classes = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is O(logn) rather than O(n) ?
Good call!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah map fits better here. It became possible since we use initializer list here {} and C++11 is enabled.

REPLACE struct THIS
{NULL, NULL}
};

CSGObject* shogun::create(const char* classname, EPrimitiveType generic)
{
for (class_list_entry_t* i=class_list; i->m_class_name != NULL;
i++)
auto entry = classes.find(classname);
if (entry != classes.end())
{
if (strncmp(i->m_class_name, classname, STRING_LEN) == 0)
return i->m_create_function(generic);
return entry->second(generic);
}

return NULL;
return nullptr;
}

void shogun::delete_object(CSGObject* object)
Expand All @@ -78,10 +68,9 @@ void shogun::delete_object(CSGObject* object)
std::set<std::string> shogun::available_objects()
{
std::set<std::string> result;
for (class_list_entry_t* i=class_list; i->m_class_name != NULL;
i++)
for (const auto& each : classes)
{
result.insert(i->m_class_name);
result.insert(each.first);
}
return result;
}