Skip to content

Commit 5a5a4c5

Browse files
committed
+ leetcode
1 parent 3738de8 commit 5a5a4c5

File tree

7 files changed

+1838
-0
lines changed

7 files changed

+1838
-0
lines changed

c++/my_shared_ptr.hh

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
3+
namespace wxg {
4+
5+
template <typename T>
6+
class my_shared_ptr {
7+
private:
8+
T *m_ptr = nullptr;
9+
10+
int *refcount = 0;
11+
12+
public:
13+
explicit my_shared_ptr(T *p) : m_ptr(p), refcount(new int(1);) {}
14+
my_shared_ptr(my_shared_ptr &&p)
15+
: m_ptr(p.m_ptr), refcount(new int(1)) noexcept {
16+
p.m_ptr = nullptr;
17+
}
18+
19+
my_shared_ptr(const my_shared_ptr &p) noexcept {
20+
m_ptr = p.m_ptr;
21+
refcount = p.refcount;
22+
++*refcount;
23+
}
24+
25+
my_shared_ptr &operator=(const my_shared_ptr &p) noexcept {
26+
if (m_ptr && --*refcount == 0) {
27+
delete m_ptr, delete refcount;
28+
}
29+
30+
m_ptr = p.m_ptr;
31+
refcount = p.refcount;
32+
++*refcount;
33+
}
34+
35+
inline my_shared_ptr<T> &operator=(my_shared_ptr &&p) noexcept {
36+
return operator=(p);
37+
}
38+
39+
explicit operator bool() const noexcept { return m_ptr; }
40+
41+
~my_shared_ptr() noexcept {
42+
if (--*refcount == 0 && m_ptr) {
43+
delete m_ptr;
44+
delete refcount;
45+
}
46+
}
47+
48+
inline T &operator*() const noexcept { return *get(); }
49+
inline T *operator->() const noexcept { return get(); }
50+
51+
inline T *get() const noexcept { return m_ptr; }
52+
53+
inline void swap(my_shared_ptr &p) noexcept { std::swap(p.m_ptr, m_ptr); }
54+
};
55+
} // namespace wxg

c++/my_string.hh

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <string.h>
2+
#include <iostream>
3+
4+
namespace wxg {
5+
6+
class my_string {
7+
private:
8+
char *data_;
9+
10+
public:
11+
my_string() : data_(new char[1]) { *data_ = '\0'; }
12+
my_string(const char *str) : data_(new char[strlen(str) + 1]) {
13+
strcpy(data_, str);
14+
}
15+
my_string(const my_string &rhs) : data_(new char[rhs.size() + 1]) {
16+
strcpy(data_, rhs.data_);
17+
}
18+
my_string(my_string &&rhs) : data_(rhs.data_) { rhs.data_ = nullptr; }
19+
~my_string() { delete[] data_; }
20+
21+
my_string &operator=(my_string rhs) {
22+
std::swap(data_, rhs.data_);
23+
return *this;
24+
}
25+
my_string &operator=(my_string &&rhs) {
26+
std::swap(data_, rhs.data_);
27+
return *this;
28+
}
29+
30+
inline size_t size() const { return strlen(data_) + 1; }
31+
inline size_t length() const { return size(); }
32+
inline char *c_str() const { return data_; }
33+
inline void swap(my_string &rhs) { std::swap(data_, rhs.data_); }
34+
};
35+
36+
} // namespace wxg

