Skip to content

Commit

Permalink
hotplace rev.528 grooming
Browse files Browse the repository at this point in the history
  • Loading branch information
princeb612 committed May 18, 2024
1 parent 604bf02 commit 62bbf5a
Show file tree
Hide file tree
Showing 23 changed files with 2,941 additions and 1,669 deletions.
1 change: 0 additions & 1 deletion sdk/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <sdk/base/basic/ieee754.hpp>
#include <sdk/base/basic/keyvalue.hpp>
#include <sdk/base/basic/obfuscate_string.hpp>
#include <sdk/base/basic/tree.hpp>
#include <sdk/base/basic/valist.hpp>
#include <sdk/base/basic/variant.hpp>

Expand Down
12 changes: 7 additions & 5 deletions sdk/base/basic/huffman_coding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ huffman_coding &huffman_coding::learn() {
_codetable.clear();
_reverse_codetable.clear();

// _measure .. count(weight) by symbol, see hc_t::operator
// _btree .. merge by weight until (1 == size()), see hc_comparator::operator

_measure.for_each([&](hc_t const &t) -> void { _btree.insert(t); });

while (_btree.size() > 1) {
Expand All @@ -62,7 +65,7 @@ huffman_coding &huffman_coding::learn() {
k.weight = k_lhs.weight + k_rhs.weight;
k.flags = 1; // merged

typename btree_t::node_t *newone = _btree.insert(k, _btree._root); // merged
typename btree_t::node_t *newone = _btree.add(k); // merged

map_pib_t pib = _m.insert(std::make_pair(k, _btree.clone_nocascade(newone)));
pib.first->second->_left = l;
Expand All @@ -77,9 +80,10 @@ huffman_coding &huffman_coding::infer() {

if (root) {
hc_temp hc;
// generate code .. left '0', right '1'
infer(hc, root);

clear(root);
_btree.clean(root);
}

return *this;
Expand Down Expand Up @@ -377,8 +381,6 @@ huffman_coding::node_t *huffman_coding::build(node_t **root) {
return p;
}

void huffman_coding::clear(node_t *&root) { _btree.clear(root); }

void huffman_coding::build(typename btree_t::node_t *&p) {
if (p) {
if (p->_left) {
Expand All @@ -391,7 +393,7 @@ void huffman_coding::build(typename btree_t::node_t *&p) {
if (_m.end() != iter) {
typename btree_t::node_t *t = iter->second;

_btree.clear(p);
_btree.clean(p);
p = t;
_m.erase(iter);
}
Expand Down
5 changes: 3 additions & 2 deletions sdk/base/basic/huffman_coding.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <deque>
#include <functional>
#include <map>
#include <sdk/base/basic/tree.hpp>
#include <sdk/base/basic/nostd/tree.hpp>
#include <sdk/base/error.hpp>
#include <sdk/base/stl.hpp>
#include <sdk/base/stream/basic_stream.hpp>
Expand Down Expand Up @@ -120,6 +120,7 @@ class huffman_coding {
* { 1, "11111111111111111011000" },
* { 2, "1111111111111111111111100010" },
* // ...
* { 0, nullptr },
* };
*
* huffman_coding huff;
Expand All @@ -133,6 +134,7 @@ class huffman_coding {
* { 1, "11111111111111111011000" },
* { 2, "1111111111111111111111100010" },
* // ...
* { 0, nullptr },
* };
*
* huffman_coding huff;
Expand Down Expand Up @@ -183,7 +185,6 @@ class huffman_coding {

protected:
node_t *build(node_t **root = nullptr);
void clear(node_t *&root);
void build(typename btree_t::node_t *&p);
void infer(hc_temp &hc, typename btree_t::node_t *t);

Expand Down
12 changes: 6 additions & 6 deletions sdk/base/basic/keyvalue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class t_skey_value {
}

/**
* @brief add, kv_update
* @brief add, update
* @param const std::string& name [IN]
* @param const value_t& value [IN]
* @param uint32 mode [INOPT] see key_value_mode_t
Expand Down Expand Up @@ -110,14 +110,14 @@ class t_skey_value {
return ret;
}
/**
* @brief kv_update
* @brief update
* @param const std::string& name [in]
* @param const value_t& value [in]
* @return error code (see error.hpp)
* @remarks
* set(name, value, key_value_mode_t::kv_update);
*/
return_t kv_update(const std::string& name, const value_t& value) { return set(name, value, key_value_mode_t::kv_update); }
return_t update(const std::string& name, const value_t& value) { return set(name, value, key_value_mode_t::kv_update); }
/**
* @brief remove
* @param const std::string& name [IN]
Expand Down Expand Up @@ -170,7 +170,7 @@ class t_skey_value {
/**
* @brief exist
* @remarks
* kv.kv_update ("key", "value");
* kv.update ("key", "value");
* result = exist ("key"); // true
* result = exist ("value"); // false
*/
Expand Down Expand Up @@ -198,7 +198,7 @@ class t_skey_value {
* @brief return value by key
* @param const std::string& name
* @remarks
* kv.kv_update ("key", "value");
* kv.update ("key", "value");
* value = kv ["key"]; // "value"
* value = kv ["value"]; // nullptr
*/
Expand Down Expand Up @@ -228,7 +228,7 @@ class t_skey_value {
* @param value_t& value [OUT]
* @return error code (see error.hpp)
* @remarks
* kv.kv_update ("key", "value");
* kv.update ("key", "value");
* kv.query ("key", value); // "value"
* kv.query ("value", value); // ""
*/
Expand Down
38 changes: 27 additions & 11 deletions sdk/base/basic/nostd/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,33 @@
#ifndef __HOTPLACE_SDK_BASE_BASIC_NOSTD_CONTAINER__
#define __HOTPLACE_SDK_BASE_BASIC_NOSTD_CONTAINER__

#include <deque>
#include <functional>
#include <map>
#include <sdk/base/error.hpp>
#include <sdk/base/stl.hpp>
#include <sdk/base/stream/basic_stream.hpp>
//#include <sdk/base/stream/basic_stream.hpp>
#include <sdk/base/syntax.hpp>
#include <sdk/base/types.hpp>

namespace hotplace {

/**
* @remarks
* where 0 seek_begin, 1 seek_set, 2 seek_end
*/

template <typename container_t>
void for_each_const(const container_t& c, typename std::function<void(typename container_t::const_iterator, int)> f) {
if (c.size()) {
auto iter = c.begin();
f(iter++, 0);
while (c.end() != iter) {
f(iter++, 1);
}
f(c.end(), 2);
}
}

template <typename container_t>
void for_each(const container_t& c, typename std::function<void(typename container_t::const_iterator, int)> f) {
void for_each(container_t& c, typename std::function<void(typename container_t::iterator, int)> f) {
if (c.size()) {
auto iter = c.begin();
f(iter++, 0);
Expand All @@ -36,20 +50,22 @@ void for_each(const container_t& c, typename std::function<void(typename contain
}

template <typename container_t, typename stream_type>
void print(const container_t& c, stream_type& s) {
for_each<container_t>(c, [&](typename container_t::const_iterator iter, int where) -> void {
void print(const container_t& c, stream_type& s, const std::string& mark_prologue = "[", const std::string& mark_delimiter = ", ",
const std::string& mark_epilogue = "]") {
auto func = [&](typename container_t::const_iterator iter, int where) -> void {
switch (where) {
case 0:
s << "[" << *iter;
s << mark_prologue << *iter;
break;
case 1:
s << ", " << *iter;
s << mark_delimiter << *iter;
break;
case 2:
s << "]";
s << mark_epilogue;
break;
}
});
};
for_each_const<container_t>(c, func);
}

} // namespace hotplace
Expand Down
44 changes: 44 additions & 0 deletions sdk/base/basic/nostd/exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab smarttab : */
/**
* @file {file}
* @author Soo Han, Kim (princeb612.kr@gmail.com)
* @desc
*
* Revision History
* Date Name Description
*
*/

#ifndef __HOTPLACE_SDK_BASE_BASIC_EXCEPTION__
#define __HOTPLACE_SDK_BASE_BASIC_EXCEPTION__

#include <sdk/base/error.hpp>
#include <sdk/base/stl.hpp>
#include <sdk/base/syntax.hpp>
#include <sdk/base/types.hpp>

namespace hotplace {

class exception {
private:
errorcode_t _errorcode;
std::string _desc;

public:
exception(errorcode_t err) { _errorcode = err; }
exception(errorcode_t err, const std::string& desc) : _errorcode(err), _desc(desc) {}
exception(const exception& rhs) : _errorcode(rhs._errorcode), _desc(rhs._desc) {}
exception(exception&& rhs) : _errorcode(rhs._errorcode), _desc(std::move(rhs._desc)) {}

errorcode_t get_errorcode() const { return _errorcode; }
std::string get_error_message() const {
std::string msg;
error_advisor::get_instance()->error_message(_errorcode, msg);
return msg;
}
std::string get_description() const { return _desc; }
};

} // namespace hotplace

#endif
Loading

0 comments on commit 62bbf5a

Please sign in to comment.