Skip to content

Commit

Permalink
add high speed circular list
Browse files Browse the repository at this point in the history
add test scenario
  • Loading branch information
smzahraee committed Jan 13, 2018
1 parent d6f089f commit 52049ca
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .vscode/settings.json
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"
},
}
61 changes: 61 additions & 0 deletions circular_list.h
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
31 changes: 31 additions & 0 deletions main.cpp
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;
}

0 comments on commit 52049ca

Please sign in to comment.