-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test scenario
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"clang-format.executable": "clang-format-3.9", | ||
"clang-format.fallbackStyle": "Google", | ||
"files.associations": { | ||
"*.tcc": "cpp", | ||
"cctype": "cpp", | ||
"clocale": "cpp", | ||
"cstdint": "cpp", | ||
"cstdio": "cpp", | ||
"cstdlib": "cpp", | ||
"cwchar": "cpp", | ||
"cwctype": "cpp", | ||
"exception": "cpp", | ||
"initializer_list": "cpp", | ||
"iosfwd": "cpp", | ||
"iostream": "cpp", | ||
"istream": "cpp", | ||
"limits": "cpp", | ||
"new": "cpp", | ||
"ostream": "cpp", | ||
"streambuf": "cpp", | ||
"type_traits": "cpp", | ||
"typeinfo": "cpp", | ||
"chrono": "cpp" | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#ifndef CIRCULAR_LIST_H | ||
#define CIRCULAR_LIST_H | ||
|
||
template <class T> | ||
class CirculaList { | ||
public: | ||
explicit CirculaList(int size) : size_(size) { | ||
buf_ = new T[size_]; | ||
Clear(); | ||
} | ||
|
||
inline int Count() { return (head_ - tail_) & (size_ - 1); } | ||
|
||
inline int RemainingCapacity() { return (tail_ - (head_ + 1)) & (size_ - 1); } | ||
|
||
inline bool IsEmpty() { return head_ == tail_; } | ||
|
||
inline void Clear() { head_ = tail_ = 0; } | ||
|
||
inline bool HasFreeSpace() { return RemainingCapacity() > 0; } | ||
|
||
inline bool Push(T element) { | ||
if (HasFreeSpace()) { | ||
buf_[head_] = element; | ||
head_ = (head_ + 1) & (size_ - 1); | ||
return true; | ||
} else | ||
return false; | ||
} | ||
|
||
inline bool Pop(T &element) { | ||
if (IsEmpty()) | ||
return false; | ||
else { | ||
element = buf_[tail_]; | ||
tail_ = (tail_ + 1) & (size_ - 1); | ||
return true; | ||
} | ||
} | ||
inline bool Top(T &element) { | ||
if (IsEmpty()) | ||
return false; | ||
else { | ||
element = buf_[tail_]; | ||
} | ||
} | ||
|
||
inline bool At(int index, T &element) { | ||
if (Count() <= index) return false; | ||
element = buf_[(head_ + index) & (size_ - 1)]; | ||
return true; | ||
} | ||
|
||
private: | ||
T *buf_; | ||
int head_; | ||
int tail_; | ||
const int size_; | ||
}; | ||
|
||
#endif // !1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <chrono> | ||
#include <iostream> | ||
#include <string> | ||
#include "circular_list.h" | ||
|
||
#define MAX_SIZE 1048576 | ||
using namespace std; | ||
|
||
#define CASE_11 | ||
int main(int argc, char *[]) { | ||
chrono::steady_clock::time_point begin = chrono::steady_clock::now(); | ||
|
||
CirculaList<int> list(MAX_SIZE); | ||
for (int i = 0; i < MAX_SIZE; i++) list.Push(i); | ||
|
||
int b = 0; | ||
|
||
for (int i = MAX_SIZE - 1; i >= 0; i--) { | ||
list.At(i, b); | ||
// cout << std::to_string(b) << " "; | ||
} | ||
// cout << '\n'; | ||
|
||
for (int i = 0; i < MAX_SIZE; i++) list.Pop(b); | ||
|
||
chrono::steady_clock::time_point end = chrono::steady_clock::now(); | ||
|
||
cout << "Time difference = " | ||
<< chrono::duration_cast<chrono::nanoseconds>(end - begin).count() | ||
<< endl; | ||
} |