├── .gitignore ├── test ├── CMakeLists.txt └── ring_queue_test.cc ├── CMakeLists.txt ├── README.md ├── ring_queue.h └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10 FATAL_ERROR) 2 | 3 | add_executable(ring_queue_test 4 | ring_queue_test.cc 5 | ) 6 | 7 | target_link_libraries(ring_queue_test 8 | gtest 9 | pthread 10 | dl 11 | ) 12 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10 FATAL_ERROR) 2 | 3 | project(RING_LIST) 4 | 5 | set(CMAKE_CONFIGURATION_TYPES Release) 6 | set(CMAKE_BUILD_TYPE Release) 7 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++1y") 8 | set(CMAKE_INCLUDE_CURRENT_DIR TRUE) 9 | set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin") 10 | 11 | include_directories(${PROJECT_SOURCE_DIR}) 12 | 13 | add_subdirectory(test) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # container::RingQueue 2 | An ring priority queue for C++. 3 | 4 | ## Features 5 | * STL-like 6 | * Single-header implementation. Just drop it in your project. 7 | * Not thread-safe 8 | * C++11 implementation 9 | * Fully portable 10 | * Self-sort like std::set 11 | 12 | ## Basic use 13 | The entire queue's implementation is contained in one header `ring_list.h`. 14 | 15 | Simple example: 16 | ``` 17 | #include "ring_queue.h" 18 | 19 | container::RingQueue q; 20 | 21 | q.push(2); 22 | q.push(1); 23 | assert(q.front() == 1); 24 | 25 | q.pop(); 26 | q.clear(); 27 | 28 | ``` 29 | Description of methods: 30 | * `push(const T& value)` Insert an element into queue 31 | * `emplace(Args&&... args)` Insert an element into queue 32 | * `pop()` Remove the smallest element from header 33 | * `erase(const iterator& position)` Remove element in 'position' 34 | * `front()` The first element 35 | * `empty()` The queue is empty or not 36 | * `size()` The element number 37 | * `clear()` Clear queue 38 | * `begin()` The begin iterator 39 | * `end()` The end iterator 40 | 41 | ## Tests 42 | I've written quite a few unit tests. The tests depend on [googletest](https://github.com/google/googletest), you need install it firstly if you want to run the test. I run the test in windows10/ubuntu18-x64/ubuntu16-armv8, use valgrind check the test as well, There may still be bugs. If anyone is seeing buggy behaviour, I'd like to hear about it! Just open an issue on GitHub. -------------------------------------------------------------------------------- /ring_queue.h: -------------------------------------------------------------------------------- 1 | #ifndef RING_QUEUE_H_ 2 | #define RING_QUEUE_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace container { 10 | 11 | template class Allocator { 12 | public: 13 | Allocator() { 14 | chunk_size_ = (sizeof(T) + 7) & (~7); 15 | InitOnePool(); 16 | } 17 | ~Allocator() { DestroyAlloctor(); } 18 | 19 | T *GetNode() noexcept { 20 | T *res = nullptr; 21 | 22 | if (start_ == end_) { 23 | res = (T *)start_; 24 | 25 | if (InitOnePool() != 0) { 26 | return nullptr; 27 | } 28 | 29 | return res; 30 | } 31 | res = (T *)start_; 32 | start_ = start_->next; 33 | 34 | return res; 35 | } 36 | 37 | void PutNode(T *chunk) noexcept { 38 | end_->next = (Chunk *)chunk; 39 | end_ = (Chunk *)chunk; 40 | } 41 | 42 | private: 43 | union Chunk { 44 | union Chunk *next{nullptr}; 45 | char data[1]; 46 | }; 47 | 48 | static const int MAXPOOLNUM = 10; 49 | 50 | void *headers_[MAXPOOLNUM]; 51 | int pool_num_{0}; 52 | uint32_t chunk_size_{0}; 53 | Chunk *start_{nullptr}; 54 | Chunk *end_{nullptr}; 55 | 56 | int InitOnePool() noexcept { 57 | if (pool_num_ >= MAXPOOLNUM) { 58 | return -1; 59 | } 60 | 61 | Chunk *mem = (Chunk *)calloc(1, chunk_size_ * Num); 62 | if (mem == nullptr) { 63 | printf("memory init error\n"); 64 | exit(-1); 65 | } 66 | 67 | Chunk *cur = mem; 68 | Chunk *next = mem; 69 | for (int i = 1;; ++i) { 70 | cur = next; 71 | next = (Chunk *)((char *)next + chunk_size_); 72 | if (i == Num) { 73 | cur->next = nullptr; 74 | break; 75 | } else { 76 | cur->next = next; 77 | } 78 | } 79 | 80 | start_ = mem; 81 | end_ = cur; //(Chunk*)((char*)cur + sizeof(T)); 82 | headers_[pool_num_++] = mem; 83 | 84 | return 0; 85 | } 86 | 87 | void DestroyAlloctor() noexcept { 88 | for (int i = 0; i < pool_num_; ++i) { 89 | free(headers_[i]); 90 | } 91 | } 92 | }; 93 | 94 | template struct QueueNode { 95 | char data[sizeof(T)]; 96 | QueueNode *prev{nullptr}; 97 | QueueNode *next{nullptr}; 98 | }; 99 | 100 | template struct Iterator { 101 | using Self = Iterator; 102 | 103 | Iterator(QueueNode *node) : node_(node) {} 104 | 105 | T &operator*() const noexcept { return *(T *)node_->data; } 106 | 107 | T *operator->() const noexcept { return (T *)node_->data; } 108 | 109 | Self &operator++() noexcept { 110 | node_ = node_->next; 111 | return *this; 112 | } 113 | 114 | Self &operator++(int) noexcept { 115 | Self tmp = *this; 116 | node_ = node_->next; 117 | return tmp; 118 | } 119 | 120 | Self &operator--() noexcept { 121 | node_ = node_->prev; 122 | return *this; 123 | } 124 | 125 | Self &operator--(int) noexcept { 126 | Self tmp = *this; 127 | node_ = node_->prev; 128 | return tmp; 129 | } 130 | 131 | bool operator==(const Self &other) const noexcept { 132 | return node_ == other.node_; 133 | } 134 | 135 | bool operator!=(const Self &other) const noexcept { 136 | return node_ != other.node_; 137 | } 138 | 139 | QueueNode *node_{nullptr}; 140 | }; 141 | 142 | template > 143 | class RingQueue { 144 | public: 145 | using iterator = Iterator; 146 | using Node = QueueNode; 147 | 148 | public: 149 | RingQueue() { Init(); } 150 | ~RingQueue() { clear(); } 151 | 152 | inline bool empty() const { return dummy_->next == dummy_; } 153 | 154 | uint32_t size() { return length_; } 155 | 156 | iterator begin() noexcept { return iterator(dummy_->next); } 157 | 158 | iterator end() noexcept { return iterator(dummy_); } 159 | 160 | T &front() const noexcept { return *(T *)dummy_->next->data; } 161 | 162 | void push(const T &value) { 163 | // 'value' is biger than all element in the queue mostly, hence, we should 164 | // find the location from tail, and this runs well in 165 | // x64(Intel i7-9700K) 166 | 167 | // 'node' is the first one smaller than 'value' in inverse order 168 | Node *node = nullptr; 169 | for (node = dummy_->prev; node != dummy_; node = node->prev) { 170 | if (value_compare_(*(T *)node->data, value)) { 171 | break; 172 | } 173 | } 174 | 175 | Node *new_node = alloc_.GetNode(); 176 | _construct((T *)new_node->data, value); 177 | 178 | node->next->prev = new_node; 179 | new_node->next = node->next; 180 | new_node->prev = node; 181 | node->next = new_node; 182 | length_++; 183 | 184 | // if queue is full, delete head node 185 | if (length_ > Size) { 186 | Node *head = dummy_->next; 187 | _destroy((T *)head->data); 188 | 189 | dummy_->next = head->next; 190 | head->next->prev = dummy_; 191 | length_--; 192 | 193 | alloc_.PutNode(head); 194 | } 195 | } 196 | 197 | template void emplace(Args &&... args) { 198 | Node *new_node = alloc_.GetNode(); 199 | _construct((T *)new_node->data, std::forward(args)...); 200 | 201 | Node *node = nullptr; 202 | for (node = dummy_->prev; node != dummy_; node = node->prev) { 203 | if (value_compare_(*(T *)node->data, *(T *)new_node->data)) { 204 | break; 205 | } 206 | } 207 | 208 | node->next->prev = new_node; 209 | new_node->next = node->next; 210 | new_node->prev = node; 211 | node->next = new_node; 212 | length_++; 213 | 214 | // if queue is full, delete head node 215 | if (length_ > Size) { 216 | Node *head = dummy_->next; 217 | _destroy((T *)head->data); 218 | 219 | dummy_->next = head->next; 220 | head->next->prev = dummy_; 221 | length_--; 222 | 223 | alloc_.PutNode(head); 224 | } 225 | } 226 | 227 | // pop front 228 | void pop() noexcept { 229 | if (length_ > 0) { 230 | Node *head = dummy_->next; 231 | _destroy((T *)head->data); 232 | 233 | dummy_->next = head->next; 234 | head->next->prev = dummy_; 235 | length_--; 236 | 237 | alloc_.PutNode(head); 238 | } 239 | } 240 | 241 | iterator erase(const iterator &position) noexcept { 242 | iterator next(position.node_->next); 243 | 244 | if (length_ > 0) { 245 | position.node_->next->prev = position.node_->prev; 246 | position.node_->prev->next = position.node_->next; 247 | length_--; 248 | 249 | _destroy((T *)position.node_->data); 250 | alloc_.PutNode(position.node_); 251 | } 252 | 253 | return next; 254 | } 255 | 256 | void clear() noexcept { 257 | for (Node *tmp = dummy_->next; tmp != dummy_; tmp = tmp->next) { 258 | _destroy((T *)tmp->data); 259 | alloc_.PutNode(tmp); 260 | } 261 | 262 | dummy_->next = dummy_; 263 | dummy_->prev = dummy_; 264 | length_ = 0; 265 | } 266 | 267 | private: 268 | Compare value_compare_; 269 | Allocator alloc_; 270 | Node *dummy_{nullptr}; 271 | int length_{0}; 272 | 273 | private: 274 | void Init() { 275 | dummy_ = alloc_.GetNode(); 276 | 277 | dummy_->next = dummy_; 278 | dummy_->prev = dummy_; 279 | } 280 | 281 | template inline void _construct(T *_p, Args &&... _args) { 282 | ::new (static_cast(_p)) T(std::forward(_args)...); 283 | } 284 | 285 | inline void _destroy(T *_p) { _p->~T(); } 286 | }; 287 | 288 | } // namespace container 289 | 290 | #endif 291 | -------------------------------------------------------------------------------- /test/ring_queue_test.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | class NonDefaultConstructor { 9 | public: 10 | NonDefaultConstructor() = delete; 11 | }; 12 | 13 | class Base { 14 | public: 15 | Base(int index = 0, const char *p = "nullptr") : index_(index) { 16 | // printf("con\n"); 17 | if (p == nullptr) { 18 | str_ = new char[1]; 19 | *str_ = '\0'; 20 | } else { 21 | int len = strlen(p); 22 | str_ = new char[len + 1]; 23 | memcpy(str_, p, len + 1); 24 | } 25 | } 26 | Base(const Base &other) { 27 | // printf("copy con\n"); 28 | int len = strlen(other.str_); 29 | str_ = new char[len + 1]; 30 | memcpy(str_, other.str_, len + 1); 31 | 32 | index_ = other.index_; 33 | } 34 | Base(Base &&other) { 35 | // printf("move copy con\n"); 36 | str_ = other.str_; 37 | other.str_ = nullptr; 38 | 39 | index_ = other.index_; 40 | } 41 | Base &operator=(const Base &other) { 42 | // printf("operator=\n"); 43 | if (&other != this) { 44 | delete[] str_; 45 | int len = strlen(other.str_); 46 | str_ = new char[len + 1]; 47 | memcpy(str_, other.str_, len + 1); 48 | 49 | index_ = other.index_; 50 | } 51 | return *this; 52 | } 53 | ~Base() { 54 | // printf("destru\n"); 55 | delete[] str_; 56 | } 57 | 58 | bool operator==(int x) const noexcept { return index_ == x; } 59 | 60 | int index_{0}; 61 | char *str_{nullptr}; 62 | 63 | friend bool operator<(const Base &x, const Base &y); 64 | }; 65 | 66 | bool operator<(const Base &x, const Base &y) { return x.index_ < y.index_; } 67 | 68 | class AllocatorTest : public testing::Test { 69 | public: 70 | container::Allocator alloc; 71 | }; 72 | 73 | TEST_F(AllocatorTest, GetNodeFunc) { 74 | int chunk_size = sizeof(Base); 75 | 76 | Base *p1 = alloc.GetNode(); 77 | Base *p2 = alloc.GetNode(); 78 | Base *p3 = alloc.GetNode(); 79 | alloc.PutNode(p1); 80 | 81 | ::new ((void *)p1) Base(); 82 | ::new ((void *)p2) Base(); 83 | 84 | static_cast(p1)->~Base(); 85 | static_cast(p2)->~Base(); 86 | 87 | EXPECT_EQ((char *)p2 - (char *)p1, chunk_size); 88 | EXPECT_EQ((char *)p3 - (char *)p1, chunk_size * 2); 89 | 90 | Base *p4 = alloc.GetNode(); 91 | Base *p5 = alloc.GetNode(); 92 | Base *p6 = alloc.GetNode(); 93 | EXPECT_EQ(p1, p6); 94 | } 95 | 96 | TEST_F(AllocatorTest, MaxGetNode) { 97 | for (int i = 0; i < 1000; ++i) { 98 | Base *p = alloc.GetNode(); 99 | if (p == nullptr) { 100 | EXPECT_EQ(i, 49); 101 | break; 102 | } 103 | } 104 | } 105 | 106 | class RingQueueTest : public testing::Test { 107 | public: 108 | 109 | public: 110 | container::RingQueue int_queue_; 111 | container::RingQueue base_queue_; 112 | container::RingQueue non_default_con_; 113 | }; 114 | 115 | TEST_F(RingQueueTest, type_int) { 116 | int_queue_.push(2); 117 | int_queue_.push(3); 118 | int_queue_.push(1); 119 | 120 | EXPECT_EQ(int_queue_.front(), 1); 121 | int_queue_.pop(); 122 | EXPECT_EQ(int_queue_.front(), 2); 123 | int_queue_.clear(); 124 | EXPECT_TRUE(int_queue_.empty()); 125 | } 126 | 127 | TEST_F(RingQueueTest, Init) { 128 | EXPECT_TRUE(base_queue_.empty()); 129 | EXPECT_EQ(base_queue_.size(), 0); 130 | } 131 | 132 | TEST_F(RingQueueTest, push_3_element_order) { 133 | Base a(2); 134 | Base b(5); 135 | Base c(8); 136 | 137 | EXPECT_EQ(a.index_, 2); 138 | EXPECT_EQ(b.index_, 5); 139 | EXPECT_EQ(c.index_, 8); 140 | 141 | base_queue_.push(a); 142 | base_queue_.push(b); 143 | base_queue_.push(c); 144 | 145 | EXPECT_FALSE(base_queue_.empty()); 146 | EXPECT_EQ(base_queue_.size(), 3); 147 | 148 | auto it = base_queue_.begin(); 149 | EXPECT_EQ(it->index_, 2); 150 | ++it; 151 | EXPECT_EQ(it->index_, 5); 152 | ++it; 153 | EXPECT_EQ(it->index_, 8); 154 | } 155 | 156 | TEST_F(RingQueueTest, push_3_element_unorder) { 157 | Base a(5); 158 | Base b(8); 159 | Base c(2); 160 | 161 | base_queue_.push(a); 162 | base_queue_.push(b); 163 | base_queue_.push(c); 164 | 165 | EXPECT_FALSE(base_queue_.empty()); 166 | EXPECT_EQ(base_queue_.size(), 3); 167 | 168 | auto it = base_queue_.begin(); 169 | EXPECT_EQ(it->index_, 2); 170 | ++it; 171 | EXPECT_EQ(it->index_, 5); 172 | ++it; 173 | EXPECT_EQ(it->index_, 8); 174 | } 175 | 176 | TEST_F(RingQueueTest, push_15_element_order) { 177 | for (int i = 0; i < 15; ++i) { 178 | base_queue_.push(Base(i + 1)); 179 | } 180 | 181 | EXPECT_FALSE(base_queue_.empty()); 182 | EXPECT_EQ(base_queue_.size(), 10); 183 | 184 | auto it = base_queue_.begin(); 185 | int i = 6; 186 | for (; it != base_queue_.end(); ++it, ++i) { 187 | EXPECT_EQ(it->index_, i); 188 | } 189 | } 190 | 191 | TEST_F(RingQueueTest, push_15_element_unorder) { 192 | for (int i = 15; i > 0; --i) { 193 | base_queue_.push(Base(i)); 194 | } 195 | 196 | EXPECT_FALSE(base_queue_.empty()); 197 | EXPECT_EQ(base_queue_.size(), 10); 198 | 199 | auto it = base_queue_.begin(); 200 | int i = 6; 201 | for (; it != base_queue_.end(); ++it, ++i) { 202 | EXPECT_EQ(it->index_, i); 203 | } 204 | } 205 | 206 | TEST_F(RingQueueTest, preorder_emplace_time) { 207 | for (int i = 0; i < 1000000; ++i) { 208 | base_queue_.emplace(i); 209 | } 210 | 211 | EXPECT_FALSE(base_queue_.empty()); 212 | EXPECT_EQ(base_queue_.size(), 10); 213 | } 214 | 215 | TEST_F(RingQueueTest, reverse_emplace_time) { 216 | for (int i = 1000000; i > 0; --i) { 217 | base_queue_.emplace(i); 218 | } 219 | 220 | EXPECT_FALSE(base_queue_.empty()); 221 | EXPECT_EQ(base_queue_.size(), 10); 222 | } 223 | 224 | TEST_F(RingQueueTest, clear) { 225 | base_queue_.emplace(2); 226 | base_queue_.emplace(5); 227 | base_queue_.emplace(8); 228 | base_queue_.clear(); 229 | 230 | EXPECT_TRUE(base_queue_.empty()); 231 | EXPECT_EQ(base_queue_.size(), 0); 232 | } 233 | 234 | TEST(std__list, emplace_time) { 235 | std::list base_queue_; 236 | 237 | for (int i = 0; i < 1000000; ++i) { 238 | if (base_queue_.size() > 10) { 239 | base_queue_.clear(); 240 | } 241 | base_queue_.emplace_back(i); 242 | base_queue_.sort(); 243 | } 244 | } 245 | 246 | TEST(std__set, emplace_time) { 247 | std::set se; 248 | 249 | for (int i = 0; i < 1000000; ++i) { 250 | if (se.size() > 10) { 251 | se.clear(); 252 | } 253 | se.emplace(i); 254 | } 255 | } 256 | 257 | TEST_F(RingQueueTest, push_pop) { 258 | base_queue_.emplace(3); 259 | base_queue_.emplace(2, "two"); 260 | 261 | EXPECT_EQ(base_queue_.size(), 2); 262 | EXPECT_STREQ(base_queue_.front().str_, "two"); 263 | EXPECT_EQ(base_queue_.front().index_, 2); 264 | 265 | base_queue_.pop(); 266 | EXPECT_EQ(base_queue_.size(), 1); 267 | EXPECT_STREQ(base_queue_.front().str_, "nullptr"); 268 | EXPECT_EQ(base_queue_.front().index_, 3); 269 | } 270 | 271 | TEST_F(RingQueueTest, pushes_popes) { 272 | base_queue_.emplace(); 273 | base_queue_.pop(); 274 | EXPECT_EQ(base_queue_.size(), 0); 275 | base_queue_.emplace(); 276 | base_queue_.pop(); 277 | EXPECT_EQ(base_queue_.size(), 0); 278 | base_queue_.emplace(); 279 | base_queue_.pop(); 280 | base_queue_.pop(); 281 | base_queue_.pop(); 282 | base_queue_.pop(); 283 | EXPECT_EQ(base_queue_.size(), 0); 284 | } 285 | 286 | TEST_F(RingQueueTest, emplace) { 287 | base_queue_.emplace(1, "hello"); 288 | EXPECT_FALSE(base_queue_.empty()); 289 | EXPECT_EQ(base_queue_.size(), 1); 290 | } 291 | 292 | TEST_F(RingQueueTest, erase) { 293 | base_queue_.emplace(1, "hello"); 294 | base_queue_.emplace(3, "hello"); 295 | base_queue_.emplace(2, "hello"); 296 | 297 | int i = 1; 298 | for (auto it = base_queue_.begin(); it != base_queue_.end(); ++it, ++i) { 299 | ASSERT_EQ(it->index_, i); 300 | } 301 | 302 | auto it = base_queue_.begin(); 303 | ++it; 304 | base_queue_.erase(it); 305 | 306 | it = base_queue_.begin(); 307 | EXPECT_EQ(it->index_, 1); 308 | ++it; 309 | EXPECT_EQ(it->index_, 3); 310 | 311 | base_queue_.erase(base_queue_.begin()); 312 | EXPECT_EQ(base_queue_.begin()->index_, 3); 313 | 314 | base_queue_.emplace(1, "hello"); 315 | base_queue_.emplace(4, "hello"); 316 | base_queue_.emplace(2, "hello"); 317 | it = base_queue_.erase(base_queue_.begin()); 318 | it = base_queue_.erase(it); 319 | i = 3; 320 | for (auto it = base_queue_.begin(); it != base_queue_.end(); ++it, ++i) { 321 | ASSERT_EQ(it->index_, i); 322 | } 323 | 324 | for (auto it = base_queue_.begin(); it != base_queue_.end();) { 325 | it = base_queue_.erase(it); 326 | } 327 | EXPECT_TRUE(base_queue_.empty()); 328 | } 329 | 330 | int main(int argc, char *argv[]) { 331 | testing::InitGoogleTest(&argc, argv); 332 | 333 | return RUN_ALL_TESTS(); 334 | } 335 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------