Skip to content

Commit

Permalink
Add std::map operator== overload and != for vector
Browse files Browse the repository at this point in the history
Change-Id: I3b40533d2586d455db184e9d845618b41b848296
Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/46712
Tested-by: Jenkins Server <pfd-jenkins+hostboot@us.ibm.com>
Tested-by: FSP CI Jenkins <fsp-CI-jenkins+hostboot@us.ibm.com>
Reviewed-by: Nicholas E. Bofferding <bofferdn@us.ibm.com>
Tested-by: Jenkins OP Build CI <op-jenkins+hostboot@us.ibm.com>
Tested-by: Jenkins OP HW <op-hw-jenkins+hostboot@us.ibm.com>
Reviewed-by: Michael Baiocchi <mbaiocch@us.ibm.com>
Reviewed-by: Marshall J. Wilks <mjwilks@us.ibm.com>
Reviewed-by: Daniel M. Crowell <dcrowell@us.ibm.com>
  • Loading branch information
Stephen Cprek authored and dcrowell77 committed Oct 10, 2017
1 parent 0213310 commit 784ba67
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
24 changes: 16 additions & 8 deletions src/build/tools/addCopyright
Expand Up @@ -572,16 +572,24 @@ sub filetype
if ( -f $filename )
{
my $type = `grep "\\\$Filetype:.*\\\$" $filename`;
if ( $type =~ m/\$Filetype:([^\$]*)\$/ )
if ($type ne "")
{
$type = $1;
if ( $type =~ m/\$Filetype:([^\$]*)\$/ )
{
$type = $1;
}
$type =~ s/^\s*//;
$type =~ s/\s*$//;
my @knownTypes = qw/ Assembly Automake Autoconf C CVS EmxFile
LinkerScript Makefile MofFile Perl PrdRuleFile
PrdTestCaseFile Python RPC Shellscript Tcl /;
return $type if ( grep(/^$type$/, @knownTypes) );
}
# Search for files that have the comment /* vim: set filetype=cpp : */
elsif (`grep \"set filetype=cpp\" $filename`)
{
return "C";
}
$type =~ s/^\s*//;
$type =~ s/\s*$//;
my @knownTypes = qw/ Assembly Automake Autoconf C CVS EmxFile
LinkerScript Makefile MofFile Perl PrdRuleFile
PrdTestCaseFile Python RPC Shellscript Tcl /;
return $type if ( grep(/^$type$/, @knownTypes) );
}
{ # Other random files containing non-printable characters.
my $file = `cat $filename`;
Expand Down
28 changes: 28 additions & 0 deletions src/include/map
Expand Up @@ -473,6 +473,34 @@ namespace std
equal_range( const key_type& k) const
{ return submap::equal_range(k); }
};

/**
* @brief Map equality comparison.
* @param lhs A map.
* @param rhs A map of the same type as lhs.
* @return True iff the size and elements of the maps are equal.
*
* This is an equivalence relation. It is linear in the size of the
* maps. Maps are considered equivalent if their sizes are equal,
* and if corresponding elements compare equal.
*/
template <typename _K, typename _T, typename _C>
inline bool operator==(const map<_K,_T,_C>& lhs,
const map<_K,_T,_C>& rhs)
{
return (lhs.size() == rhs.size()) &&
(std::equal(lhs.begin(), lhs.end(), rhs.begin()));
}

/**
* @brief Map inequality comparison. See operator==
*/
template <typename _K, typename _T, typename _C>
inline bool operator!=(const map<_K,_T,_C>& lhs,
const map<_K,_T,_C>& rhs)
{
return !(lhs == rhs);
}
};

#endif
Expand Down
8 changes: 7 additions & 1 deletion src/include/vector
Expand Up @@ -5,7 +5,7 @@
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2011,2016 */
/* Contributors Listed Below - COPYRIGHT 2011,2017 */
/* [+] International Business Machines Corp. */
/* */
/* */
Expand Down Expand Up @@ -730,6 +730,12 @@ namespace std
return true;
}

template <class T, class U>
bool operator!=(const vector<T>& l, const vector<U>& r)
{
return !(l == r);
}

}; // end namespace std

// ------------------------------------------------------------------------------------------------
Expand Down
24 changes: 24 additions & 0 deletions src/usr/testcore/lib/stltest.H
Expand Up @@ -233,6 +233,30 @@ class STLTest : public CxxTest::TestSuite
}


// Test map comparison with non-integral key,value pairs
std::map<std::array<int,3>, std::vector<int> > m1 = {
{ {1,2,3}, {100, 101, 102} },
{ {4,5,6}, {103, 104, 105} },
{ {7,8,9}, {106, 107, 108} },
{ {10,11,12}, {109, 110, 111} },
};
std::map<std::array<int,3>, std::vector<int> > m2 = {
{ {10,11,12}, {109, 110, 111} },
{ {7,8,9}, {106, 107, 108} },
{ {4,5,6}, {103, 104, 105} },
{ {1,2,3}, {100, 101, 102} },
};

if (m1 != m2)
{
TS_FAIL("std::map operator!= is true on equal maps");
}
m2.at({1,2,3}).at(0) = 99;
if (m1 == m2)
{
TS_FAIL("std::map operator== is true on unequal maps");
}

}


Expand Down

0 comments on commit 784ba67

Please sign in to comment.