Skip to content

Commit d2a996e

Browse files
authored
Add files via upload
1 parent f1de75f commit d2a996e

File tree

4 files changed

+125
-44
lines changed

4 files changed

+125
-44
lines changed

wiz/STRINGBUILDER.H

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ namespace wiz {
2727
buffer[this->len] = '\0';
2828
buffer_first = buffer;
2929
}
30-
StringBuilder(const StringBuilder&) = delete;
31-
StringBuilder(StringBuilder&&) = delete;
30+
StringBuilder(const StringBuilder& other)
31+
{
32+
buffer = (char*)malloc(sizeof(char) * (other.capacity + 1)); // 1 for '\0'
33+
this->len = other.len;
34+
capacity = other.capacity;
35+
memcpy(buffer, other.buffer, other.capacity);
36+
buffer[this->len] = '\0';
37+
buffer_first = buffer;
38+
}
39+
3240
StringBuilder& operator=(const StringBuilder& other)
3341
{
3442
if (buffer_first != nullptr) {
@@ -88,13 +96,14 @@ namespace wiz {
8896
this->len = this->len + len;
8997
}
9098
else {
91-
char* new_buffer = (char*)malloc(sizeof(char) * (this->len + len + 1));
99+
char* new_buffer = (char*)malloc(sizeof(char) * (2 * (this->len + len) + 1));
92100
memcpy(new_buffer, buffer, this->len);
93101
memcpy(new_buffer + this->len, cstr, len);
94102
new_buffer[this->len + len] = '\0';
95103
free(buffer_first);
96104
buffer = new_buffer;
97105
buffer_first = buffer;
106+
this->capacity = 2 * (this->len + len);
98107
this->len = this->len + len;
99108
}
100109
}
@@ -145,6 +154,15 @@ namespace wiz {
145154
{
146155
this->buffer[idx] = val;
147156
}
157+
158+
char& operator[](const int idx)
159+
{
160+
return this->buffer[idx];
161+
}
162+
const char& operator[](const int idx) const
163+
{
164+
return this->buffer[idx];
165+
}
148166
};
149167
}
150168

wiz/load_data.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,13 @@ namespace wiz {
555555
private:
556556
class LoadData2 {
557557
public:
558-
void operator()(vector<string>* strVec, NoneReserver* reserver, UserType* global)
558+
void operator()(StringBuilder* strVec, NoneReserver* reserver, UserType* global)
559559
{
560560
ArrayQueue<Token> aq;
561561

562-
//wiz::load_data::Utility::DoThread test(strVec, &aq);
562+
wiz::load_data::Utility::DoThread test(strVec, &aq);
563563

564-
//test(0, strVec->size() - 1);
564+
test();
565565

566566
_LoadData(aq, *reserver, *global);
567567
}
@@ -576,15 +576,15 @@ namespace wiz {
576576
inFile.close(); return false;
577577
}
578578
UserType globalTemp = global;
579-
vector<vector<string>> strVec(1);
579+
vector<StringBuilder> strVec(1, StringBuilder(128));
580580
int count = 0;
581581

582582
try {
583583

584584
while (wiz::load_data::Utility::Reserve3(inFile, strVec[count], 100000))
585585
{
586586
count++;
587-
strVec.push_back(vector<string>());
587+
strVec.push_back(StringBuilder(128));
588588
}
589589
strVec.pop_back();
590590
inFile.close();
@@ -622,6 +622,7 @@ namespace wiz {
622622
}
623623
}
624624
}
625+
getchar();
625626
}
626627
catch (Error e) { std::cout << e << endl; if(inFile.is_open()) inFile.close(); return false; }
627628
catch (const char* err) { std::cout << err << endl; if (inFile.is_open())inFile.close(); return false; }

