2009-12-20 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Sort _optionHandlers in OptionParser by name in ascending order.
	Use vector instead of deque for efficiency.
	* src/OptionHandler.h
	* src/OptionHandlerFactory.h
	* src/OptionParser.cc
	* src/OptionParser.h
	* src/version_usage.cc
	* test/OptionParserTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2009-12-20 14:25:51 +00:00
parent 6c6e7f2c51
commit e77e1ec24d
7 changed files with 129 additions and 72 deletions

View File

@ -1,3 +1,14 @@
2009-12-20 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Sort _optionHandlers in OptionParser by name in ascending order.
Use vector instead of deque for efficiency.
* src/OptionHandler.h
* src/OptionHandlerFactory.h
* src/OptionParser.cc
* src/OptionParser.h
* src/version_usage.cc
* test/OptionParserTest.cc
2009-12-20 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Code cleanup

View File

@ -38,7 +38,7 @@
#include "common.h"
#include <string>
#include <deque>
#include <vector>
#include <iosfwd>
#include "SharedHandle.h"
@ -87,8 +87,30 @@ public:
virtual void setOptionID(int id) = 0;
};
class OptionHandlerNameLesser:public std::binary_function
<SharedHandle<OptionHandler>, SharedHandle<OptionHandler>, bool> {
public:
bool operator()
(const SharedHandle<OptionHandler>& lhs,
const SharedHandle<OptionHandler>& rhs) const
{
return lhs->getName() < rhs->getName();
}
};
class OptionHandlerIDLesser:public std::binary_function
<SharedHandle<OptionHandler>, SharedHandle<OptionHandler>, bool> {
public:
bool operator()
(const SharedHandle<OptionHandler>& lhs,
const SharedHandle<OptionHandler>& rhs) const
{
return lhs->getOptionID() < rhs->getOptionID();
}
};
typedef SharedHandle<OptionHandler> OptionHandlerHandle;
typedef std::deque<OptionHandlerHandle> OptionHandlers;
typedef std::vector<OptionHandlerHandle> OptionHandlers;
std::ostream& operator<<(std::ostream& o, const OptionHandler& optionHandler);

View File

@ -36,8 +36,10 @@
#define _D_OPTION_HANDLER_FACTORY_H_
#include "common.h"
#include <vector>
#include "SharedHandle.h"
#include <deque>
namespace aria2 {
@ -45,7 +47,7 @@ class OptionHandler;
class OptionHandlerFactory {
public:
static std::deque<SharedHandle<OptionHandler> > createOptionHandlers();
static std::vector<SharedHandle<OptionHandler> > createOptionHandlers();
};
} // namespace aria2

View File

