From 7c390fa23b3772d407a22d1ad1752fdae472749c Mon Sep 17 00:00:00 2001 From: "Chodor, Jaroslaw" Date: Mon, 28 Jan 2019 14:26:33 +0100 Subject: [PATCH] New helpers is utilities_containers Change-Id: Ibb653c89a556c3bb32ce891e786719f70b56431f --- runtime/utilities/range.h | 71 +++++++++++++++++++---- runtime/utilities/stackvec.h | 16 ++++- unit_tests/utilities/containers_tests.cpp | 56 +++++++++++++++++- 3 files changed, 130 insertions(+), 13 deletions(-) diff --git a/runtime/utilities/range.h b/runtime/utilities/range.h index 420a75f29fca6..dfde445467ed8 100644 --- a/runtime/utilities/range.h +++ b/runtime/utilities/range.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2018 Intel Corporation + * Copyright (C) 2017-2019 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -7,28 +7,77 @@ #pragma once +#include + namespace OCLRT { -template +template struct Range { - Range(T *base, size_t count) - : Beg(base), End(base + count) { + using iterator = DataType *; + using const_iterator = const DataType *; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + Range(DataType *base, size_t count) + : begIt(base), endIt(base + count) { + } + + template size())> + Range(SequentialContainerT &container) + : Range(&*container.begin(), container.size()) { + } + + template + Range(T (&base)[S]) + : Range(&base[0], S) { + } + + iterator begin() { + return begIt; + } + + iterator end() { + return endIt; + } + + const_iterator begin() const { + return begIt; + } + + const_iterator end() const { + return endIt; + } + + reverse_iterator rbegin() { + return reverse_iterator(end()); + } + + const_reverse_iterator rbegin() const { + return const_reverse_iterator(end()); + } + + reverse_iterator rend() { + return reverse_iterator(end()) + (endIt - begIt); + } + + const_reverse_iterator rend() const { + return const_reverse_iterator(end()) + (endIt - begIt); } - T *begin() { - return Beg; + bool empty() const { + return begIt == endIt; } - T *end() { - return End; + size_t size() const { + return endIt - begIt; } - T *Beg; - T *End; + iterator begIt; + iterator endIt; }; template -Range CreateRange(T *base, size_t count) { +inline Range CreateRange(T *base, size_t count) { return Range(base, count); } } // namespace OCLRT \ No newline at end of file diff --git a/runtime/utilities/stackvec.h b/runtime/utilities/stackvec.h index c7783571800bc..11b24b01385d8 100644 --- a/runtime/utilities/stackvec.h +++ b/runtime/utilities/stackvec.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Intel Corporation + * Copyright (C) 2018-2019 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -11,6 +11,7 @@ #include #include +#include #include template @@ -58,6 +59,13 @@ class StackVec { resize(initialSize); } + StackVec(std::initializer_list init) { + reserve(init.size()); + for (const auto &obj : init) { + push_back(obj); + } + } + StackVec &operator=(const StackVec &rhs) { clear(); @@ -321,3 +329,9 @@ bool operator==(const StackVec &lhs, return true; } + +template +bool operator!=(const StackVec &lhs, + const StackVec &rhs) { + return false == (lhs == rhs); +} diff --git a/unit_tests/utilities/containers_tests.cpp b/unit_tests/utilities/containers_tests.cpp index d2fbee8182c19..e848857de5e75 100644 --- a/unit_tests/utilities/containers_tests.cpp +++ b/unit_tests/utilities/containers_tests.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 Intel Corporation + * Copyright (C) 2018-2019 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -8,6 +8,7 @@ #include "runtime/utilities/arrayref.h" #include "runtime/utilities/idlist.h" #include "runtime/utilities/iflist.h" +#include "runtime/utilities/range.h" #include "runtime/utilities/stackvec.h" #include "unit_tests/utilities/containers_tests_helpers.h" @@ -1069,6 +1070,12 @@ TEST(StackVec, Constructor) { ASSERT_TRUE(contains(&bigger, &*bigger.begin())); ASSERT_TRUE(contains(&exact, &*exact.begin())); ASSERT_FALSE(contains(&smaller, &*smaller.begin())); + + StackVec withInitList{1, 2, 3, 5}; + EXPECT_EQ(1, withInitList[0]); + EXPECT_EQ(2, withInitList[1]); + EXPECT_EQ(3, withInitList[2]); + EXPECT_EQ(5, withInitList[3]); } TEST(StackVec, ConstructorWithInitialSizeGetsResizedAutomaticallyDuringConstruction) { @@ -1499,6 +1506,8 @@ TEST(StackVec, EqualsOperatorReturnsFalseIfStackVecsHaveDifferentSizes) { EXPECT_FALSE(longer == shorter); EXPECT_FALSE(shorter == longer); + EXPECT_TRUE(longer != shorter); + EXPECT_TRUE(shorter != longer); } TEST(StackVec, EqualsOperatorReturnsFalseIfDataNotEqual) { @@ -1508,6 +1517,7 @@ TEST(StackVec, EqualsOperatorReturnsFalseIfDataNotEqual) { StackVec vecA{dataA, dataA + sizeof(dataA)}; StackVec vecB{dataB, dataB + sizeof(dataB)}; EXPECT_FALSE(vecA == vecB); + EXPECT_TRUE(vecA != vecB); } TEST(StackVec, EqualsOperatorReturnsTrueIfBothContainersAreEmpty) { @@ -1515,6 +1525,7 @@ TEST(StackVec, EqualsOperatorReturnsTrueIfBothContainersAreEmpty) { StackVec vecB; EXPECT_TRUE(vecA == vecB); + EXPECT_FALSE(vecA != vecB); } TEST(StackVec, EqualsOperatorReturnsTrueIfDataIsEqual) { @@ -1524,6 +1535,7 @@ TEST(StackVec, EqualsOperatorReturnsTrueIfDataIsEqual) { StackVec vecA{dataA, dataA + sizeof(dataA)}; StackVec vecB{dataB, dataB + sizeof(dataB)}; EXPECT_TRUE(vecA == vecB); + EXPECT_FALSE(vecA != vecB); } int sum(ArrayRef a) { @@ -1638,3 +1650,45 @@ TEST(ArrayRef, EqualsOperatorReturnsTrueIfDataIsEqual) { ArrayRef arrayB{dataB, sizeof(dataB)}; EXPECT_TRUE(arrayA == arrayB); } + +TEST(Range, GivenRangeThenValidStandardIteratorsAreAvailable) { + int tab[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; + Range range = tab; + const Range &constantRange = range; + Range emptyRange{nullptr, 0}; + EXPECT_EQ(0U, emptyRange.size()); + EXPECT_TRUE(emptyRange.empty()); + EXPECT_EQ(10U, constantRange.size()); + EXPECT_FALSE(constantRange.empty()); + + auto rangeFwdIt = range.begin(); + auto rangeFwdEnd = range.end(); + auto rangeBackIt = range.rbegin(); + auto rangeBackEnd = range.rend(); + + auto constantRangeFwdIt = constantRange.begin(); + auto constantRangeFwdEnd = constantRange.end(); + auto constantRangeBackIt = constantRange.rbegin(); + auto constantRangeBackEnd = constantRange.rend(); + for (int i = 0; i < 10; ++i, ++rangeFwdIt, ++rangeBackIt, ++constantRangeFwdIt, ++constantRangeBackIt) { + EXPECT_EQ(tab[i], *rangeFwdIt) << " it : " << i; + EXPECT_EQ(tab[i], *constantRangeFwdIt) << " it : " << i; + EXPECT_NE(rangeFwdEnd, rangeFwdIt) << " it : " << i; + EXPECT_NE(constantRangeFwdEnd, constantRangeFwdIt) << " it : " << i; + + EXPECT_EQ(tab[10 - 1 - i], *rangeBackIt) << " it : " << i; + EXPECT_EQ(tab[10 - 1 - i], *constantRangeBackIt) << " it : " << i; + EXPECT_NE(rangeBackEnd, rangeBackIt) << " it : " << i; + EXPECT_NE(constantRangeBackEnd, constantRangeBackIt) << " it : " << i; + } + + EXPECT_EQ(rangeFwdEnd, rangeFwdIt); + EXPECT_EQ(constantRangeFwdEnd, constantRangeFwdIt); + EXPECT_EQ(rangeBackEnd, rangeBackIt); + EXPECT_EQ(constantRangeBackEnd, constantRangeBackIt); + + std::vector vec(&tab[0], &tab[10]); + Range rangeFromVec = vec; + EXPECT_EQ(&*vec.begin(), &*rangeFromVec.begin()); + EXPECT_EQ(&*vec.rbegin(), &*rangeFromVec.rbegin()); +}