Improve Bankswitch class efficiency by using string_view.

This commit is contained in:
Stephen Anthony 2022-12-20 22:16:11 -03:30
parent 82da36dd68
commit aaaea54310
2 changed files with 37 additions and 49 deletions

View File

@ -20,13 +20,12 @@
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string Bankswitch::typeToName(Bankswitch::Type type)
{
return BSList[static_cast<int>(type)].name;
return string{BSList[static_cast<int>(type)].name};
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Bankswitch::Type Bankswitch::nameToType(string_view n) // FIXME: name
Bankswitch::Type Bankswitch::nameToType(string_view name)
{
string name{n};
const auto it = ourNameToTypes.find(name);
if(it != ourNameToTypes.end())
return it->second;
@ -37,17 +36,17 @@ Bankswitch::Type Bankswitch::nameToType(string_view n) // FIXME: name
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string Bankswitch::typeToDesc(Bankswitch::Type type)
{
return BSList[static_cast<int>(type)].desc;
return string{BSList[static_cast<int>(type)].desc};
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Bankswitch::Type Bankswitch::typeFromExtension(const FSNode& file)
{
const string& name = file.getPath();
const string::size_type idx = name.find_last_of('.');
if(idx != string::npos)
const string_view name = file.getPath();
const auto idx = name.find_last_of('.');
if(idx != string_view::npos)
{
const auto it = ourExtensions.find(name.c_str() + idx + 1);
const auto it = ourExtensions.find(name.substr(idx + 1));
if(it != ourExtensions.end())
return it->second;
}
@ -56,12 +55,12 @@ Bankswitch::Type Bankswitch::typeFromExtension(const FSNode& file)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Bankswitch::isValidRomName(const string& name, string& ext)
bool Bankswitch::isValidRomName(string_view name, string& ext)
{
const string::size_type idx = name.find_last_of('.');
if(idx != string::npos)
const auto idx = name.find_last_of('.');
if(idx != string_view::npos)
{
const char* const e = name.c_str() + idx + 1;
const auto e = name.substr(idx + 1);
const auto it = ourExtensions.find(e);
if(it != ourExtensions.end())
{
@ -73,27 +72,7 @@ bool Bankswitch::isValidRomName(const string& name, string& ext)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Bankswitch::isValidRomName(const FSNode& name, string& ext)
{
return isValidRomName(name.getPath(), ext);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Bankswitch::isValidRomName(const FSNode& name)
{
string ext; // extension not used
return isValidRomName(name.getPath(), ext);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool Bankswitch::isValidRomName(const string& name)
{
string ext; // extension not used
return isValidRomName(name, ext);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constexpr std::array<Bankswitch::Description, static_cast<int>(Bankswitch::Type::NumSchemes)>
constexpr std::array<Bankswitch::Description, static_cast<uInt32>(Bankswitch::Type::NumSchemes)>
Bankswitch::BSList = {{
{ "AUTO" , "Auto-detect" },
{ "0840" , "0840 (8K EconoBanking)" },
@ -154,8 +133,8 @@ Bankswitch::BSList = {{
#endif
}};
#if defined(GUI_SUPPORT)
const std::array<Bankswitch::SizesType, static_cast<int>(Bankswitch::Type::NumSchemes)>
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const std::array<Bankswitch::SizesType, static_cast<uInt32>(Bankswitch::Type::NumSchemes)>
Bankswitch::Sizes = {{
{ Bankswitch::any_KB, Bankswitch::any_KB }, // _AUTO
{ 8_KB, 8_KB }, // _0840
@ -215,7 +194,6 @@ Bankswitch::Sizes = {{
{ Bankswitch::any_KB, Bankswitch::any_KB }
#endif
}};
#endif // GUI_SUPPORT
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Bankswitch::ExtensionMap Bankswitch::ourExtensions = {

View File

@ -52,23 +52,23 @@ class Bankswitch
NumMulti = _128IN1 - _2IN1 + 1,
};
#ifdef GUI_SUPPORT
struct SizesType {
size_t minSize{0};
size_t maxSize{0};
};
static constexpr size_t any_KB = 0;
static const std::array<SizesType, static_cast<uInt32>(Type::NumSchemes)> Sizes;
#endif
static const std::array<SizesType,
static_cast<uInt32>(Type::NumSchemes)> Sizes;
// Info about the various bankswitch schemes, useful for displaying
// in GUI dropdown boxes, etc
struct Description {
const char* const name{nullptr};
const char* const desc{nullptr};
string_view name;
string_view desc;
};
static const std::array<Description, static_cast<int>(Type::NumSchemes)> BSList;
static const std::array<Description,
static_cast<uInt32>(Type::NumSchemes)> BSList;
public:
// Convert BSType enum to string
@ -90,14 +90,22 @@ class Bankswitch
@param name Filename of potential ROM file
@param ext The extension extracted from the given file
*/
static bool isValidRomName(const string& name, string& ext);
static bool isValidRomName(string_view name, string& ext);
/**
Convenience functions for different parameter types.
*/
static bool isValidRomName(const FSNode& name, string& ext);
static bool isValidRomName(const FSNode& name);
static bool isValidRomName(const string& name);
static inline bool isValidRomName(const FSNode& name, string& ext) {
return isValidRomName(name.getPath(), ext);
}
static inline bool isValidRomName(const FSNode& name) {
string ext; // extension not used
return isValidRomName(name.getPath(), ext);
}
static inline bool isValidRomName(string_view name) {
string ext; // extension not used
return isValidRomName(name, ext);
}
// Output operator
friend ostream& operator<<(ostream& os, const Bankswitch::Type& t) {
@ -106,14 +114,16 @@ class Bankswitch
private:
struct TypeComparator {
bool operator() (const string& a, const string& b) const {
bool operator() (string_view a, string_view b) const {
return BSPF::compareIgnoreCase(a, b) < 0;
}
};
using ExtensionMap = const std::map<string, Bankswitch::Type, TypeComparator>;
using ExtensionMap = const std::map<string_view, Bankswitch::Type,
TypeComparator>;
static ExtensionMap ourExtensions;
using NameToTypeMap = const std::map<string, Bankswitch::Type, TypeComparator>;
using NameToTypeMap = const std::map<string_view, Bankswitch::Type,
TypeComparator>;
static NameToTypeMap ourNameToTypes;
private: