From 20961181a6daa47175c9df8bb223865421d2bfda Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 21 Sep 2016 19:52:59 +0000 Subject: Replace the barely used SmallVector optimizations with std::vector and explicit reserve. --- .../src/vespa/vespalib/stllike/CMakeLists.txt | 1 - .../src/vespa/vespalib/stllike/smallvector.cpp | 2 - .../src/vespa/vespalib/stllike/smallvector.h | 191 --------------------- 3 files changed, 194 deletions(-) delete mode 100644 staging_vespalib/src/vespa/vespalib/stllike/smallvector.cpp delete mode 100644 staging_vespalib/src/vespa/vespalib/stllike/smallvector.h (limited to 'staging_vespalib') diff --git a/staging_vespalib/src/vespa/vespalib/stllike/CMakeLists.txt b/staging_vespalib/src/vespa/vespalib/stllike/CMakeLists.txt index cf36a7f210f..e6b34d52615 100644 --- a/staging_vespalib/src/vespa/vespalib/stllike/CMakeLists.txt +++ b/staging_vespalib/src/vespa/vespalib/stllike/CMakeLists.txt @@ -1,6 +1,5 @@ # Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. vespa_add_library(staging_vespalib_vespalib_stllike OBJECT SOURCES - smallvector.cpp DEPENDS ) diff --git a/staging_vespalib/src/vespa/vespalib/stllike/smallvector.cpp b/staging_vespalib/src/vespa/vespalib/stllike/smallvector.cpp deleted file mode 100644 index 59d71217c41..00000000000 --- a/staging_vespalib/src/vespa/vespalib/stllike/smallvector.cpp +++ /dev/null @@ -1,2 +0,0 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -#include "smallvector.h" diff --git a/staging_vespalib/src/vespa/vespalib/stllike/smallvector.h b/staging_vespalib/src/vespa/vespalib/stllike/smallvector.h deleted file mode 100644 index 931e8ef433d..00000000000 --- a/staging_vespalib/src/vespa/vespalib/stllike/smallvector.h +++ /dev/null @@ -1,191 +0,0 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -/** - * A vector type implementation that is optimized for keeping a small amount of - * elements. If a small amount is kept, no malloc will be done within the - * vector implementation. - */ - -#pragma once - -#include -#include -#include -#include - -namespace vespalib { - -/** - * A generic iterator implementation using size() and operator[] to access - * elements. - */ -template -class IndexedContainerIterator - : public std::iterator -{ - Container* _container; - uint64_t _index; - -public: - typedef IndexedContainerIterator Iterator; - typedef typename std::iterator::difference_type difference_type; - // Required to be possible to default construct iterators - IndexedContainerIterator() : _container(0), _index(-1) {} - IndexedContainerIterator(Container& c, uint64_t index) - : _container(&c), _index(index) {} - - T& operator*() { return (*_container)[_index]; } - T* operator->() { return &(*_container)[_index]; } - - bool operator==(const Iterator& o) const { - return (_index == o._index); - } - bool operator!=(const Iterator& o) const { - return (_index != o._index); - } - bool operator<(const Iterator& o) const { - return (_index < o._index); - } - - Iterator& operator++() { - ++_index; - return *this; - } - Iterator operator++(int) { - return Iterator(*_container, _index++); - } - Iterator& operator--() { - --_index; - return *this; - } - Iterator operator--(int) { - return Iterator(*_container, _index--); - } - - Iterator operator+(const difference_type& v) { - return Iterator(*_container, _index + v); - } - Iterator operator-(const difference_type& v) { - return Iterator(*_container, _index - v); - } - difference_type operator-(const Iterator& o) { - return _index - o._index; - } -}; - -template -class SmallVector { - size_t _size; - T _smallVector[S]; - std::vector _bigVector; - -public: - typedef IndexedContainerIterator, T> iterator; - typedef IndexedContainerIterator, const T> const_iterator; - typedef T value_type; - typedef T& reference; - typedef const T& const_reference; - typedef size_t difference_type; - typedef size_t size_type; - - iterator begin() { return iterator(*this, 0); } - iterator end() { return iterator(*this, _size); } - const_iterator begin() const { return const_iterator(*this, 0); } - const_iterator end() const { return const_iterator(*this, _size); } - - SmallVector() : _size(0) {} - - SmallVector(std::initializer_list elems) - : _size(0) - { - for (auto it=elems.begin(); it != elems.end(); ++it) { - push_back(*it); - } - } - - SmallVector(const SmallVector& other) = delete; - SmallVector& operator=(const SmallVector& other) = delete; - - size_t getEfficientSizeLimit() const { return S; } - - void push_back(const T& t) { - if (_size < S) { - _smallVector[_size] = t; - ++_size; - } else { - if (_size == S) { - populateVector(); - } - _bigVector.push_back(t); - ++_size; - } - } - void pop_back() { - if (_size <= S) { - --_size; - } else { - if (--_size == S) { - _bigVector.clear(); - } else { - _bigVector.pop_back(); - } - } - } - const T& back() const { return operator[](_size - 1); } - T& back() { return operator[](_size - 1); } - const T& front() const { return operator[](0); } - T& front() { return operator[](0); } - void clear() { - _size = 0; - _bigVector.clear(); - } - const T& operator[](size_t i) const { - if (i < S) { - return _smallVector[i]; - } else { - return _bigVector[i]; - } - } - T& operator[](size_t i) { - if (i < S) { - return _smallVector[i]; - } else { - return _bigVector[i]; - } - } - bool empty() const { return (_size == 0); } - size_t size() const { return _size; } - - template - bool operator==(const O& o) const { - if (size() != o.size()) return false; - for (size_t i=0; i<_size; ++i) { - if ((*this)[i] != o[i]) return false; - } - return true; - } - template - bool operator!=(const O& o) const { - return !(operator==(o)); - } - - void erase(iterator eraseIt) { - SmallVector copy; - for (auto it = begin(); it != end(); ++it) { - if (it != eraseIt) { - copy.push_back(*it); - } - } - copy.swap(*this); - } - -private: - void populateVector() { - _bigVector.reserve(S+1); - for (size_t i=0; i