Redo Command and CommandMap again
This commit is contained in:
parent
a509c4bded
commit
4df3f620f1
@ -1,28 +1,12 @@
|
|||||||
#include "Command.h"
|
#include "Command.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static const struct
|
Command::Command()
|
||||||
{
|
{
|
||||||
const char * name;
|
id = NOP;
|
||||||
uint32_t flags;
|
count = 0u;
|
||||||
} Commands[] = {
|
following_char = 0u;
|
||||||
{"nop", 0u},
|
motion.id = Motion::NOP;
|
||||||
{"forward-up-to-char", Command::FOLLOWING_CHAR},
|
motion.count = 0u;
|
||||||
{"forward-on-to-char", Command::FOLLOWING_CHAR},
|
motion.following_char = 0u;
|
||||||
{"back-up-to-char", Command::FOLLOWING_CHAR},
|
|
||||||
{"back-on-to-char", Command::FOLLOWING_CHAR},
|
|
||||||
{"delete", Command::RANGE},
|
|
||||||
{"delete-line", 0u},
|
|
||||||
};
|
|
||||||
|
|
||||||
uint32_t Command::find_command_by_name(const char * name)
|
|
||||||
{
|
|
||||||
for (uint32_t i = 0u; i < COMMAND_COUNT; i++)
|
|
||||||
{
|
|
||||||
if (strcmp(Commands[i].name, name) == 0u)
|
|
||||||
{
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return COMMAND_COUNT;
|
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,6 @@
|
|||||||
class Command
|
class Command
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
enum : uint32_t
|
|
||||||
{
|
|
||||||
FOLLOWING_CHAR = 0x1u,
|
|
||||||
RANGE = 0x2u,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum : uint32_t
|
enum : uint32_t
|
||||||
{
|
{
|
||||||
NOP,
|
NOP,
|
||||||
@ -24,7 +18,29 @@ public:
|
|||||||
COMMAND_COUNT,
|
COMMAND_COUNT,
|
||||||
};
|
};
|
||||||
|
|
||||||
uint32_t find_command_by_name(const char * name);
|
class Motion
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
enum : uint32_t
|
||||||
|
{
|
||||||
|
NOP,
|
||||||
|
FORWARD_UP_TO_CHAR,
|
||||||
|
FORWARD_ON_TO_CHAR,
|
||||||
|
BACK_UP_TO_CHAR,
|
||||||
|
BACK_ON_TO_CHAR,
|
||||||
|
};
|
||||||
|
|
||||||
|
uint32_t id;
|
||||||
|
uint32_t count;
|
||||||
|
uint32_t following_char;
|
||||||
|
};
|
||||||
|
|
||||||
|
uint32_t id;
|
||||||
|
uint32_t count;
|
||||||
|
uint32_t following_char;
|
||||||
|
Motion motion;
|
||||||
|
|
||||||
|
Command();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,65 +1,38 @@
|
|||||||
#include "CommandMap.h"
|
#include "CommandMap.h"
|
||||||
#include "Command.h"
|
#include "Command.h"
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
CommandMap::CommandMap()
|
void CommandMap::add(const char * s, uint32_t id,
|
||||||
|
std::shared_ptr<CommandMap> next_map, bool following_char)
|
||||||
{
|
{
|
||||||
m_commands[0] = m_commands[1] = Command::NOP;
|
Node * node = &m_root_node;
|
||||||
}
|
size_t length = strlen(s);
|
||||||
|
if (length < 1u)
|
||||||
std::shared_ptr<CommandMap> CommandMap::access(uint32_t entry, bool insert)
|
return;
|
||||||
{
|
for (size_t i = 0u; i < length; i++)
|
||||||
auto it = m_children.find(entry);
|
|
||||||
if (it == m_children.end())
|
|
||||||
{
|
{
|
||||||
if (insert)
|
uint32_t c = (uint32_t)s[i];
|
||||||
|
auto it = node->next_chars.find(c);
|
||||||
|
if (it != node->next_chars.end())
|
||||||
{
|
{
|
||||||
std::shared_ptr<CommandMap> new_cm = std::make_shared<CommandMap>();
|
node = &*it->second;
|
||||||
m_children[entry] = new_cm;
|
|
||||||
return new_cm;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return nullptr;
|
auto new_node = std::make_shared<Node>();
|
||||||
|
node->next_chars[c] = new_node;
|
||||||
|
node = &*new_node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return it->second;
|
uint8_t flags = Node::FLAG_TERMINATOR;
|
||||||
|
if (following_char)
|
||||||
|
flags |= Node::FLAG_FOLLOWING_CHAR;
|
||||||
|
node->id = id;
|
||||||
|
node->flags = flags;
|
||||||
|
node->next_map = next_map;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<CommandMap> CommandMap::access(const std::vector<uint32_t> entries, bool insert)
|
std::shared_ptr<Command> CommandMap::get_command(uint32_t * command_characters, uint32_t count)
|
||||||
{
|
{
|
||||||
if (entries.size() == 0u)
|
return nullptr;
|
||||||
{
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
std::shared_ptr<CommandMap> cm = access(entries[0], insert);
|
|
||||||
for (size_t i = 1u; i < entries.size(); i++)
|
|
||||||
{
|
|
||||||
if (cm == nullptr)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
cm = cm->access(entries[i], insert);
|
|
||||||
}
|
|
||||||
return cm;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t CommandMap::command() const
|
|
||||||
{
|
|
||||||
if (m_commands[1] != Command::NOP)
|
|
||||||
{
|
|
||||||
return m_commands[1];
|
|
||||||
}
|
|
||||||
return m_commands[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
void CommandMap::set_command(uint32_t command, bool user_mapping)
|
|
||||||
{
|
|
||||||
m_commands[user_mapping ? 1 : 0] = command;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CommandMap::empty() const
|
|
||||||
{
|
|
||||||
return (m_commands[0] == Command::NOP) &&
|
|
||||||
(m_commands[1] == Command::NOP) &&
|
|
||||||
(m_children.size() == 0u);
|
|
||||||
}
|
}
|
||||||
|
@ -4,37 +4,37 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "Command.h"
|
||||||
|
|
||||||
class CommandMap
|
class CommandMap
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CommandMap();
|
void add(const char * s, uint32_t id, std::shared_ptr<CommandMap> next_map,
|
||||||
std::shared_ptr<CommandMap> get(uint32_t entry)
|
bool following_char);
|
||||||
{
|
std::shared_ptr<Command> get_command(uint32_t * command_characters, uint32_t count);
|
||||||
return access(entry, false);
|
|
||||||
}
|
|
||||||
std::shared_ptr<CommandMap> get(const std::vector<uint32_t> entries)
|
|
||||||
{
|
|
||||||
return access(entries, false);
|
|
||||||
}
|
|
||||||
std::shared_ptr<CommandMap> put(uint32_t entry)
|
|
||||||
{
|
|
||||||
return access(entry, true);
|
|
||||||
}
|
|
||||||
std::shared_ptr<CommandMap> put(const std::vector<uint32_t> entries)
|
|
||||||
{
|
|
||||||
return access(entries, true);
|
|
||||||
}
|
|
||||||
uint32_t command() const;
|
|
||||||
void set_command(uint32_t command, bool user_mapping);
|
|
||||||
bool empty() const;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint32_t m_commands[2];
|
struct Node
|
||||||
std::unordered_map<uint32_t, std::shared_ptr<CommandMap>> m_children;
|
{
|
||||||
|
enum : uint8_t
|
||||||
|
{
|
||||||
|
FLAG_FOLLOWING_CHAR = 0x1u,
|
||||||
|
FLAG_TERMINATOR = 0x2u,
|
||||||
|
};
|
||||||
|
|
||||||
std::shared_ptr<CommandMap> access(uint32_t entry, bool insert);
|
std::unordered_map<uint32_t, std::shared_ptr<Node>> next_chars;
|
||||||
std::shared_ptr<CommandMap> access(const std::vector<uint32_t> entries, bool insert);
|
std::shared_ptr<CommandMap> next_map;
|
||||||
|
uint32_t id;
|
||||||
|
uint8_t flags;
|
||||||
|
|
||||||
|
Node()
|
||||||
|
{
|
||||||
|
id = 0u;
|
||||||
|
flags = 0u;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Node m_root_node;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user