Skip to content

Commit 62796bf

Browse files
authored
Merge pull request #447 from fasiondog/feature/k
feat(KData): KData新增getKData方法获取另外的KData,减少未预加载数据读取
2 parents 36de18f + 3ed4579 commit 62796bf

29 files changed

+601
-219
lines changed

hikyuu_cpp/hikyuu/Block.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,29 @@ class HKU_API Block {
4343
return iter;
4444
}
4545

46-
bool isNull() const {
46+
bool isNull() const noexcept {
4747
return !m_data;
4848
}
4949

50-
uint64_t id() const {
50+
uint64_t id() const noexcept {
5151
return m_data ? (uint64_t)m_data.get() : 0;
5252
}
5353

54-
bool operator==(const Block& blk) const {
54+
bool operator==(const Block& blk) const noexcept {
5555
return m_data == blk.m_data;
5656
}
5757

58-
bool operator!=(const Block& blk) const {
58+
bool operator!=(const Block& blk) const noexcept {
5959
return m_data != blk.m_data;
6060
}
6161

6262
/** 获取板块类别 */
63-
string category() const {
63+
string category() const noexcept {
6464
return m_data ? m_data->m_category : "";
6565
}
6666

6767
/** 获取板块名称 */
68-
string name() const {
68+
string name() const noexcept {
6969
return m_data ? m_data->m_name : "";
7070
}
7171

@@ -130,12 +130,12 @@ class HKU_API Block {
130130
bool remove(const Stock& stock);
131131

132132
/** 包含的证券数量 */
133-
size_t size() const {
133+
size_t size() const noexcept {
134134
return m_data ? m_data->m_stockDict.size() : 0;
135135
}
136136

137137
/** 是否为空 */
138-
bool empty() const {
138+
bool empty() const noexcept {
139139
return size() ? false : true;
140140
}
141141

@@ -146,7 +146,7 @@ class HKU_API Block {
146146
}
147147

148148
/** 获取对应的指数,可能为空 Stock */
149-
Stock getIndexStock() const {
149+
Stock getIndexStock() const noexcept {
150150
return m_data ? m_data->m_indexStock : Stock();
151151
}
152152

hikyuu_cpp/hikyuu/HistoryFinanceInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
namespace hku {
1111

12-
HistoryFinanceInfo& HistoryFinanceInfo::operator=(const HistoryFinanceInfo& other) {
12+
HistoryFinanceInfo& HistoryFinanceInfo::operator=(const HistoryFinanceInfo& other) noexcept {
1313
HKU_IF_RETURN(this == &other, *this);
1414
fileDate = other.fileDate;
1515
reportDate = other.reportDate;
1616
values = other.values;
1717
return *this;
1818
}
1919

20-
HistoryFinanceInfo& HistoryFinanceInfo::operator=(HistoryFinanceInfo&& other) {
20+
HistoryFinanceInfo& HistoryFinanceInfo::operator=(HistoryFinanceInfo&& other) noexcept {
2121
HKU_IF_RETURN(this == &other, *this);
2222
fileDate = std::move(other.fileDate);
2323
reportDate = std::move(other.reportDate);

hikyuu_cpp/hikyuu/HistoryFinanceInfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ struct HKU_API HistoryFinanceInfo {
2323

2424
HistoryFinanceInfo() = default;
2525
HistoryFinanceInfo(const HistoryFinanceInfo&) = default;
26-
HistoryFinanceInfo(HistoryFinanceInfo&& rv)
26+
HistoryFinanceInfo(HistoryFinanceInfo&& rv) noexcept
2727
: fileDate(std::move(rv.fileDate)),
2828
reportDate(std::move(rv.reportDate)),
2929
values(std::move(rv.values)) {}
3030

31-
HistoryFinanceInfo& operator=(const HistoryFinanceInfo&);
32-
HistoryFinanceInfo& operator=(HistoryFinanceInfo&&);
31+
HistoryFinanceInfo& operator=(const HistoryFinanceInfo&) noexcept;
32+
HistoryFinanceInfo& operator=(HistoryFinanceInfo&&) noexcept;
3333
};
3434

3535
} // namespace hku

hikyuu_cpp/hikyuu/KData.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,39 @@ void KData::tocsv(const string& filename) {
9494
}
9595

9696
KData KData::getKData(const Datetime& start, const Datetime& end) const {
97+
const auto& self_query = getQuery();
98+
return getKData(KQueryByDate(start, end, self_query.kType(), self_query.recoverType()));
99+
}
100+
101+
KData KData::getKData(const KQuery& query) const {
97102
KData ret;
98103
const Stock& stk = getStock();
99104
HKU_IF_RETURN(stk.isNull(), ret);
100105

101-
const KQuery& query = getQuery();
102-
ret = KData(stk, KQueryByDate(start, end, query.kType(), query.recoverType()));
106+
auto* p = dynamic_cast<KDataSharedBufferImp*>(m_imp.get());
107+
if (p != nullptr) {
108+
ret = KData(stk, query);
109+
return ret;
110+
}
111+
112+
const auto& self_query = getQuery();
113+
if (empty() || self_query.recoverType() != KQuery::NO_RECOVER ||
114+
query.kType() != self_query.kType()) {
115+
ret = KData(stk, query);
116+
return ret;
117+
}
118+
119+
if (query == self_query) {
120+
ret.m_imp = m_imp;
121+
return ret;
122+
}
123+
124+
auto imp = m_imp->getOtherFromSelf(query);
125+
ret.m_imp = std::move(imp);
103126
return ret;
104127
}
105128

106-
KData KData::getKData(int64_t start, int64_t end) const {
129+
KData KData::getSubKData(int64_t start, int64_t end) const {
107130
int64_t total = static_cast<int64_t>(size());
108131
size_t startix, endix;
109132
if (start < 0) {

hikyuu_cpp/hikyuu/KData.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ class HKU_API KData {
3333
KData(KData&&);
3434
KData& operator=(KData&&);
3535

36-
size_t size() const;
37-
bool empty() const;
36+
size_t size() const noexcept;
37+
bool empty() const noexcept;
3838

3939
bool operator==(const KData&) const;
4040
bool operator!=(const KData&) const;
4141

4242
DatetimeList getDatetimeList() const;
4343

4444
/** 获取指定位置的KRecord,未作越界检查 */
45-
const KRecord& getKRecord(size_t pos) const;
45+
const KRecord& getKRecord(size_t pos) const noexcept;
4646

4747
/** 按日期查询KRecord */
48-
const KRecord& getKRecord(Datetime datetime) const;
48+
const KRecord& getKRecord(Datetime datetime) const noexcept;
4949

5050
/** 同getKRecord @see getKRecord */
51-
const KRecord& operator[](size_t pos) const {
51+
const KRecord& operator[](size_t pos) const noexcept {
5252
return getKRecord(pos);
5353
}
5454

@@ -68,6 +68,8 @@ class HKU_API KData {
6868
*/
6969
KData getKData(const Datetime& start, const Datetime& end) const;
7070

71+
KData getKData(const KQuery& query) const;
72+
7173
/**
7274
* 获取相同时间范围内的其他类型K线数据,如日线下对应的分钟线数据
7375
* @param ktype
@@ -81,7 +83,7 @@ class HKU_API KData {
8183
* @param end 结束索引
8284
* @return KData
8385
*/
84-
KData getKData(int64_t start, int64_t end = Null<int64_t>()) const;
86+
KData getSubKData(int64_t start, int64_t end = Null<int64_t>()) const;
8587

8688
/**
8789
* 特殊用途!谨慎!按当前K线范围,获取指定日期范围的其他类型的按日期查询的 Query 条件
@@ -99,7 +101,7 @@ class HKU_API KData {
99101
const KQuery::KType& ktype) const;
100102

101103
/** 按日期查询对应的索引位置,注:是 KData 中的位置,不是在 Stock 中原始K记录的位置 */
102-
size_t getPos(const Datetime& datetime) const;
104+
size_t getPos(const Datetime& datetime) const noexcept;
103105

104106
/** 按日期获取在原始 K 线记录中的位置 */
105107
size_t getPosInStock(Datetime datetime) const;
@@ -272,15 +274,13 @@ inline KData::KData(KData&& x) : m_imp(std::move(x.m_imp)) {
272274
}
273275

274276
inline KData& KData::operator=(const KData& x) {
275-
if (this == &x)
276-
return *this;
277+
HKU_IF_RETURN(this == &x, *this);
277278
m_imp = x.m_imp;
278279
return *this;
279280
}
280281

281282
inline KData& KData::operator=(KData&& x) {
282-
if (this == &x)
283-
return *this;
283+
HKU_IF_RETURN(this == &x, *this);
284284
m_imp = std::move(x.m_imp);
285285
x.m_imp = get_null_kdata_imp();
286286
return *this;
@@ -290,24 +290,24 @@ inline DatetimeList KData::getDatetimeList() const {
290290
return m_imp->getDatetimeList();
291291
}
292292

293-
inline const KRecord& KData::getKRecord(size_t pos) const {
293+
inline const KRecord& KData::getKRecord(size_t pos) const noexcept {
294294
return m_imp->getKRecord(pos); // 不会抛出异常
295295
}
296296

297-
inline const KRecord& KData::getKRecord(Datetime datetime) const {
297+
inline const KRecord& KData::getKRecord(Datetime datetime) const noexcept {
298298
size_t pos = getPos(datetime);
299299
return pos != Null<size_t>() ? getKRecord(pos) : KRecord::NullKRecord;
300300
}
301301

302-
inline size_t KData::getPos(const Datetime& datetime) const {
302+
inline size_t KData::getPos(const Datetime& datetime) const noexcept {
303303
return m_imp->getPos(datetime);
304304
}
305305

306-
inline size_t KData::size() const {
306+
inline size_t KData::size() const noexcept {
307307
return m_imp->size();
308308
}
309309

310-
inline bool KData::empty() const {
310+
inline bool KData::empty() const noexcept {
311311
return m_imp->empty();
312312
}
313313

hikyuu_cpp/hikyuu/KDataImp.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace hku {
1313

14-
class HKU_API KDataImp {
14+
class HKU_API KDataImp : public enable_shared_from_this<KDataImp> {
1515
public:
1616
KDataImp() = default;
1717
KDataImp(const Stock& stock, const KQuery& query);
@@ -25,11 +25,11 @@ class HKU_API KDataImp {
2525
return m_stock;
2626
}
2727

28-
virtual bool empty() const {
28+
virtual bool empty() const noexcept {
2929
return true;
3030
}
3131

32-
virtual size_t size() const {
32+
virtual size_t size() const noexcept {
3333
return 0;
3434
}
3535

@@ -45,11 +45,11 @@ class HKU_API KDataImp {
4545
return 0;
4646
}
4747

48-
virtual size_t getPos(const Datetime& datetime) const {
48+
virtual size_t getPos(const Datetime& datetime) const noexcept {
4949
return Null<size_t>();
5050
}
5151

52-
virtual const KRecord& getKRecord(size_t pos) const {
52+
virtual const KRecord& getKRecord(size_t pos) const noexcept {
5353
return KRecord::NullKRecord;
5454
}
5555

@@ -73,6 +73,11 @@ class HKU_API KDataImp {
7373
return DatetimeList();
7474
}
7575

76+
typedef shared_ptr<KDataImp> KDataImpPtr;
77+
virtual KDataImpPtr getOtherFromSelf(const KQuery& query) const {
78+
return std::make_shared<KDataImp>(m_stock, query);
79+
}
80+
7681
protected:
7782
KQuery m_query;
7883
Stock m_stock;

0 commit comments

Comments
 (0)