Skip to content

Commit

Permalink
New helpers is utilities_containers
Browse files Browse the repository at this point in the history
Change-Id: Ibb653c89a556c3bb32ce891e786719f70b56431f
  • Loading branch information
jchodor authored and Compute-Runtime-Automation committed Jan 28, 2019
1 parent 08f80d1 commit 7c390fa
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 13 deletions.
71 changes: 60 additions & 11 deletions runtime/utilities/range.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,83 @@
/*
* Copyright (C) 2017-2018 Intel Corporation
* Copyright (C) 2017-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

#pragma once

#include <iterator>

namespace OCLRT {

template <typename T>
template <typename DataType>
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<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;

Range(DataType *base, size_t count)
: begIt(base), endIt(base + count) {
}

template <typename SequentialContainerT, typename BeginT = decltype(((SequentialContainerT *)nullptr)->size())>
Range(SequentialContainerT &container)
: Range(&*container.begin(), container.size()) {
}

template <typename T, size_t S>
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 <typename T>
Range<T> CreateRange(T *base, size_t count) {
inline Range<T> CreateRange(T *base, size_t count) {
return Range<T>(base, count);
}
} // namespace OCLRT
16 changes: 15 additions & 1 deletion runtime/utilities/stackvec.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 Intel Corporation
* Copyright (C) 2018-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand All @@ -11,6 +11,7 @@

#include <cinttypes>
#include <cstddef>
#include <iterator>
#include <vector>

template <typename DataType, size_t OnStackCapacity>
Expand Down Expand Up @@ -58,6 +59,13 @@ class StackVec {
resize(initialSize);
}

StackVec(std::initializer_list<DataType> init) {
reserve(init.size());
for (const auto &obj : init) {
push_back(obj);
}
}

StackVec &operator=(const StackVec &rhs) {
clear();

Expand Down Expand Up @@ -321,3 +329,9 @@ bool operator==(const StackVec<T, LhsStackCaps> &lhs,

return true;
}

template <typename T, size_t LhsStackCaps, size_t RhsStackCaps>
bool operator!=(const StackVec<T, LhsStackCaps> &lhs,
const StackVec<T, RhsStackCaps> &rhs) {
return false == (lhs == rhs);
}
56 changes: 55 additions & 1 deletion unit_tests/utilities/containers_tests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 Intel Corporation
* Copyright (C) 2018-2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand All @@ -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"

Expand Down Expand Up @@ -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<Type, 4> 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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -1508,13 +1517,15 @@ TEST(StackVec, EqualsOperatorReturnsFalseIfDataNotEqual) {
StackVec<char, 10> vecA{dataA, dataA + sizeof(dataA)};
StackVec<char, 15> vecB{dataB, dataB + sizeof(dataB)};
EXPECT_FALSE(vecA == vecB);
EXPECT_TRUE(vecA != vecB);
}

TEST(StackVec, EqualsOperatorReturnsTrueIfBothContainersAreEmpty) {
StackVec<char, 10> vecA;
StackVec<char, 15> vecB;

EXPECT_TRUE(vecA == vecB);
EXPECT_FALSE(vecA != vecB);
}

TEST(StackVec, EqualsOperatorReturnsTrueIfDataIsEqual) {
Expand All @@ -1524,6 +1535,7 @@ TEST(StackVec, EqualsOperatorReturnsTrueIfDataIsEqual) {
StackVec<char, 10> vecA{dataA, dataA + sizeof(dataA)};
StackVec<char, 15> vecB{dataB, dataB + sizeof(dataB)};
EXPECT_TRUE(vecA == vecB);
EXPECT_FALSE(vecA != vecB);
}

int sum(ArrayRef<int> a) {
Expand Down Expand Up @@ -1638,3 +1650,45 @@ TEST(ArrayRef, EqualsOperatorReturnsTrueIfDataIsEqual) {
ArrayRef<char> 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<int> range = tab;
const Range<int> &constantRange = range;
Range<int> 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<int> vec(&tab[0], &tab[10]);
Range<int> rangeFromVec = vec;
EXPECT_EQ(&*vec.begin(), &*rangeFromVec.begin());
EXPECT_EQ(&*vec.rbegin(), &*rangeFromVec.rbegin());
}

0 comments on commit 7c390fa

Please sign in to comment.