c++/my_unique_ptr.hh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
3+
namespace wxg {
4+
5+
template <typename T>
6+
class my_unique_ptr {
7+
private:
8+
T *m_ptr = nullptr;
9+
10+
public:
11+
explicit my_unique_ptr(T *p) : m_ptr(p) {}
12+
my_unique_ptr(my_unique_ptr &&p) : m_ptr(p.m_ptr) noexcept {
13+
p.m_ptr = nullptr;
14+
}
15+
16+
my_unique_ptr(const my_unique_ptr &) = delete;
17+
my_unique_ptr &operator=(const my_unique_ptr &) = delete;
18+
explicit operator bool() const noexcept { return m_ptr; }
19+
20+
~my_unique_ptr() {
21+
if (m_ptr) delete m_ptr;
22+
}
23+
24+
inline my_unique_ptr<T> &operator=(my_unique_ptr &&p) noexcept {
25+
std::swap(*this, p);
26+
return *this;
27+
}
28+
29+
inline T &operator*() const { return *get(); }
30+
inline T *operator->() const { return get(); }
31+
32+
inline void reset(T *q = nullptr) noexcept {
33+
if (q != m_ptr) {
34+
if (m_ptr) delete m_ptr;
35+
m_ptr = q;
36+
}
37+
}
38+
39+
inline T *release() noexcept {
40+
T *res = m_ptr;
41+
m_ptr = nullptr;
42+
return res;
43+
}
44+
45+
inline T *get() const noexcept { return m_ptr; }
46+
47+
inline void swap(my_unique_ptr &p) noexcept { std::swap(p.m_ptr, m_ptr); }
48+
};
49+
} // namespace wxg

c++/rw_lock.hh

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <condition_variable>
2+
#include <mutex>
3+
4+
using Lock = std::unique_lock<std::mutex>;
5+
6+
/**
7+
* implement with unique_lock and condition_variable
8+
* read first
9+
*/
10+
class readwrite_lock_1 {
11+
public:
12+
readwrite_lock_1() : stat(0) {}
13+
14+
void readLock() {
15+
Lock lock(mutex);
16+
while (stat < 0) cv.wait(lock);
17+
++stat;
18+
}
19+
20+
void readUnlock() {
21+
Lock lock(mutex);
22+
if (--stat == 0) cv.notify_one(); // 叫醒一个等待的写操作
23+
}
24+
25+
void writeLock() {
26+
Lock lock(mutex);
27+
while (stat != 0) cv.wait(lock);
28+
stat = -1;
29+
}
30+
31+
void writeUnlock() {
32+
Lock lock(mutex);
33+
stat = 0;
34+
cv.notify_all(); // 叫醒所有等待的读和写操作
35+
}
36+
37+
private:
38+
std::mutex mutex;
39+
std::condition_variable cv;
40+
int stat; // == 0 无锁;> 0 已加读锁个数;< 0 已加写锁
41+
};
42+
43+
/**
44+
* implement with two mutex
45+
* read first
46+
*/
47+
class readwrite_lock_2 {
48+
public:
49+
readwrite_lock_2() : read_cnt(0) {}
50+
51+
void readLock() {
52+
read_mtx.lock();
53+
if (++read_cnt == 1) write_mtx.lock();
54+
55+
read_mtx.unlock();
56+
}
57+
58+
void readUnlock() {
59+
read_mtx.lock();
60+
if (--read_cnt == 0) write_mtx.unlock();
61+
62+
read_mtx.unlock();
63+
}
64+
65+
void writeLock() { write_mtx.lock(); }
66+
void writeUnlock() { write_mtx.unlock(); }
67+
68+
private:
69+
std::mutex read_mtx;
70+
std::mutex write_mtx;
71+
int read_cnt; // 已加读锁个数
72+
};
73+
74+
/**
75+
* implement with mutex and condition_variable
76+
* write first
77+
*/
78+
class readwrite_lock_3 {
79+
private:
80+
std::mutex mutex;
81+
std::condition_variable cv;
82+
int rd_cnt; //等待读的数量
83+
int wr_cnt; //等待写的数量
84+
85+
public:
86+
readwrite_lock_3() : rd_cnt(0), wr_cnt(0) {}
87+
88+
void readLock() {
89+
Lock lock(mutex);
90+
++rd_cnt;
91+
while (wr_cnt > 0) cv.wait(lock);
92+
}
93+
94+
void readUnlock() {
95+
Lock lock(mutex);
96+
--rd_cnt;
97+
if (rd_cnt == 0) cv.notify_all();
98+
}
99+
100+
void writeLock() {
101+
Lock lock(mutex);
102+
++wr_cnt;
103+
while (wr_cnt + rd_cnt >= 2) cv.wait(lock);
104+
}
105+
106+
void writerUnlock() {
107+
Lock lock(mutex);
108+
--wr_cnt;
109+
if (wr_cnt == 0) cv.notify_all();
110+
}
111+
};

0 commit comments

Comments
 (0)