Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
138be05d
Commit
138be05d
authored
Aug 23, 2013
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added split_array()
parent
98d7a8bf
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
92 additions
and
0 deletions
+92
-0
array.h
dlib/array.h
+1
-0
array_tools.h
dlib/array/array_tools.h
+38
-0
array_tools_abstract.h
dlib/array/array_tools_abstract.h
+33
-0
array.cpp
dlib/test/array.cpp
+20
-0
No files found.
dlib/array.h
View file @
138be05d
...
...
@@ -4,6 +4,7 @@
#define DLIB_ARRAy_
#include "array/array_kernel.h"
#include "array/array_tools.h"
#endif // DLIB_ARRAy_
dlib/array/array_tools.h
0 → 100644
View file @
138be05d
// Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_ARRAY_tOOLS_H_
#define DLIB_ARRAY_tOOLS_H_
#include "../assert.h"
#include "array_tools_abstract.h"
namespace
dlib
{
template
<
typename
T
>
void
split_array
(
T
&
a
,
T
&
b
,
double
frac
)
{
// make sure requires clause is not broken
DLIB_ASSERT
(
0
<=
frac
&&
frac
<=
1
,
"
\t
void split_array()"
<<
"
\n\t
frac must be between 0 and 1."
<<
"
\n\t
frac: "
<<
frac
);
const
unsigned
long
asize
=
static_cast
<
unsigned
long
>
(
a
.
size
()
*
frac
);
const
unsigned
long
bsize
=
a
.
size
()
-
asize
;
b
.
resize
(
bsize
);
for
(
unsigned
long
i
=
0
;
i
<
b
.
size
();
++
i
)
{
swap
(
b
[
i
],
a
[
i
+
asize
]);
}
a
.
resize
(
asize
);
}
}
#endif // DLIB_ARRAY_tOOLS_H_
dlib/array/array_tools_abstract.h
0 → 100644
View file @
138be05d
// Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_ARRAY_tOOLS_ABSTRACT_H_
#ifdef DLIB_ARRAY_tOOLS_ABSTRACT_H_
#include "array_kernel_abstract.h"
namespace
dlib
{
template
<
typename
T
>
void
split_array
(
T
&
a
,
T
&
b
,
double
frac
);
/*!
requires
- 0 <= frac <= 1
- T must be an array type such as dlib::array or std::vector
ensures
- This function takes the elements of a and splits them into two groups. The
first group remains in a and the second group is put into b. The ordering of
elements in a is preserved. In particular, concatenating #a with #b will
reproduce the original contents of a.
- The elements in a are moved around using global swap(). So they must be
swappable, but do not need to be copyable.
- #a.size() == floor(a.size()*frac)
- #b.size() == a.size()-#a.size()
!*/
}
#endif // DLIB_ARRAY_tOOLS_ABSTRACT_H_
dlib/test/array.cpp
View file @
138be05d
...
...
@@ -618,6 +618,25 @@ namespace
DLIB_TEST
(
dlib
::
is_array
<
array
<
stuff
>
>::
value
==
true
);
}
void
test_array_split
()
{
array
<
int
>
temp
(
5
);
for
(
unsigned
int
i
=
0
;
i
<
temp
.
size
();
++
i
)
temp
[
i
]
=
i
;
array
<
int
>
b
;
split_array
(
temp
,
b
,
0.5
);
DLIB_TEST
(
temp
.
size
()
==
2
);
DLIB_TEST
(
b
.
size
()
==
3
);
DLIB_TEST
(
temp
[
0
]
==
0
);
DLIB_TEST
(
temp
[
1
]
==
1
);
DLIB_TEST
(
b
[
0
]
==
2
);
DLIB_TEST
(
b
[
1
]
==
3
);
DLIB_TEST
(
b
[
2
]
==
4
);
}
class
array_tester
:
public
tester
{
...
...
@@ -639,6 +658,7 @@ namespace
array_expand_test
<
array
<
unsigned
long
>
>
();
DLIB_TEST
(
dlib
::
is_array
<
int
>::
value
==
false
);
test_array_split
();
}
}
a
;
...
...
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