From 52049cae6a0678dd497df4e6a965dae8e2245aa3 Mon Sep 17 00:00:00 2001 From: mohsen Date: Sat, 13 Jan 2018 18:26:44 +0330 Subject: [PATCH] add high speed circular list add test scenario --- .vscode/settings.json | 26 ++++++++++++++++++ circular_list.h | 61 +++++++++++++++++++++++++++++++++++++++++++ main.cpp | 31 ++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 circular_list.h create mode 100644 main.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8c8b8c0 --- /dev/null +++ b/.vscode/settings.json @@ -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" + }, +} \ No newline at end of file diff --git a/circular_list.h b/circular_list.h new file mode 100644 index 0000000..0b95639 --- /dev/null +++ b/circular_list.h @@ -0,0 +1,61 @@ +#ifndef CIRCULAR_LIST_H +#define CIRCULAR_LIST_H + +template +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 \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0852b40 --- /dev/null +++ b/main.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#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 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(end - begin).count() + << endl; +}