wiz/load_data_types.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace wiz {
3434
Type(const Type& type)
3535
: name(type.name)
3636
{
37-
chk();
37+
//chk();
3838
}
3939
virtual ~Type() { }
4040
bool IsFail() const { // change body
@@ -47,13 +47,13 @@ namespace wiz {
4747
{
4848
this->name = name;
4949

50-
chk();
50+
//chk();
5151
}
5252
void SetName(string&& name)
5353
{
5454
this->name = name;
5555

56-
chk();
56+
//chk();
5757
}
5858
bool operator==(const Type& t) const {
5959
return name == t.name;

wiz/load_data_utility.h

Lines changed: 95 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -541,13 +541,13 @@ namespace wiz {
541541
class DoThread // need to rename!
542542
{
543543
private:
544-
string* strVec;
544+
StringBuilder* strVec;
545545
public:
546546
ArrayQueue<Token>* aq;
547547
//int strVecStart;
548548
//int strVecEnd;
549549
public:
550-
DoThread(string* strVec, ArrayQueue<Token>* aq) //, list<string>* aq)//, int strVecStart, int strVecEnd)
550+
DoThread(StringBuilder* strVec, ArrayQueue<Token>* aq) //, list<string>* aq)//, int strVecStart, int strVecEnd)
551551
: strVec(strVec), aq(aq) // , strVecStart(strVecStart), strVecEnd(strVecEnd)
552552
{
553553
//
@@ -561,11 +561,11 @@ namespace wiz {
561561
//while (tokenizer.hasMoreTokens()) {
562562
// aq.push(tokenizer.nextToken());
563563
//}
564-
string statement = *strVec;
564+
StringBuilder& statement = *strVec;
565565
int token_first = 0, token_last = 0; // idx of token in statement.
566566
int state = 0;
567567

568-
for (int i = 0; i < statement.size(); ++i) {
568+
for (int i = 0; i < statement.Size(); ++i) {
569569
if (0 == state && '\"' == statement[i]) {
570570
//token_last = i - 1;
571571
//if (token_last >= 0 && token_last - token_first + 1 > 0) {
@@ -580,68 +580,131 @@ namespace wiz {
580580
}
581581
else if (1 == state && '\"' == statement[i]) {
582582
state = 0; token_last = i;
583-
584-
//aq->emplace_back(statement.substr(token_first, token_last - token_first + 1));
585-
//token_first = i + 1;
586583
}
587584

588585
if (0 == state && '=' == statement[i]) {
589586
token_last = i - 1;
590587
if (token_last >= 0 && token_last - token_first + 1 > 0) {
591-
aq->push(Token(statement.substr(token_first, token_last - token_first + 1), false));
588+
statement.Divide(i);
589+
590+
aq->push(Token(string(statement.Str()), false));
591+
statement.LeftShift(i + 1);
592+
593+
aq->push(Token("=", false));
594+
token_first = 0;
595+
token_last = 0;
596+
i = -1;
597+
}
598+
else {
599+
aq->push(Token("=", false));
600+
statement.LeftShift(1);
601+
token_first = 0;
602+
i = -1;
592603
}
593-
aq->push(Token("=", false));
594-
token_first = i + 1;
595604
}
596605
else if (0 == state && isWhitespace(statement[i])) { // isspace ' ' \t \r \n , etc... ?
597606
token_last = i - 1;
598607
if (token_last >= 0 && token_last - token_first + 1 > 0) {
599-
aq->push(Token(statement.substr(token_first, token_last - token_first + 1), false));
608+
statement.Divide(i);
609+
610+
aq->push(Token(string(statement.Str()), false));
611+
612+
statement.LeftShift(i + 1);
613+
614+
token_first = 0;
615+
token_last = 0;
616+
i = -1;
617+
}
618+
else
619+
{
620+
statement.LeftShift(1);
621+
token_first = 0;
622+
i = -1;
600623
}
601-
token_first = i + 1;
602624
}
603625
else if (0 == state && '{' == statement[i]) {
604626
token_last = i - 1;
605627
if (token_last >= 0 && token_last - token_first + 1 > 0) {
606-
aq->push(Token(statement.substr(token_first, token_last - token_first + 1), false));
628+
statement.Divide(i);
629+
630+
aq->push(Token(string(statement.Str()), false));
631+
statement.LeftShift(i + 1);
632+
633+
aq->push(Token("{", false));
634+
token_first = 0;
635+
token_last = 0;
636+
i = -1;
637+
}
638+
else {
639+
aq->push(Token("{", false));
640+
statement.LeftShift(1);
641+
token_first = 0;
642+
i = -1;
607643
}
608-
aq->push(Token("{", false));
609-
token_first = i + 1;
610644
}
611645
else if (0 == state && '}' == statement[i]) {
612646
token_last = i - 1;
613647
if (token_last >= 0 && token_last - token_first + 1 > 0) {
614-
aq->push(Token(statement.substr(token_first, token_last - token_first + 1), false));
648+
statement.Divide(i);
649+
650+
651+
aq->push(Token(string(statement.Str()), false));
652+
statement.LeftShift(i + 1);
653+
654+
aq->push(Token("}", false));
655+
656+
token_first = 0;
657+
token_last = 0;
658+
i = -1;
659+
}
660+
else {
661+
aq->push(Token("}", false));
662+
663+
statement.LeftShift(1);
664+
token_first = 0;
665+
i = -1;
615666
}
616-
aq->push(Token("}", false));
617-
token_first = i + 1;
618667
}
619668

620669
if (0 == state && '#' == statement[i]) { // different from load_data_from_file
621670
token_last = i - 1;
622671
if (token_last >= 0 && token_last - token_first + 1 > 0) {
623-
aq->push(Token(statement.substr(token_first, token_last - token_first + 1), false));
672+
statement.Divide(i);
673+
aq->push(Token(string(statement.Str()), false));
674+
statement.LeftShift(i + 1);
675+
i = 0;
624676
}
625677
int j = 0;
626-
for (j = i; j < statement.size(); ++j) {
678+
for (j = i; j < statement.Size(); ++j) {
627679
if (statement[j] == '\n') // cf) '\r' ?
628680
{
629681
break;
630682
}
631683
}
632684
--j; // "before enter key" or "before end"
633-
685+
634686
if (j - i + 1 > 0) {
635-
aq->push(Token(statement.substr(i, j - i + 1), true));
687+
statement.Divide(j + 1);
688+
689+
aq->push(Token(string(statement.Str()), true));
690+
statement.LeftShift(j + 2);
691+
692+
token_first = 0;
693+
token_last = 0;
694+
i = -1;
695+
}
696+
else {
697+
statement.LeftShift(j + 2);
698+
token_first = 0;
699+
token_last = 0;
700+
i = -1;
636701
}
637-
token_first = j + 2;
638-
i = token_first - 1;
639702
}
640703
}
641704

642-
if (token_first < statement.size())
705+
if (token_first < statement.Size())
643706
{
644-
aq->push(Token(statement.substr(token_first), false));
707+
aq->push(Token(string(statement.Str()), false));
645708
}
646709
}
647710
}
@@ -656,7 +719,7 @@ namespace wiz {
656719
//}
657720
};
658721
public:
659-
static bool Reserve3(ifstream& inFile, vector<string>& strVecTemp, const int min_num = 1)
722+
static bool Reserve3(ifstream& inFile, StringBuilder& strVecTemp, const int min_num = 1)
660723
{
661724
int count = 0;
662725
string temp;
@@ -665,7 +728,8 @@ namespace wiz {
665728

666729
for (int i = 0; i < min_num && (getline(inFile, temp)); ++i) {
667730
if (temp.empty()) { continue; }
668-
strVecTemp.push_back(temp);
731+
strVecTemp.Append(temp.c_str(), temp.size());
732+
strVecTemp.AppendChar('\n');
669733
for (int j = 0; j < temp.size(); ++j) {
670734
if (temp[j] == '{') {
671735
brace++;
@@ -679,7 +743,7 @@ namespace wiz {
679743

680744
while (brace != 0 && getline(inFile, temp)) {
681745
if (temp.empty()) { continue; }
682-
strVecTemp.push_back(temp);
746+
strVecTemp.Append(temp.c_str(), temp.size());
683747
for (int j = 0; j < temp.size(); ++j) {
684748
if (temp[j] == '{') {
685749
brace++;
@@ -698,18 +762,16 @@ namespace wiz {
698762
{
699763
int count = 0;
700764
string temp;
701-
wiz::StringBuilder builder(1024 * num); //vector<string> strVecTemp;
765+
wiz::StringBuilder builder(128 * num); //vector<string> strVecTemp;
702766

703767
for (int i = 0; i < num && (getline(inFile, temp)); ++i) {
704768
if (temp.empty()) { continue; }
705769
builder.Append(temp.c_str(), temp.size());
706770
builder.AppendChar('\n');
707771
count++;
708772
}
709-
710-
string str(builder.Str());
711773

712-
DoThread doThread(&str, &aq);
774+
DoThread doThread(&builder, &aq);
713775

714776
doThread(); // (0, count - 1);
715777

0 commit comments

Comments
 (0)