@ -172,36 +172,58 @@ void OptionParser::parse(Option& option, std::istream& is)
}
}
OptionHandlerHandle OptionParser::getOptionHandlerByName(const std::string& optName)
{
for(OptionHandlers::iterator itr = _optionHandlers.begin();
itr != _optionHandlers.end(); ++itr) {
if((*itr)->canHandle(optName)) {
return *itr;
}
class DummyOptionHandler:public NameMatchOptionHandler {
protected:
virtual void parseArg(Option& option, const std::string& arg) {}
public:
DummyOptionHandler(const std::string& name):NameMatchOptionHandler(name) {}
virtual std::string createPossibleValuesString() const
{
return A2STR::NIL;
}
return SharedHandle<OptionHandler>(new NullOptionHandler());
};
OptionHandlerHandle OptionParser::getOptionHandlerByName
(const std::string& optName)
{
SharedHandle<OptionHandler> handler(new DummyOptionHandler(optName));
std::vector<SharedHandle<OptionHandler> >::const_iterator i =
std::lower_bound(_optionHandlers.begin(), _optionHandlers.end(),
handler, OptionHandlerNameLesser());
if(i == _optionHandlers.end()) {
handler.reset(new NullOptionHandler());
} else {
handler = *i;
}
return handler;
}
void OptionParser::setOptionHandlers(const std::deque<SharedHandle<OptionHandler> >& optionHandlers)
void OptionParser::setOptionHandlers
(const std::vector<SharedHandle<OptionHandler> >& optionHandlers)
{
_optionHandlers = optionHandlers;
for(std::deque<SharedHandle<OptionHandler> >::iterator i =
for(std::vector<SharedHandle<OptionHandler> >::iterator i =
_optionHandlers.begin(); i != _optionHandlers.end(); ++i) {
(*i)->setOptionID(++_idCounter);
}
std::sort(_optionHandlers.begin(), _optionHandlers.end(),
OptionHandlerNameLesser());
}
void OptionParser::addOptionHandler
(const SharedHandle<OptionHandler>& optionHandler)
{
optionHandler->setOptionID(++_idCounter);
_optionHandlers.push_back(optionHandler);
std::vector<SharedHandle<OptionHandler> >::iterator i =
std::lower_bound(_optionHandlers.begin(), _optionHandlers.end(),
optionHandler, OptionHandlerNameLesser());
_optionHandlers.insert(i, optionHandler);
}
void OptionParser::parseDefaultValues(Option& option) const
{
for(std::deque<SharedHandle<OptionHandler> >::const_iterator i =
for(std::vector<SharedHandle<OptionHandler> >::const_iterator i =
_optionHandlers.begin(); i != _optionHandlers.end(); ++i) {
if(!(*i)->getDefaultValue().empty()) {
(*i)->parse(option, (*i)->getDefaultValue());
@ -209,12 +231,12 @@ void OptionParser::parseDefaultValues(Option& option) const
}
}
class FindByTag :
class FindOptionHandlerByTag :
public std::unary_function<SharedHandle<OptionHandler>, bool> {
private:
std::string _tag;
public:
FindByTag(const std::string& tag):_tag(tag) {}
FindOptionHandlerByTag(const std::string& tag):_tag(tag) {}
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
{
@ -222,22 +244,24 @@ public:
}
};
std::deque<SharedHandle<OptionHandler> >
std::vector<SharedHandle<OptionHandler> >
OptionParser::findByTag(const std::string& tag) const
{
std::deque<SharedHandle<OptionHandler> > result;
std::vector<SharedHandle<OptionHandler> > result;
std::remove_copy_if(_optionHandlers.begin(), _optionHandlers.end(),
std::back_inserter(result),
std::not1(FindByTag(tag)));
std::not1(FindOptionHandlerByTag(tag)));
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
return result;
}
class FindByNameSubstring :
class FindOptionHandlerByNameSubstring :
public std::unary_function<SharedHandle<OptionHandler> , bool> {
private:
std::string _substring;
public:
FindByNameSubstring(const std::string& substring):_substring(substring) {}
FindOptionHandlerByNameSubstring
(const std::string& substring):_substring(substring) {}
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
{
@ -246,22 +270,24 @@ public:
}
};
std::deque<SharedHandle<OptionHandler> >
std::vector<SharedHandle<OptionHandler> >
OptionParser::findByNameSubstring(const std::string& substring) const
{
std::deque<SharedHandle<OptionHandler> > result;
std::vector<SharedHandle<OptionHandler> > result;
std::remove_copy_if(_optionHandlers.begin(), _optionHandlers.end(),
std::back_inserter(result),
std::not1(FindByNameSubstring(substring)));
std::not1(FindOptionHandlerByNameSubstring(substring)));
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
return result;
}
std::deque<SharedHandle<OptionHandler> > OptionParser::findAll() const
std::vector<SharedHandle<OptionHandler> > OptionParser::findAll() const
{
std::deque<SharedHandle<OptionHandler> > result;
std::vector<SharedHandle<OptionHandler> > result;
std::remove_copy_if(_optionHandlers.begin(), _optionHandlers.end(),
std::back_inserter(result),
mem_fun_sh(&OptionHandler::isHidden));
std::sort(result.begin(), result.end(), OptionHandlerIDLesser());
return result;
}
@ -269,39 +295,35 @@ template<typename InputIterator, typename Predicate>
static SharedHandle<OptionHandler> findOptionHandler
(InputIterator first, InputIterator last, Predicate pred)
{
SharedHandle<OptionHandler> handler;
InputIterator i = std::find_if(first, last, pred);
if(i == last) {
return SharedHandle<OptionHandler>();
} else {
return *i;
if(i != last) {
handler = *i;
}
return handler;
}
class FindByName :
public std::unary_function<SharedHandle<OptionHandler> , bool> {
private:
std::string _name;
public:
FindByName(const std::string& name):_name(name) {}
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
{
return !optionHandler->isHidden() && optionHandler->getName() == _name;
}
};
SharedHandle<OptionHandler>
OptionParser::findByName(const std::string& name) const
{
return findOptionHandler(_optionHandlers.begin(), _optionHandlers.end(),
FindByName(name));
SharedHandle<OptionHandler> handler(new DummyOptionHandler(name));
std::vector<SharedHandle<OptionHandler> >::const_iterator i =
std::lower_bound(_optionHandlers.begin(), _optionHandlers.end(),
handler, OptionHandlerNameLesser());
if(i == _optionHandlers.end() || (*i)->isHidden()) {
handler.reset();
} else {
handler = *i;
}
return handler;
}
class FindByID:public std::unary_function<SharedHandle<OptionHandler>, bool> {
class FindOptionHandlerByID:public std::unary_function
<SharedHandle<OptionHandler>, bool> {
private:
int _id;
public:
FindByID(int id):_id(id) {}
FindOptionHandlerByID(int id):_id(id) {}
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
{
@ -312,15 +334,15 @@ public:
SharedHandle<OptionHandler> OptionParser::findByID(int id) const
{
return findOptionHandler(_optionHandlers.begin(), _optionHandlers.end(),
FindByID(id));
FindOptionHandlerByID(id));
}
class FindByShortName:
class FindOptionHandlerByShortName:
public std::unary_function<SharedHandle<OptionHandler>, bool> {
private:
char _shortName;
public:
FindByShortName(char shortName):_shortName(shortName) {}
FindOptionHandlerByShortName(char shortName):_shortName(shortName) {}
bool operator()(const SharedHandle<OptionHandler>& optionHandler) const
{
@ -332,16 +354,10 @@ public:
SharedHandle<OptionHandler> OptionParser::findByShortName(char shortName) const
{
return findOptionHandler(_optionHandlers.begin(), _optionHandlers.end(),
FindByShortName(shortName));
FindOptionHandlerByShortName(shortName));
}
const std::deque<SharedHandle<OptionHandler> >&
OptionParser::getOptionHandlers() const
{
return _optionHandlers;
}
SharedHandle<OptionParser> OptionParser::_optionParser;
SharedHandle<OptionParser> OptionParser::getInstance()

View File

@ -39,6 +39,7 @@
#include <string>
#include <deque>
#include <vector>
#include <iosfwd>
#include "SharedHandle.h"
@ -52,7 +53,9 @@ class OptionParser {
private:
int _idCounter;
std::deque<SharedHandle<OptionHandler> > _optionHandlers;
// _optionHandlers is sorted by OptionHandler::getName() in
// ascending order.
std::vector<SharedHandle<OptionHandler> > _optionHandlers;
SharedHandle<OptionHandler>
getOptionHandlerByName(const std::string& optName);
@ -74,27 +77,31 @@ public:
void parseDefaultValues(Option& option) const;
void setOptionHandlers
(const std::deque<SharedHandle<OptionHandler> >& optionHandlers);
(const std::vector<SharedHandle<OptionHandler> >& optionHandlers);
void addOptionHandler(const SharedHandle<OptionHandler>& optionHandler);
std::deque<SharedHandle<OptionHandler> >
// Hidden options are not returned.
std::vector<SharedHandle<OptionHandler> >
findByTag(const std::string& tag) const;
std::deque<SharedHandle<OptionHandler> >
// Hidden options are not returned.
std::vector<SharedHandle<OptionHandler> >
findByNameSubstring(const std::string& substring) const;
std::deque<SharedHandle<OptionHandler> > findAll() const;
// Hidden options are not returned.
std::vector<SharedHandle<OptionHandler> > findAll() const;
// Hidden options are not returned.
SharedHandle<OptionHandler>
findByName(const std::string& name) const;
// Hidden options are not returned.
SharedHandle<OptionHandler> findByID(int id) const;
// Hidden options are not returned.
SharedHandle<OptionHandler> findByShortName(char shortName) const;
const std::deque<SharedHandle<OptionHandler> >& getOptionHandlers() const;
static SharedHandle<OptionParser> getInstance();
};

View File

@ -86,7 +86,7 @@ void showUsage(const std::string& keyword, const OptionParser& oparser) {
" METALINK_FILE]...") << "\n"
<< "\n";
if(util::startsWith(keyword, "#")) {
std::deque<SharedHandle<OptionHandler> > handlers =
std::vector<SharedHandle<OptionHandler> > handlers =
keyword == TAG_ALL ? oparser.findAll():oparser.findByTag(keyword);
if(keyword == TAG_ALL) {
std::cout << _("Printing all options.");
@ -106,7 +106,7 @@ void showUsage(const std::string& keyword, const OptionParser& oparser) {
std::ostream_iterator<SharedHandle<OptionHandler> >
(std::cout, "\n\n"));
} else {
std::deque<SharedHandle<OptionHandler> > handlers =
std::vector<SharedHandle<OptionHandler> > handlers =
oparser.findByNameSubstring(keyword);
if(!handlers.empty()) {
std::cout << StringFormat(_("Printing options whose name includes"

View File

@ -2,7 +2,6 @@
#include <cstring>
#include <sstream>
#include <deque>
#include <cppunit/extensions/HelperMacros.h>
@ -75,7 +74,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(OptionParserTest);
void OptionParserTest::testFindAll()
{
std::deque<SharedHandle<OptionHandler> > res = _oparser->findAll();
std::vector<SharedHandle<OptionHandler> > res = _oparser->findAll();
CPPUNIT_ASSERT_EQUAL((size_t)3, res.size());
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), res[0]->getName());
CPPUNIT_ASSERT_EQUAL(std::string("bravo"), res[1]->getName());
@ -84,7 +83,7 @@ void OptionParserTest::testFindAll()
void OptionParserTest::testFindByNameSubstring()
{
std::deque<SharedHandle<OptionHandler> > res =
std::vector<SharedHandle<OptionHandler> > res =
_oparser->findByNameSubstring("l");
CPPUNIT_ASSERT_EQUAL((size_t)2, res.size());
CPPUNIT_ASSERT_EQUAL(std::string("alpha"), res[0]->getName());
@ -93,7 +92,7 @@ void OptionParserTest::testFindByNameSubstring()
void OptionParserTest::testFindByTag()
{
std::deque<SharedHandle<OptionHandler> > res =
std::vector<SharedHandle<OptionHandler> > res =
_oparser->findByTag("pineapple");
CPPUNIT_ASSERT_EQUAL((size_t)2, res.size());
CPPUNIT_ASSERT_EQUAL(std::string("bravo"), res[0]->getName());