sepehrmoghiseh
2 years ago
15 changed files with 688 additions and 64 deletions
@ -0,0 +1,212 @@ |
|||
#include <QFile> |
|||
#include <iostream> |
|||
|
|||
#include "LinkedList.h" |
|||
#include "Node.h" |
|||
|
|||
#define SENDER 4 |
|||
#define COMMAND 2 |
|||
#define RECEIVER 4 |
|||
#define TIME 11 |
|||
using namespace std; |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::qListLinked(Node* newNode, int count){ |
|||
if(count == 0) |
|||
{ |
|||
newNode->chainLen += 1; |
|||
_linkedList.append(newNode); |
|||
} |
|||
else |
|||
{ |
|||
Node* tmp = _linkedList.last(); |
|||
tmp->chainLen += 1; |
|||
while(tmp->next != NULL) |
|||
{ |
|||
tmp = tmp->next; |
|||
} |
|||
tmp->next = newNode; |
|||
newNode->pre = tmp; |
|||
} |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::pushBack(string sender, string receiver, string command, float time, int count) { |
|||
Node* newNode = new Node(); |
|||
newNode->sender = sender; |
|||
newNode->command = command; |
|||
newNode->receiver = receiver; |
|||
newNode->time = time; |
|||
newNode->chainLen = 0; |
|||
newNode->next = NULL; |
|||
newNode->pre = NULL; |
|||
|
|||
qListLinked(newNode, count); |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::qListOmit(string line){ |
|||
omitedLines.append(line); |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::addToOmitList(int where){ |
|||
omitedLines.insert(where + 1, "chain"); |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::removeOmitLast(){ //remove uncorrect format
|
|||
omitedLines.removeLast(); |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::addTime(float time){ //addTime to null blocks in chain
|
|||
Node* head = _linkedList.last(); |
|||
while(head->next != NULL) |
|||
{ |
|||
head = head->next; |
|||
} |
|||
if(head->time == NULL) |
|||
{ |
|||
head->time = time; |
|||
} |
|||
else |
|||
{ |
|||
while(head->time != NULL) |
|||
{ |
|||
head = head->pre; |
|||
} |
|||
head->time = time; |
|||
} |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::makeLinkedList(){ |
|||
string subLine; |
|||
string sender; |
|||
string command; |
|||
string receiver; |
|||
string time; |
|||
float numFloat; |
|||
string lastLine; |
|||
int count = 0; |
|||
for(int i = 0; i < omitedLines.length(); i++) |
|||
{ |
|||
if(omitedLines[i].find(">>>") != std::string::npos) //line with <<< init
|
|||
{ |
|||
subLine = omitedLines[i].substr(omitedLines[i].find(">") + SENDER); |
|||
sender = subLine.substr(0, subLine.find("|") - 1); |
|||
subLine = omitedLines[i].substr(omitedLines[i].find("|") + COMMAND); |
|||
command = subLine.substr(0, subLine.find("=") - 1); |
|||
subLine = omitedLines[i].substr(omitedLines[i].find("==>") + RECEIVER); |
|||
receiver = subLine.substr(0, subLine.find(",")); |
|||
|
|||
pushBack(sender, receiver, command, NULL, count); //add the line with null
|
|||
//time
|
|||
count++; |
|||
} |
|||
else if(omitedLines[i].find("<<<") != std::string::npos) |
|||
{ |
|||
subLine = |
|||
omitedLines[i].substr(omitedLines[i].find("exec time:") + TIME); |
|||
time = subLine.substr(0, subLine.find(",")); |
|||
numFloat = std::stof(time); |
|||
addTime(numFloat); |
|||
} |
|||
else if(omitedLines[i].find("chain") != std::string::npos) //add the time to the
|
|||
//last null time variable
|
|||
//in the chain
|
|||
{ |
|||
count = 0; |
|||
} |
|||
} |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::chainDetector(){ //a function for finding the chains P.S: chain can
|
|||
//have one block.
|
|||
int count = 0; |
|||
|
|||
for(int i = 0; i < omitedLines.length(); i++) |
|||
{ |
|||
if(omitedLines[i].find(">>>") != std::string::npos) |
|||
{ |
|||
count++; |
|||
} |
|||
if(omitedLines[i].find("<<<") != std::string::npos) |
|||
{ |
|||
count--; |
|||
} |
|||
if(omitedLines[i] == "chain") |
|||
{ |
|||
continue; |
|||
} |
|||
if(count == 0) |
|||
{ |
|||
addToOmitList(i); |
|||
} |
|||
} |
|||
if(omitedLines.last() != "chain") |
|||
{ |
|||
while(omitedLines.last() != "chain") |
|||
{ |
|||
removeOmitLast(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
void LinkedList::fileOpen(const QString& path){ //read the file & add them to a Qlist for easy
|
|||
//access
|
|||
fstream newFile; |
|||
newFile.open(path.toStdString(), ios::in); |
|||
if(newFile.is_open()) |
|||
{ |
|||
string line; |
|||
string subLine; |
|||
string secSubLine; |
|||
while(getline(newFile, line)) |
|||
{ |
|||
if(line.find(">>>") != std::string::npos) |
|||
{ |
|||
subLine = line.substr(line.find(">")); |
|||
secSubLine = subLine.substr(0, subLine.find(",")); |
|||
qListOmit(secSubLine); |
|||
} |
|||
else if(line.find("<<<") != std::string::npos) |
|||
{ |
|||
subLine = line.substr(line.find("<")); |
|||
secSubLine = subLine.substr(0, subLine.find(",")); |
|||
qListOmit(secSubLine); |
|||
} |
|||
} |
|||
} |
|||
|
|||
newFile.close(); |
|||
} |
|||
|
|||
float LinkedList::convertTime(float input) |
|||
{ |
|||
return input * 1000; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
QList<Node*> LinkedList::Initiate(const QString& path){ |
|||
fileOpen(path); |
|||
chainDetector(); |
|||
makeLinkedList(); |
|||
|
|||
return _linkedList; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
@ -1,4 +1,41 @@ |
|||
#ifndef LINKEDLIST_H |
|||
#define LINKEDLIST_H |
|||
#ifndef FILE_H |
|||
#define FILE_H |
|||
#include <string> |
|||
#include <fstream> |
|||
#include <iostream> |
|||
#include <QList> |
|||
using namespace std; |
|||
struct Node; |
|||
|
|||
#endif // LINKEDLIST_H
|
|||
class LinkedList |
|||
{ |
|||
private: |
|||
QList<Node*> _linkedList; |
|||
QList<string> omitedLines; |
|||
|
|||
Node* head; |
|||
void qListLinked(Node* newNode, int count); |
|||
|
|||
void pushBack(string sender, string receiver, string command, float time, int count); |
|||
|
|||
void qListOmit(string line); |
|||
|
|||
void addToOmitList(int where); |
|||
|
|||
void removeOmitLast(); |
|||
|
|||
void addTime(float time); |
|||
|
|||
void makeLinkedList(); |
|||
|
|||
void chainDetector(); |
|||
|
|||
void fileOpen(const QString& path); |
|||
|
|||
public: |
|||
QList<Node*> Initiate(const QString& path); |
|||
|
|||
float convertTime(float input); |
|||
}; |
|||
|
|||
#endif //FILE_H
|
|||
|
@ -1,4 +1,15 @@ |
|||
#ifndef NODE_H |
|||
#define NODE_H |
|||
#include <string> |
|||
struct Node { |
|||
std::string sender; |
|||
std::string receiver; |
|||
std::string command; |
|||
float time; |
|||
int chainLen; |
|||
|
|||
#endif // NODE_H
|
|||
Node* next; |
|||
Node* pre; |
|||
}; |
|||
|
|||
#endif //NODE_H
|
|||
|
@ -1,6 +1,321 @@ |
|||
#include <fstream> |
|||
#include <iostream> |
|||
#include <cstring> |
|||
#include <QtMath> |
|||
#include "SortedLinkedList.h" |
|||
|
|||
SortedLinkedList::SortedLinkedList() |
|||
#include "Node.h" |
|||
#include <cmath> |
|||
SortedLinkedList::SortedLinkedList(QList<Node*> linkedList) : |
|||
defaultLinkedList(linkedList) |
|||
{ |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
QList<Node*> SortedLinkedList::defaultLinkedListGetter(){ |
|||
return defaultLinkedList; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
QList<Node*> SortedLinkedList::timeSortingAsc(float start, float end){ |
|||
if(start > 0 && end == FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->time >= start) |
|||
{ |
|||
timeSortedLinkedListAsc.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else if(start > 0 && end < FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->time >= start && defaultLinkedList[i]->time <= end) |
|||
{ |
|||
timeSortedLinkedListAsc.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else if(start <= 0 && end < FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->time <= end) |
|||
{ |
|||
timeSortedLinkedListAsc.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
timeSortedLinkedListAsc = defaultLinkedList; |
|||
} |
|||
|
|||
if(!timeSortedLinkedListAsc.isEmpty()) |
|||
{ |
|||
for(int k = 1; k < timeSortedLinkedListAsc.length(); k++) |
|||
{ |
|||
Node* temp = timeSortedLinkedListAsc[k]; |
|||
int j = k - 1; |
|||
while(j >= 0 && temp->time <= timeSortedLinkedListAsc[j]->time) |
|||
{ |
|||
timeSortedLinkedListAsc[j + 1] = timeSortedLinkedListAsc[j]; |
|||
j = j - 1; |
|||
} |
|||
timeSortedLinkedListAsc[j + 1] = temp; |
|||
} |
|||
} |
|||
|
|||
return timeSortedLinkedListAsc; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
QList<Node*> SortedLinkedList::timeSortingDes(float start, float end){ |
|||
if(start > 0 && end == FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->time >= start) |
|||
{ |
|||
timeSortedLinkedListDes.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else if(start > 0 && end < FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->time >= start && defaultLinkedList[i]->time <= end) |
|||
{ |
|||
timeSortedLinkedListDes.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else if(start <= 0 && end < FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->time <= end) |
|||
{ |
|||
timeSortedLinkedListDes.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
timeSortedLinkedListDes = defaultLinkedList; |
|||
} |
|||
if(!timeSortedLinkedListDes.isEmpty()) |
|||
{ |
|||
for(int k = 1; k < timeSortedLinkedListDes.length(); k++) |
|||
{ |
|||
Node* temp = timeSortedLinkedListDes[k]; |
|||
int j = k - 1; |
|||
while(j >= 0 && temp->time >= timeSortedLinkedListDes[j]->time) |
|||
{ |
|||
timeSortedLinkedListDes[j + 1] = timeSortedLinkedListDes[j]; |
|||
j = j - 1; |
|||
} |
|||
timeSortedLinkedListDes[j + 1] = temp; |
|||
} |
|||
} |
|||
|
|||
return timeSortedLinkedListDes; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
QList<Node*> SortedLinkedList::timeSorting(int dir, float start, float end){ |
|||
switch(dir) |
|||
{ |
|||
case 0: |
|||
return timeSortingAsc(start, end); |
|||
|
|||
case 1: |
|||
return timeSortingDes(start, end); |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
QList<Node*> SortedLinkedList::chainLenSortingAsc(float start, float end){ |
|||
if(start > 0 && end == FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->chainLen >= start) |
|||
{ |
|||
chainLenSortedLinkedListAsc.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else if(start > 0 && end < FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->chainLen >= start && defaultLinkedList[i]->chainLen <= end) |
|||
{ |
|||
chainLenSortedLinkedListAsc.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else if(start <= 0 && end < FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->chainLen <= end) |
|||
{ |
|||
chainLenSortedLinkedListAsc.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
chainLenSortedLinkedListAsc = defaultLinkedList; |
|||
} |
|||
if(!chainLenSortedLinkedListAsc.isEmpty()) |
|||
{ |
|||
for(int k = 1; k < chainLenSortedLinkedListAsc.length(); k++) |
|||
{ |
|||
Node* temp = chainLenSortedLinkedListAsc[k]; |
|||
int j = k - 1; |
|||
while(j >= 0 && temp->chainLen <= chainLenSortedLinkedListAsc[j]->chainLen) |
|||
{ |
|||
chainLenSortedLinkedListAsc[j + 1] = chainLenSortedLinkedListAsc[j]; |
|||
j = j - 1; |
|||
} |
|||
chainLenSortedLinkedListAsc[j + 1] = temp; |
|||
} |
|||
} |
|||
|
|||
return chainLenSortedLinkedListAsc; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
QList<Node*> SortedLinkedList::chainLenSortingDes(float start, float end){ |
|||
if(start > 0 && end == FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->chainLen >= start) |
|||
{ |
|||
chainLenSortedLinkedListDes.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else if(start > 0 && end < FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->chainLen >= start && defaultLinkedList[i]->chainLen <= end) |
|||
{ |
|||
chainLenSortedLinkedListDes.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else if(start <= 0 && end < FP_INFINITE) |
|||
{ |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
if(defaultLinkedList[i]->chainLen <= end) |
|||
{ |
|||
chainLenSortedLinkedListDes.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
chainLenSortedLinkedListDes = defaultLinkedList; |
|||
} |
|||
if(!chainLenSortedLinkedListDes.isEmpty()) |
|||
{ |
|||
for(int k = 1; k < chainLenSortedLinkedListDes.length(); k++) |
|||
{ |
|||
Node* temp = chainLenSortedLinkedListDes[k]; |
|||
int j = k - 1; |
|||
while(j >= 0 && temp->chainLen >= chainLenSortedLinkedListDes[j]->chainLen) |
|||
{ |
|||
chainLenSortedLinkedListDes[j + 1] = chainLenSortedLinkedListDes[j]; |
|||
j = j - 1; |
|||
} |
|||
chainLenSortedLinkedListDes[j + 1] = temp; |
|||
} |
|||
} |
|||
|
|||
return chainLenSortedLinkedListDes; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
QList<Node*> SortedLinkedList::chainLenSorting(int dir, float start, float end){ |
|||
switch(dir) |
|||
{ |
|||
case 0: |
|||
return chainLenSortingAsc(start, end); |
|||
|
|||
case 1: |
|||
return chainLenSortingDes(start, end); |
|||
|
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
|||
QList<Node*> SortedLinkedList::senderFilter(string filterWord){ |
|||
transform(filterWord.begin(), filterWord.end(), filterWord.begin(), ::toupper); //uppercase
|
|||
string iterate; |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
iterate = defaultLinkedList[i]->sender; |
|||
transform(iterate.begin(), iterate.end(), iterate.begin(), ::toupper); //uppercase
|
|||
if(iterate.find(filterWord) == 0) |
|||
{ |
|||
senderFilterLinkedList.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
|
|||
return senderFilterLinkedList; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
QList<Node*> SortedLinkedList::receiverFilter(string filterWord){ |
|||
transform(filterWord.begin(), filterWord.end(), filterWord.begin(), ::toupper); //uppercase
|
|||
string iterate; |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
iterate = defaultLinkedList[i]->receiver; |
|||
transform(iterate.begin(), iterate.end(), iterate.begin(), ::toupper); //uppercase
|
|||
if(iterate.find(filterWord) == 0) |
|||
{ |
|||
receiverFilterLinkedList.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
|
|||
return receiverFilterLinkedList; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
QList<Node*> SortedLinkedList::commandFilter(string filterWord){ |
|||
transform(filterWord.begin(), filterWord.end(), filterWord.begin(), ::toupper); //uppercase
|
|||
string iterate; |
|||
for(int i = 0; i < defaultLinkedList.length(); i++) |
|||
{ |
|||
iterate = defaultLinkedList[i]->command; |
|||
transform(iterate.begin(), iterate.end(), iterate.begin(), ::toupper); //uppercase
|
|||
if(iterate.find(filterWord) == 0) |
|||
{ |
|||
commandFilterLinkedList.append(defaultLinkedList[i]); |
|||
} |
|||
} |
|||
|
|||
return commandFilterLinkedList; |
|||
} |
|||
|
|||
/*<============================================================================================================>*/ |
|||
|
@ -1,11 +1,44 @@ |
|||
#ifndef SORTEDLINKEDLIST_H |
|||
#define SORTEDLINKEDLIST_H |
|||
|
|||
#ifndef SORTLINKEDLIST_H |
|||
#define SORTLINKEDLIST_H |
|||
#include <fstream> |
|||
#include <iostream> |
|||
#include <QList> |
|||
#include <QtMath> |
|||
#include "LinkedList.h" |
|||
|
|||
class SortedLinkedList |
|||
{ |
|||
private: |
|||
QList<Node*> defaultLinkedList; |
|||
|
|||
QList<Node*> timeSortedLinkedListAsc; |
|||
QList<Node*> timeSortedLinkedListDes; |
|||
|
|||
QList<Node*> chainLenSortedLinkedListAsc; |
|||
QList<Node*> chainLenSortedLinkedListDes; |
|||
|
|||
QList<Node*> senderFilterLinkedList; |
|||
QList<Node*> receiverFilterLinkedList; |
|||
QList<Node*> commandFilterLinkedList; |
|||
|
|||
QList<Node*> timeSortingAsc(float start = 0, float end = FP_INFINITE); |
|||
QList<Node*> timeSortingDes(float start = 0, float end = FP_INFINITE); |
|||
|
|||
QList<Node*> chainLenSortingAsc(float start = 0, float end = FP_INFINITE); |
|||
QList<Node*> chainLenSortingDes(float start = 0, float end = FP_INFINITE); |
|||
|
|||
public: |
|||
SortedLinkedList(); |
|||
SortedLinkedList(QList<Node*> linkedList); |
|||
|
|||
QList<Node*> timeSorting(int dir = 0, float start = 0, float end = FP_INFINITE); |
|||
|
|||
QList<Node*> defaultLinkedListGetter(); |
|||
|
|||
QList<Node*> chainLenSorting(int dir = 0, float start = 0, float end = FP_INFINITE); |
|||
|
|||
QList<Node*> senderFilter(string filterWord); |
|||
QList<Node*> receiverFilter(string filterWord); |
|||
QList<Node*> commandFilter(string filterWord); |
|||
}; |
|||
|
|||
#endif // SORTEDLINKEDLIST_H
|
|||
#endif //SORTLINKEDLIST_H
|
|||
|
@ -1,6 +1,17 @@ |
|||
#include "LinkedListTest.h" |
|||
|
|||
#include <QTest> |
|||
#include "SortedLinkedList.h" |
|||
#include "Node.h" |
|||
LinkedListTest::LinkedListTest() |
|||
{ |
|||
} |
|||
|
|||
void LinkedListTest::test() |
|||
{ |
|||
LinkedList linkList; |
|||
auto l = linkList.Initiate("D:/git commands/App-Log-Reader/test.usl"); |
|||
SortedLinkedList t(l); |
|||
auto result = t.chainLenSorting(1, 2); |
|||
|
|||
QCOMPARE(result.last()->chainLen, 2); |
|||
} |
|||
|
@ -1,11 +1,16 @@ |
|||
#ifndef LINKEDLISTTEST_H |
|||
#define LINKEDLISTTEST_H |
|||
#include <QObject> |
|||
|
|||
|
|||
class LinkedListTest |
|||
class LinkedListTest : public QObject |
|||
{ |
|||
Q_OBJECT |
|||
|
|||
public: |
|||
LinkedListTest(); |
|||
|
|||
private slots: |
|||
void test(); |
|||
}; |
|||
|
|||
#endif // LINKEDLISTTEST_H
|
|||
#endif //LINKEDLISTTEST_H
|
|||
|
Loading…
Reference in new issue