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

Changed ArgMap to be backed by a vector instead of a map. #262

Merged
merged 1 commit into from
Jan 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
return;
case internal::Arg::NAMED_ARG:
named_arg = static_cast<const NamedArg*>(args.values_[i].pointer);
map_.insert(Pair(named_arg->name, *named_arg));
map_.push_back(Pair(named_arg->name, *named_arg));
break;
default:
/*nothing*/;
Expand All @@ -628,7 +628,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
internal::Arg::Type arg_type = args.type(i);
if (arg_type == internal::Arg::NAMED_ARG) {
named_arg = static_cast<const NamedArg*>(args.args_[i].pointer);
map_.insert(Pair(named_arg->name, *named_arg));
map_.push_back(Pair(named_arg->name, *named_arg));
}
}
for (unsigned i = ArgList::MAX_PACKED_ARGS;/*nothing*/; ++i) {
Expand All @@ -637,7 +637,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
return;
case internal::Arg::NAMED_ARG:
named_arg = static_cast<const NamedArg*>(args.args_[i].pointer);
map_.insert(Pair(named_arg->name, *named_arg));
map_.push_back(Pair(named_arg->name, *named_arg));
break;
default:
/*nothing*/;
Expand Down
12 changes: 9 additions & 3 deletions format.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
#include <memory>
#include <stdexcept>
#include <string>
#include <map>
#include <vector>
#include <utility>

#ifndef FMT_USE_IOSTREAMS
# define FMT_USE_IOSTREAMS 1
Expand Down Expand Up @@ -1671,7 +1672,7 @@ namespace internal {
template <typename Char>
class ArgMap {
private:
typedef std::map<fmt::BasicStringRef<Char>, internal::Arg> MapType;
typedef std::vector<std::pair<fmt::BasicStringRef<Char>, internal::Arg> > MapType;
typedef typename MapType::value_type Pair;

MapType map_;
Expand All @@ -1680,7 +1681,12 @@ class ArgMap {
FMT_API void init(const ArgList &args);

const internal::Arg* find(const fmt::BasicStringRef<Char> &name) const {
typename MapType::const_iterator it = map_.find(name);
typename MapType::const_iterator it = map_.begin();
// the list is unsorted, so just return the first matching name.
for (; it != map_.end(); ++it) {
if (it->first == name)
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer return early here and prevent the unnecessary check in the end.

}
return it != map_.end() ? &it->second : 0;
}
};
Expand Down