Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
folly
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
folly
Commits
c5a492c0
Commit
c5a492c0
authored
Apr 26, 2013
by
Tom Jackson
Committed by
Sara Golemon
May 20, 2013
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
distinctBy()
Test Plan: Unit tests Reviewed By: jbrewer@fb.com FB internal diff: D791149
parent
5f517beb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
134 additions
and
4 deletions
+134
-4
folly/experimental/Gen-inl.h
folly/experimental/Gen-inl.h
+86
-4
folly/experimental/Gen.h
folly/experimental/Gen.h
+11
-0
folly/experimental/test/GenTest.cpp
folly/experimental/test/GenTest.cpp
+37
-0
No files found.
folly/experimental/Gen-inl.h
View file @
c5a492c0
...
@@ -901,11 +901,11 @@ class Order : public Operator<Order<Selector, Comparer>> {
...
@@ -901,11 +901,11 @@ class Order : public Operator<Order<Selector, Comparer>> {
}
}
public:
public:
Generator
(
Source
source
,
Generator
(
Source
source
,
const
Selector
&
selector
,
Selector
selector
,
const
Comparer
&
comparer
)
Comparer
comparer
)
:
source_
(
std
::
move
(
source
)),
:
source_
(
std
::
move
(
source
)),
selector_
(
s
elector
),
selector_
(
s
td
::
move
(
selector
)
),
comparer_
(
comparer
)
{}
comparer_
(
std
::
move
(
comparer
)
)
{}
VectorType
operator
|
(
const
Collect
<
VectorType
>&
)
const
{
VectorType
operator
|
(
const
Collect
<
VectorType
>&
)
const
{
return
asVector
();
return
asVector
();
...
@@ -956,6 +956,86 @@ class Order : public Operator<Order<Selector, Comparer>> {
...
@@ -956,6 +956,86 @@ class Order : public Operator<Order<Selector, Comparer>> {
}
}
};
};
/**
* Distinct - For filtering duplicates out of a sequence. A selector may be
* provided to generate a key to uniquify for each value.
*
* This type is usually used through the 'distinct' helper function, like:
*
* auto closest = from(results)
* | distinctBy([](Item& i) {
* return i.target;
* })
* | take(10);
*/
template
<
class
Selector
>
class
Distinct
:
public
Operator
<
Distinct
<
Selector
>>
{
Selector
selector_
;
public:
Distinct
()
{}
explicit
Distinct
(
Selector
selector
)
:
selector_
(
std
::
move
(
selector
))
{}
template
<
class
Value
,
class
Source
>
class
Generator
:
public
GenImpl
<
Value
,
Generator
<
Value
,
Source
>>
{
Source
source_
;
Selector
selector_
;
typedef
typename
std
::
decay
<
Value
>::
type
StorageType
;
// selector_ cannot be passed an rvalue or it would end up passing the husk
// of a value to the downstream operators.
typedef
const
StorageType
&
ParamType
;
typedef
typename
std
::
result_of
<
Selector
(
ParamType
)
>::
type
KeyType
;
typedef
typename
std
::
decay
<
KeyType
>::
type
KeyStorageType
;
public:
Generator
(
Source
source
,
Selector
selector
)
:
source_
(
std
::
move
(
source
)),
selector_
(
std
::
move
(
selector
))
{}
template
<
class
Body
>
void
foreach
(
Body
&&
body
)
const
{
std
::
unordered_set
<
KeyStorageType
>
keysSeen
;
source_
.
foreach
([
&
](
Value
value
)
{
if
(
keysSeen
.
insert
(
selector_
(
ParamType
(
value
))).
second
)
{
body
(
std
::
forward
<
Value
>
(
value
));
}
});
}
template
<
class
Handler
>
bool
apply
(
Handler
&&
handler
)
const
{
std
::
unordered_set
<
KeyStorageType
>
keysSeen
;
return
source_
.
apply
([
&
](
Value
value
)
->
bool
{
if
(
keysSeen
.
insert
(
selector_
(
ParamType
(
value
))).
second
)
{
return
handler
(
std
::
forward
<
Value
>
(
value
));
}
return
true
;
});
}
};
template
<
class
Source
,
class
Value
,
class
Gen
=
Generator
<
Value
,
Source
>
>
Gen
compose
(
GenImpl
<
Value
,
Source
>&&
source
)
const
{
return
Gen
(
std
::
move
(
source
.
self
()),
selector_
);
}
template
<
class
Source
,
class
Value
,
class
Gen
=
Generator
<
Value
,
Source
>
>
Gen
compose
(
const
GenImpl
<
Value
,
Source
>&
source
)
const
{
return
Gen
(
source
.
self
(),
selector_
);
}
};
/**
/**
* Composed - For building up a pipeline of operations to perform, absent any
* Composed - For building up a pipeline of operations to perform, absent any
* particular source generator. Useful for building up custom pipelines.
* particular source generator. Useful for building up custom pipelines.
...
@@ -1614,6 +1694,8 @@ static const detail::Min<Identity, Greater> max;
...
@@ -1614,6 +1694,8 @@ static const detail::Min<Identity, Greater> max;
static
const
detail
::
Order
<
Identity
>
order
;
static
const
detail
::
Order
<
Identity
>
order
;
static
const
detail
::
Distinct
<
Identity
>
distinct
;
static
const
detail
::
Map
<
Move
>
move
;
static
const
detail
::
Map
<
Move
>
move
;
static
const
detail
::
Concat
concat
;
static
const
detail
::
Concat
concat
;
...
...
folly/experimental/Gen.h
View file @
c5a492c0
...
@@ -21,6 +21,8 @@
...
@@ -21,6 +21,8 @@
#include <type_traits>
#include <type_traits>
#include <utility>
#include <utility>
#include <algorithm>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include "folly/Range.h"
#include "folly/Range.h"
#include "folly/Optional.h"
#include "folly/Optional.h"
...
@@ -213,6 +215,9 @@ class Skip;
...
@@ -213,6 +215,9 @@ class Skip;
template
<
class
Selector
,
class
Comparer
=
Less
>
template
<
class
Selector
,
class
Comparer
=
Less
>
class
Order
;
class
Order
;
template
<
class
Selector
>
class
Distinct
;
template
<
class
First
,
class
Second
>
template
<
class
First
,
class
Second
>
class
Composed
;
class
Composed
;
...
@@ -380,6 +385,12 @@ Order orderByDescending(Selector selector = Identity()) {
...
@@ -380,6 +385,12 @@ Order orderByDescending(Selector selector = Identity()) {
return
Order
(
std
::
move
(
selector
));
return
Order
(
std
::
move
(
selector
));
}
}
template
<
class
Selector
,
class
Distinct
=
detail
::
Distinct
<
Selector
>
>
Distinct
distinctBy
(
Selector
selector
=
Identity
())
{
return
Distinct
(
std
::
move
(
selector
));
}
template
<
int
n
,
template
<
int
n
,
class
Get
=
detail
::
Map
<
Get
<
n
>
>>
class
Get
=
detail
::
Map
<
Get
<
n
>
>>
Get
get
()
{
Get
get
()
{
...
...
folly/experimental/test/GenTest.cpp
View file @
c5a492c0
...
@@ -268,6 +268,43 @@ TEST(Gen, OrderTake) {
...
@@ -268,6 +268,43 @@ TEST(Gen, OrderTake) {
EXPECT_EQ
(
expected
,
actual
);
EXPECT_EQ
(
expected
,
actual
);
}
}
TEST
(
Gen
,
Distinct
)
{
auto
expected
=
vector
<
int
>
{
3
,
1
,
2
};
auto
actual
=
from
({
3
,
1
,
3
,
2
,
1
,
2
,
3
})
|
distinct
|
as
<
vector
>
();
EXPECT_EQ
(
expected
,
actual
);
}
TEST
(
Gen
,
DistinctBy
)
{
// 0 1 4 9 6 5 6 9 4 1 0
auto
expected
=
vector
<
int
>
{
0
,
1
,
2
,
3
,
4
,
5
};
auto
actual
=
seq
(
0
,
100
)
|
distinctBy
([](
int
i
)
{
return
i
*
i
%
10
;
})
|
as
<
vector
>
();
EXPECT_EQ
(
expected
,
actual
);
}
TEST
(
Gen
,
DistinctMove
)
{
// 0 1 4 9 6 5 6 9 4 1 0
auto
expected
=
vector
<
int
>
{
0
,
1
,
2
,
3
,
4
,
5
};
auto
actual
=
seq
(
0
,
100
)
|
mapped
([](
int
i
)
{
return
std
::
unique_ptr
<
int
>
(
new
int
(
i
));
})
// see comment below about selector parameters for Distinct
|
distinctBy
([](
const
std
::
unique_ptr
<
int
>&
pi
)
{
return
*
pi
*
*
pi
%
10
;
})
|
mapped
([](
std
::
unique_ptr
<
int
>
pi
)
{
return
*
pi
;
})
|
as
<
vector
>
();
// NOTE(tjackson): the following line intentionally doesn't work:
// | distinctBy([](std::unique_ptr<int> pi) { return *pi * *pi % 10; })
// This is because distinctBy because the selector intentionally requires a
// const reference. If it required a move-reference, the value might get
// gutted by the selector before said value could be passed to downstream
// operators.
EXPECT_EQ
(
expected
,
actual
);
}
TEST
(
Gen
,
MinBy
)
{
TEST
(
Gen
,
MinBy
)
{
EXPECT_EQ
(
7
,
seq
(
1
,
10
)
EXPECT_EQ
(
7
,
seq
(
1
,
10
)
|
minBy
([](
int
i
)
->
double
{
|
minBy
([](
int
i
)
->
double
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment