| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #ifndef NCNN_SIMPLESTL_H |
| 16 | #define NCNN_SIMPLESTL_H |
| 17 | |
| 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | #if !NCNN_SIMPLESTL |
| 23 | |
| 24 | #include <new> |
| 25 | |
| 26 | #else |
| 27 | |
| 28 | // allocation functions |
| 29 | NCNN_EXPORT void* operator new(size_t size); |
| 30 | NCNN_EXPORT void* operator new[](size_t size); |
| 31 | // placement allocation functions |
| 32 | NCNN_EXPORT void* operator new(size_t size, void* ptr); |
| 33 | NCNN_EXPORT void* operator new[](size_t size, void* ptr); |
| 34 | // deallocation functions |
| 35 | NCNN_EXPORT void operator delete(void* ptr); |
| 36 | NCNN_EXPORT void operator delete[](void* ptr); |
| 37 | // deallocation functions since c++14 |
| 38 | #if __cplusplus >= 201402L |
| 39 | NCNN_EXPORT void operator delete(void* ptr, size_t sz); |
| 40 | NCNN_EXPORT void operator delete[](void* ptr, size_t sz); |
| 41 | #endif |
| 42 | // placement deallocation functions |
| 43 | NCNN_EXPORT void operator delete(void* ptr, void* voidptr2); |
| 44 | NCNN_EXPORT void operator delete[](void* ptr, void* voidptr2); |
| 45 | |
| 46 | #endif |
| 47 | |
| 48 | // minimal stl data structure implementation |
| 49 | namespace std { |
| 50 | |
| 51 | template<typename T> |
| 52 | const T& max(const T& a, const T& b) |
| 53 | { |
| 54 | return (a < b) ? b : a; |
| 55 | } |
| 56 | |
| 57 | template<typename T> |
| 58 | const T& min(const T& a, const T& b) |
| 59 | { |
| 60 | return (a > b) ? b : a; |
| 61 | } |
| 62 | |
| 63 | template<typename T> |
| 64 | void swap(T& a, T& b) |
| 65 | { |
| 66 | T temp(a); |
| 67 | a = b; |
| 68 | b = temp; |
| 69 | } |
| 70 | |
| 71 | template<typename T1, typename T2> |
| 72 | struct pair |
| 73 | { |
| 74 | pair() |
| 75 | : first(), second() |
| 76 | { |
| 77 | } |
| 78 | pair(const T1& t1, const T2& t2) |
| 79 | : first(t1), second(t2) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | T1 first; |
| 84 | T2 second; |
| 85 | }; |
| 86 | |
| 87 | template<typename T1, typename T2> |
| 88 | bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y) |
| 89 | { |
| 90 | return (x.first == y.first && x.second == y.second); |
| 91 | } |
| 92 | template<typename T1, typename T2> |
| 93 | bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y) |
| 94 | { |
| 95 | return x.first < y.first || (!(y.first < x.first) && x.second < y.second); |
| 96 | } |
| 97 | template<typename T1, typename T2> |
| 98 | bool operator!=(const pair<T1, T2>& x, const pair<T1, T2>& y) |
| 99 | { |
| 100 | return !(x == y); |
| 101 | } |
| 102 | template<typename T1, typename T2> |
| 103 | bool operator>(const pair<T1, T2>& x, const pair<T1, T2>& y) |
| 104 | { |
| 105 | return y < x; |
| 106 | } |
| 107 | template<typename T1, typename T2> |
| 108 | bool operator<=(const pair<T1, T2>& x, const pair<T1, T2>& y) |
| 109 | { |
| 110 | return !(y < x); |
| 111 | } |
| 112 | template<typename T1, typename T2> |
| 113 | bool operator>=(const pair<T1, T2>& x, const pair<T1, T2>& y) |
| 114 | { |
| 115 | return !(x < y); |
| 116 | } |
| 117 | |
| 118 | template<typename T1, typename T2> |
| 119 | pair<T1, T2> make_pair(const T1& t1, const T2& t2) |
| 120 | { |
| 121 | return pair<T1, T2>(t1, t2); |
| 122 | } |
| 123 | |
| 124 | template<typename T> |
| 125 | struct node |
| 126 | { |
| 127 | node* prev_; |
| 128 | node* next_; |
| 129 | T data_; |
| 130 | |
| 131 | node() |
| 132 | : prev_(0), next_(0), data_() |
| 133 | { |
| 134 | } |
| 135 | node(const T& t) |
| 136 | : prev_(0), next_(0), data_(t) |
| 137 | { |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | template<typename T> |
| 142 | struct iter_list |
| 143 | { |
| 144 | iter_list() |
| 145 | : curr_(0) |
| 146 | { |
| 147 | } |
| 148 | iter_list(node<T>* n) |
| 149 | : curr_(n) |
| 150 | { |
| 151 | } |
| 152 | iter_list(const iter_list& i) |
| 153 | : curr_(i.curr_) |
| 154 | { |
| 155 | } |
| 156 | ~iter_list() |
| 157 | { |
| 158 | } |
| 159 | |
| 160 | iter_list& operator=(const iter_list& i) |
| 161 | { |
| 162 | curr_ = i.curr_; |
| 163 | return *this; |
| 164 | } |
| 165 | |
| 166 | T& operator*() |
| 167 | { |
| 168 | return curr_->data_; |
| 169 | } |
| 170 | T* operator->() |
| 171 | { |
| 172 | return &(curr_->data_); |
| 173 | } |
| 174 | |
| 175 | bool operator==(const iter_list& i) |
| 176 | { |
| 177 | return curr_ == i.curr_; |
| 178 | } |
| 179 | bool operator!=(const iter_list& i) |
| 180 | { |
| 181 | return curr_ != i.curr_; |
| 182 | } |
| 183 | |
| 184 | iter_list& operator++() |
| 185 | { |
| 186 | curr_ = curr_->next_; |
| 187 | return *this; |
| 188 | } |
| 189 | iter_list& operator--() |
| 190 | { |
| 191 | curr_ = curr_->prev_; |
| 192 | return *this; |
| 193 | } |
| 194 | |
| 195 | node<T>* curr_; |
| 196 | }; |
| 197 | |
| 198 | template<typename T> |
| 199 | struct list |
| 200 | { |
| 201 | typedef iter_list<T> iterator; |
| 202 | |
| 203 | list() |
| 204 | { |
| 205 | head_ = new node<T>(); |
| 206 | tail_ = head_; |
| 207 | count_ = 0; |
| 208 | } |
| 209 | ~list() |
| 210 | { |
| 211 | clear(); |
| 212 | delete head_; |
| 213 | } |
| 214 | list(const list& l) |
| 215 | { |
| 216 | head_ = new node<T>(); |
| 217 | tail_ = head_; |
| 218 | count_ = 0; |
| 219 | |
| 220 | for (iter_list<T> i = l.begin(); i != l.end(); ++i) |
| 221 | { |
| 222 | push_back(*i); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | list& operator=(const list& l) |
| 227 | { |
| 228 | if (this == &l) |
| 229 | { |
| 230 | return *this; |
| 231 | } |
| 232 | clear(); |
| 233 | |
| 234 | for (iter_list<T> i = l.begin(); i != l.end(); ++i) |
| 235 | { |
| 236 | push_back(*i); |
| 237 | } |
| 238 | return *this; |
| 239 | } |
| 240 | |
| 241 | void clear() |
| 242 | { |
| 243 | while (count_ > 0) |
| 244 | { |
| 245 | pop_front(); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void pop_front() |
| 250 | { |
| 251 | if (count_ > 0) |
| 252 | { |
| 253 | head_ = head_->next_; |
| 254 | delete head_->prev_; |
| 255 | head_->prev_ = 0; |
| 256 | --count_; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | size_t size() const |
| 261 | { |
| 262 | return count_; |
| 263 | } |
| 264 | iter_list<T> begin() const |
| 265 | { |
| 266 | return iter_list<T>(head_); |
| 267 | } |
| 268 | iter_list<T> end() const |
| 269 | { |
| 270 | return iter_list<T>(tail_); |
| 271 | } |
| 272 | bool empty() const |
| 273 | { |
| 274 | return count_ == 0; |
| 275 | } |
| 276 | |
| 277 | void push_back(const T& t) |
| 278 | { |
| 279 | if (count_ == 0) |
| 280 | { |
| 281 | head_ = new node<T>(t); |
| 282 | head_->prev_ = 0; |
| 283 | head_->next_ = tail_; |
| 284 | tail_->prev_ = head_; |
| 285 | count_ = 1; |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | node<T>* temp = new node<T>(t); |
| 290 | temp->prev_ = tail_->prev_; |
| 291 | temp->next_ = tail_; |
| 292 | tail_->prev_->next_ = temp; |
| 293 | tail_->prev_ = temp; |
| 294 | ++count_; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | iter_list<T> erase(iter_list<T> pos) |
| 299 | { |
| 300 | if (pos != end()) |
| 301 | { |
| 302 | node<T>* temp = pos.curr_; |
| 303 | if (temp == head_) |
| 304 | { |
| 305 | ++pos; |
| 306 | temp->next_->prev_ = 0; |
| 307 | head_ = temp->next_; |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | --pos; |
| 312 | temp->next_->prev_ = temp->prev_; |
| 313 | temp->prev_->next_ = temp->next_; |
| 314 | ++pos; |
| 315 | } |
| 316 | delete temp; |
| 317 | --count_; |
| 318 | } |
| 319 | return pos; |
| 320 | } |
| 321 | |
| 322 | protected: |
| 323 | node<T>* head_; |
| 324 | node<T>* tail_; |
| 325 | size_t count_; |
| 326 | }; |
| 327 | |
| 328 | template<typename T> |
| 329 | struct greater |
| 330 | { |
| 331 | bool operator()(const T& x, const T& y) const |
| 332 | { |
| 333 | return (x > y); |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | template<typename T> |
| 338 | struct less |
| 339 | { |
| 340 | bool operator()(const T& x, const T& y) const |
| 341 | { |
| 342 | return (x < y); |
| 343 | } |
| 344 | }; |
| 345 | |
| 346 | template<typename RandomAccessIter, typename Compare> |
| 347 | void partial_sort(RandomAccessIter first, RandomAccessIter middle, RandomAccessIter last, Compare comp) |
| 348 | { |
| 349 | // [TODO] heap sort should be used here, but we simply use bubble sort now |
| 350 | for (RandomAccessIter i = first; i < middle; ++i) |
| 351 | { |
| 352 | // bubble sort |
| 353 | for (RandomAccessIter j = last - 1; j > first; --j) |
| 354 | { |
| 355 | if (comp(*j, *(j - 1))) |
| 356 | { |
| 357 | swap(*j, *(j - 1)); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | template<typename T> |
| 364 | struct vector |
| 365 | { |
| 366 | vector() |
| 367 | : data_(0), size_(0), capacity_(0) |
| 368 | { |
| 369 | } |
| 370 | vector(const size_t new_size, const T& value = T()) |
| 371 | : data_(0), size_(0), capacity_(0) |
| 372 | { |
| 373 | resize(new_size, value); |
| 374 | } |
| 375 | ~vector() |
| 376 | { |
| 377 | clear(); |
| 378 | } |
| 379 | vector(const vector& v) |
| 380 | : data_(0), size_(0), capacity_(0) |
| 381 | { |
| 382 | resize(v.size()); |
| 383 | for (size_t i = 0; i < size_; i++) |
| 384 | { |
| 385 | data_[i] = v.data_[i]; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | vector& operator=(const vector& v) |
| 390 | { |
| 391 | if (this == &v) |
| 392 | { |
| 393 | return *this; |
| 394 | } |
| 395 | resize(0); |
| 396 | resize(v.size()); |
| 397 | for (size_t i = 0; i < size_; i++) |
| 398 | { |
| 399 | data_[i] = v.data_[i]; |
| 400 | } |
| 401 | return *this; |
| 402 | } |
| 403 | |
| 404 | void resize(const size_t new_size, const T& value = T()) |
| 405 | { |
| 406 | try_alloc(new_size); |
| 407 | if (new_size > size_) |
| 408 | { |
| 409 | for (size_t i = size_; i < new_size; i++) |
| 410 | { |
| 411 | new (&data_[i]) T(value); |
| 412 | } |
| 413 | } |
| 414 | else if (new_size < size_) |
| 415 | { |
| 416 | for (size_t i = new_size; i < size_; i++) |
| 417 | { |
| 418 | data_[i].~T(); |
| 419 | } |
| 420 | } |
| 421 | size_ = new_size; |
| 422 | } |
| 423 | |
| 424 | void clear() |
| 425 | { |
| 426 | for (size_t i = 0; i < size_; i++) |
| 427 | { |
| 428 | data_[i].~T(); |
| 429 | } |
| 430 | delete[](char*) data_; |
| 431 | data_ = 0; |
| 432 | size_ = 0; |
| 433 | capacity_ = 0; |
| 434 | } |
| 435 | |
| 436 | T* data() const |
| 437 | { |
| 438 | return data_; |
| 439 | } |
| 440 | size_t size() const |
| 441 | { |
| 442 | return size_; |
| 443 | } |
| 444 | T& operator[](size_t i) const |
| 445 | { |
| 446 | return data_[i]; |
| 447 | } |
| 448 | T* begin() const |
| 449 | { |
| 450 | return &data_[0]; |
| 451 | } |
| 452 | T* end() const |
| 453 | { |
| 454 | return &data_[size_]; |
| 455 | } |
| 456 | bool empty() const |
| 457 | { |
| 458 | return size_ == 0; |
| 459 | } |
| 460 | |
| 461 | void push_back(const T& t) |
| 462 | { |
| 463 | try_alloc(size_ + 1); |
| 464 | new (&data_[size_]) T(t); |
| 465 | size_++; |
| 466 | } |
| 467 | |
| 468 | void insert(T* pos, T* b, T* e) |
| 469 | { |
| 470 | vector* v = 0; |
| 471 | if (b >= begin() && b < end()) |
| 472 | { |
| 473 | //the same vector |
| 474 | v = new vector(*this); |
| 475 | b = v->begin() + (b - begin()); |
| 476 | e = v->begin() + (e - begin()); |
| 477 | } |
| 478 | size_t diff = pos - begin(); |
| 479 | try_alloc(size_ + (e - b)); |
| 480 | pos = begin() + diff; |
| 481 | memmove(pos + (e - b), pos, (end() - pos) * sizeof(T)); |
| 482 | size_t len = e - b; |
| 483 | size_ += len; |
| 484 | for (size_t i = 0; i < len; i++) |
| 485 | { |
| 486 | *pos = *b; |
| 487 | pos++; |
| 488 | b++; |
| 489 | } |
| 490 | delete v; |
| 491 | } |
| 492 | |
| 493 | T* erase(T* pos) |
| 494 | { |
| 495 | pos->~T(); |
| 496 | memmove(pos, pos + 1, (end() - pos - 1) * sizeof(T)); |
| 497 | size_--; |
| 498 | return pos; |
| 499 | } |
| 500 | |
| 501 | protected: |
| 502 | T* data_; |
| 503 | size_t size_; |
| 504 | size_t capacity_; |
| 505 | void try_alloc(size_t new_size) |
| 506 | { |
| 507 | if (new_size * 3 / 2 > capacity_ / 2) |
| 508 | { |
| 509 | capacity_ = new_size * 2; |
| 510 | T* new_data = (T*)new char[capacity_ * sizeof(T)]; |
| 511 | memset(static_cast<void*>(new_data), 0, capacity_ * sizeof(T)); |
| 512 | if (data_) |
| 513 | { |
| 514 | memmove(new_data, data_, sizeof(T) * size_); |
| 515 | delete[](char*) data_; |
| 516 | } |
| 517 | data_ = new_data; |
| 518 | } |
| 519 | } |
| 520 | }; |
| 521 | |
| 522 | struct NCNN_EXPORT string : public vector<char> |
| 523 | { |
| 524 | string() |
| 525 | { |
| 526 | } |
| 527 | string(const char* str) |
| 528 | { |
| 529 | size_t len = strlen(str); |
| 530 | resize(len); |
| 531 | memcpy(data_, str, len); |
| 532 | } |
| 533 | const char* c_str() const |
| 534 | { |
| 535 | return (const char*)data_; |
| 536 | } |
| 537 | bool operator==(const string& str2) const |
| 538 | { |
| 539 | return strcmp(data_, str2.data_) == 0; |
| 540 | } |
| 541 | bool operator==(const char* str2) const |
| 542 | { |
| 543 | return strcmp(data_, str2) == 0; |
| 544 | } |
| 545 | bool operator!=(const char* str2) const |
| 546 | { |
| 547 | return strcmp(data_, str2) != 0; |
| 548 | } |
| 549 | string& operator+=(const string& str1) |
| 550 | { |
| 551 | insert(end(), str1.begin(), str1.end()); |
| 552 | return *this; |
| 553 | } |
| 554 | }; |
| 555 | |
| 556 | inline string operator+(const string& str1, const string& str2) |
| 557 | { |
| 558 | string str(str1); |
| 559 | str.insert(str.end(), str2.begin(), str2.end()); |
| 560 | return str; |
| 561 | } |
| 562 | |
| 563 | } // namespace std |
| 564 | |
| 565 | #endif // NCNN_SIMPLESTL_H |