Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
vector: dslings + docs (#7)
Browse files Browse the repository at this point in the history
* vector: dslings

* vector: base impl & docs

* update
  • Loading branch information
Sunrisepeak authored May 7, 2024
1 parent 12b72a5 commit 3dc38b3
Show file tree
Hide file tree
Showing 17 changed files with 1,072 additions and 71 deletions.
80 changes: 73 additions & 7 deletions common/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,64 @@
}

#define D2DS_WAIT HONLY_LOGW("Delete the D2DS_WAIT to continue...");
#define D2DS_RETURN HONLY_LOGW("Delete the D2DS_RETURN to continue..."); return 0;

#define D2DS_SELF_ASSIGNMENT_CHECKER if (this == &dsObj) return *this;

namespace d2ds {

struct DefaultAllocator {

static void * allocate(int bytes) {
allocate_counter()++;
if (debug())
HONLY_LOGI("DefaultAllocator: try to allocate %d bytes", bytes);
return malloc(bytes);
}

static void deallocate(void *addr, int bytes) {
deallocate_counter()++;
if (debug())
HONLY_LOGI("DefaultAllocator: free addr %p, bytes %d", addr, bytes);
assert(addr != nullptr);
free(addr);
}

public: // config & status
static bool & debug() {
static bool debugFlag = false;
return debugFlag;
}

static int & allocate_counter() {
static int cnt = 0;
return cnt;
}

static int & deallocate_counter() {
static int cnt = 0;
return cnt;
}

static void clear_status() {
allocate_counter() = 0;
deallocate_counter() = 0;
}

};

class BigFiveTest {
public:

struct Obj {
Obj(int data_ = 0) : data { data_ } { get_test_data_e().mDestructor++; }

Obj(const Obj &obj) { get_test_data_e().mCopyConstructor = true; }
Obj(const Obj &obj) {
get_test_data_e().mDestructor++;
get_test_data_e().mCopyConstructor = true;
}
Obj & operator=(const Obj &obj) {
get_test_data_e().mDestructor++;
get_test_data_e().mCopyAssignment = true;
if (this == &obj) get_test_data_e().mSelfAssignment = true;
return *this;
Expand All @@ -66,40 +111,61 @@ class BigFiveTest {
public: // checker
static bool destructor(bool enableInfo = false) {
if (enableInfo)
HONLY_LOGI("checker -> %d == 0", get_test_data_e().mDestructor);
HONLY_LOGD("checker -> %d == 0", get_test_data_e().mDestructor);
return get_test_data_e().mDestructor == 0;
}

static bool copy_constructor(bool enableInfo = false) {
if (enableInfo)
HONLY_LOGI("checker -> %d", get_test_data_e().mCopyConstructor);
HONLY_LOGD("checker -> %d", get_test_data_e().mCopyConstructor);
return get_test_data_e().mCopyConstructor;
}

static bool copy_assignment(bool enableInfo = false) {
if (enableInfo)
HONLY_LOGI("checker -> %d", get_test_data_e().mCopyAssignment);
HONLY_LOGD("checker -> %d", get_test_data_e().mCopyAssignment);
return get_test_data_e().mCopyAssignment;
}

static bool move_constructor(bool enableInfo = false) {
if (enableInfo)
HONLY_LOGI("checker -> %d", get_test_data_e().mMoveConstructor);
HONLY_LOGD("checker -> %d", get_test_data_e().mMoveConstructor);
return get_test_data_e().mMoveConstructor;
}

static bool move_assignment(bool enableInfo = false) {
if (enableInfo)
HONLY_LOGI("checker -> %d", get_test_data_e().mMoveAssignment);
HONLY_LOGD("checker -> %d", get_test_data_e().mMoveAssignment);
return get_test_data_e().mMoveAssignment;
}

static bool self_assignment(bool enableInfo = false) {
if (enableInfo)
HONLY_LOGI("checker -> %d", get_test_data_e().mMoveAssignment);
HONLY_LOGD("checker -> %d", get_test_data_e().mMoveAssignment);
return get_test_data_e().mSelfAssignment == false;
}

public:
static bool copy_constructor_pass() {
get_test_data_e().mCopyConstructor = true;
}

static bool copy_assignment_pass() {
get_test_data_e().mCopyAssignment = true;
}

static bool move_constructor_pass() {
get_test_data_e().mMoveConstructor = true;
}

static bool move_assignment_pass() {
get_test_data_e().mMoveAssignment = true;
}

static bool self_assignment_pass() {
get_test_data_e().mSelfAssignment = true;
}

public:

static void clear_status() {
Expand Down
8 changes: 4 additions & 4 deletions common/honly_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#define LOG_ENABLE true
#define _HONLY_LOG(fd, ...) if (LOG_ENABLE) { fprintf (fd, __VA_ARGS__); fprintf (fd, "\033[0m\n"); }
#define HONLY_LOGI_P(...) { fprintf (stdout, "\033[32m[%s LOGI]: - ", HONLY_LOGGER_TAG); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGI(...) { fprintf (stdout, "\033[32m[%s LOGI]: \t%s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGD(...) { fprintf (stdout, "\033[37m[%s LOGD]: \t%s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGW(...) { fprintf (stdout, "\033[33m[%s LOGW]: \t%s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGE(...) { fprintf (stderr, "\033[31m[%s LOGE]: \t%s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stderr, __VA_ARGS__); }
#define HONLY_LOGI(...) { fprintf (stdout, "\033[32m[%s LOGI]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGD(...) { fprintf (stdout, "\033[37m[%s LOGD]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGW(...) { fprintf (stdout, "\033[33m[%s LOGW]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stdout, __VA_ARGS__); }
#define HONLY_LOGE(...) { fprintf (stderr, "\033[31m[%s LOGE]: %s: %s:%d - ", HONLY_LOGGER_TAG, __func__, __FILE__, __LINE__); _HONLY_LOG(stderr, __VA_ARGS__); }

#endif
14 changes: 14 additions & 0 deletions exercises/array/Vector.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef VECTOR_HPP_D2DS
#define VECTOR_HPP_D2DS

#include <initializer_list>

#include "common/common.hpp"

namespace d2ds {
// show your code


}

#endif
4 changes: 2 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# 常用数据结构
- [数组](0_array.md)
- [定长数组Array](chapter_01_array.md)
- [动态数组Vector]()
- [动态数组Vector](chapter_02_vector.md)

- [链表]()
- [嵌入式单链表]()
Expand Down Expand Up @@ -37,7 +37,7 @@
- [C++基础](other/1_cpp_base.md)
- [范型编程](other/1_cpp_base.template.md)
- [语法糖 | 范围for循环](other/2_cpp_base.rangefor.md)
- [BigFive类行为控制]()
- [行为控制](other/3_cpp_base.bigfive.md)
- [深入理解new/delete]()
- [内存管理]()
- [设计模式]()
Expand Down
4 changes: 2 additions & 2 deletions src/chapter_01_array.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ public:
template <typename T, unsigned int N>
class Array {
public:
int * begin() {
T * begin() {
return mData_e;
}

int * end() {
T * end() {
return mData_e + N;
}
//...
Expand Down
Loading

0 comments on commit 3dc38b3

Please sign in to comment.