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