forked from ValveSoftware/steamos-compositor
-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into wlserver_fix_data_races
- Loading branch information
Showing
6 changed files
with
362 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
#pragma once | ||
|
||
#include <iterator> | ||
#include <span> | ||
|
||
namespace gamescope::Algorithm | ||
{ | ||
template <typename TObj> | ||
constexpr TObj *Begin( std::span<TObj> span ) | ||
{ | ||
return span.data(); | ||
} | ||
|
||
template <typename TObj> | ||
constexpr TObj *End( std::span<TObj> span ) | ||
{ | ||
return Begin( span ) + span.size(); | ||
} | ||
|
||
template <typename TIter, typename TObj> | ||
constexpr TIter FindSimple( TIter pFirst, TIter pEnd, const TObj &obj ) | ||
{ | ||
while ( pFirst != pEnd && *pFirst != obj ) | ||
++pFirst; | ||
|
||
return pFirst; | ||
} | ||
|
||
template <typename TIter, typename TObj> | ||
constexpr TIter FindByFour( TIter pFirst, TIter pEnd, const TObj &obj ) | ||
{ | ||
typename std::iterator_traits< TIter >::difference_type ulTripCount = ( pEnd - pFirst ) >> 2; | ||
|
||
while ( ulTripCount-- > 0 ) | ||
{ | ||
if ( pFirst[0] == obj ) | ||
return &pFirst[0]; | ||
|
||
if ( pFirst[1] == obj ) | ||
return &pFirst[1]; | ||
|
||
if ( pFirst[2] == obj ) | ||
return &pFirst[2]; | ||
|
||
if ( pFirst[3] == obj ) | ||
return &pFirst[3]; | ||
|
||
pFirst += 4; | ||
} | ||
|
||
switch ( pEnd - pFirst ) | ||
{ | ||
case 3: | ||
{ | ||
if ( pFirst[0] == obj ) | ||
return &pFirst[0]; | ||
|
||
if ( pFirst[1] == obj ) | ||
return &pFirst[1]; | ||
|
||
if ( pFirst[2] == obj ) | ||
return &pFirst[2]; | ||
|
||
return pEnd; | ||
} | ||
case 2: | ||
{ | ||
if ( pFirst[0] == obj ) | ||
return &pFirst[0]; | ||
|
||
if ( pFirst[1] == obj ) | ||
return &pFirst[1]; | ||
|
||
return pEnd; | ||
} | ||
case 1: | ||
{ | ||
if ( pFirst[0] == obj ) | ||
return &pFirst[0]; | ||
|
||
return pEnd; | ||
} | ||
case 0: | ||
{ | ||
return pEnd; | ||
} | ||
default: | ||
{ | ||
__builtin_unreachable(); | ||
} | ||
} | ||
} | ||
|
||
template <typename TIter, typename TObj> | ||
constexpr TIter Find( TIter pFirst, TIter pEnd, const TObj &obj ) | ||
{ | ||
return FindSimple( pFirst, pEnd, obj ); | ||
} | ||
|
||
template <typename TIter, typename TObj> | ||
constexpr TIter Find( std::span<TObj> span, const TObj &obj ) | ||
{ | ||
return Find( Begin( span ), End( span ), obj ); | ||
} | ||
|
||
template <typename TIter, typename TObj> | ||
constexpr bool ContainsShortcut( TIter pFirst, TIter pEnd, const TObj &obj ) | ||
{ | ||
return Find( pFirst, pEnd, obj ) != pEnd; | ||
} | ||
|
||
template <typename TIter, typename TObj> | ||
constexpr bool ContainsNoShortcut( TIter pFirst, TIter pEnd, const TObj &obj ) | ||
{ | ||
bool bFound = false; | ||
|
||
typename std::iterator_traits< TIter >::difference_type ulTripCount = ( pEnd - pFirst ) >> 2; | ||
|
||
while ( ulTripCount-- > 0 ) | ||
{ | ||
bFound |= pFirst[0] == obj || | ||
pFirst[1] == obj || | ||
pFirst[2] == obj || | ||
pFirst[3] == obj; | ||
|
||
pFirst += 4; | ||
} | ||
|
||
switch ( pEnd - pFirst ) | ||
{ | ||
case 3: | ||
{ | ||
bFound |= pFirst[0] == obj || | ||
pFirst[1] == obj || | ||
pFirst[2] == obj; | ||
break; | ||
} | ||
case 2: | ||
{ | ||
bFound |= pFirst[0] == obj || | ||
pFirst[1] == obj; | ||
break; | ||
} | ||
case 1: | ||
{ | ||
bFound |= pFirst[0] == obj; | ||
break; | ||
} | ||
case 0: | ||
{ | ||
break; | ||
} | ||
default: | ||
{ | ||
__builtin_unreachable(); | ||
} | ||
} | ||
|
||
return bFound; | ||
} | ||
|
||
template <typename TIter, typename TObj> | ||
constexpr bool Contains( TIter pFirst, TIter pEnd, const TObj &obj ) | ||
{ | ||
return ContainsNoShortcut( pFirst, pEnd, obj ); | ||
} | ||
|
||
template <typename TSpanObj, typename TObj> | ||
constexpr bool Contains( std::span<TSpanObj> span, const TObj &obj ) | ||
{ | ||
return Contains( Begin( span ), End( span ), obj ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.