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
170cffad
Commit
170cffad
authored
Jan 19, 2013
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added make_sparse_vector_inplace()
parent
c8b59522
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
105 additions
and
0 deletions
+105
-0
sparse_vector.h
dlib/svm/sparse_vector.h
+57
-0
sparse_vector_abstract.h
dlib/svm/sparse_vector_abstract.h
+18
-0
sparse_vector.cpp
dlib/test/sparse_vector.cpp
+30
-0
No files found.
dlib/svm/sparse_vector.h
View file @
170cffad
...
@@ -908,6 +908,63 @@ namespace dlib
...
@@ -908,6 +908,63 @@ namespace dlib
return
T
(
temp
.
begin
(),
temp
.
end
());
return
T
(
temp
.
begin
(),
temp
.
end
());
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
void
make_sparse_vector_inplace
(
T
&
vect
)
{
vect
=
make_sparse_vector
(
vect
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
T
,
typename
U
,
typename
alloc
>
void
make_sparse_vector_inplace
(
std
::
vector
<
std
::
pair
<
T
,
U
>
,
alloc
>&
vect
)
{
if
(
vect
.
size
()
>
0
)
{
std
::
sort
(
vect
.
begin
(),
vect
.
end
());
// merge duplicates
for
(
unsigned
long
i
=
1
;
i
<
vect
.
size
();
++
i
)
{
// if we found a duplicate
if
(
vect
[
i
-
1
].
first
==
vect
[
i
].
first
)
{
// now start collapsing and merging the vector
unsigned
long
j
=
i
-
1
;
for
(
unsigned
long
k
=
i
;
k
<
vect
.
size
();
++
k
)
{
if
(
vect
[
j
].
first
==
vect
[
k
].
first
)
{
vect
[
j
].
second
+=
vect
[
k
].
second
;
}
else
{
++
j
;
vect
[
j
]
=
vect
[
k
];
}
}
// we removed elements when we merged so we need to adjust the size.
vect
.
resize
(
j
+
1
);
return
;
}
}
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
,
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
template
<
typename
EXP
,
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
...
...
dlib/svm/sparse_vector_abstract.h
View file @
170cffad
...
@@ -512,6 +512,24 @@ namespace dlib
...
@@ -512,6 +512,24 @@ namespace dlib
will still logically represent the same vector).
will still logically represent the same vector).
!*/
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
void
make_sparse_vector_inplace
(
T
&
vect
);
/*!
requires
- v is an unsorted sparse vector
ensures
- vect == make_sparse_vector(vect)
- This function is just an optimized version of make_sparse_vector(), in
particular, when T is a std::vector<std::pair<>> type it is much more
efficient.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
template
<
...
...
dlib/test/sparse_vector.cpp
View file @
170cffad
...
@@ -110,6 +110,34 @@ namespace
...
@@ -110,6 +110,34 @@ namespace
DLIB_TEST
(
max
(
abs
(
r1
-
r2
))
<
1e-15
);
DLIB_TEST
(
max
(
abs
(
r1
-
r2
))
<
1e-15
);
}
}
// ----------------------------------------------------------------------------------------
void
test_make_sparse_vector_inplace
()
{
std
::
vector
<
std
::
pair
<
unsigned
long
,
double
>
>
vect
;
vect
.
push_back
(
make_pair
(
4
,
1
));
vect
.
push_back
(
make_pair
(
0
,
1
));
vect
.
push_back
(
make_pair
(
4
,
1
));
vect
.
push_back
(
make_pair
(
3
,
1
));
vect
.
push_back
(
make_pair
(
8
,
1
));
vect
.
push_back
(
make_pair
(
8
,
1
));
vect
.
push_back
(
make_pair
(
8
,
1
));
vect
.
push_back
(
make_pair
(
8
,
1
));
make_sparse_vector_inplace
(
vect
);
DLIB_TEST
(
vect
.
size
()
==
4
);
DLIB_TEST
(
vect
[
0
].
first
==
0
);
DLIB_TEST
(
vect
[
1
].
first
==
3
);
DLIB_TEST
(
vect
[
2
].
first
==
4
);
DLIB_TEST
(
vect
[
3
].
first
==
8
);
DLIB_TEST
(
vect
[
0
].
second
==
1
);
DLIB_TEST
(
vect
[
1
].
second
==
1
);
DLIB_TEST
(
vect
[
2
].
second
==
2
);
DLIB_TEST
(
vect
[
3
].
second
==
4
);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
class
sparse_vector_tester
:
public
tester
class
sparse_vector_tester
:
public
tester
...
@@ -129,6 +157,8 @@ namespace
...
@@ -129,6 +157,8 @@ namespace
void
perform_test
(
void
perform_test
(
)
)
{
{
test_make_sparse_vector_inplace
();
std
::
map
<
unsigned
int
,
double
>
v
;
std
::
map
<
unsigned
int
,
double
>
v
;
v
[
4
]
=
8
;
v
[
4
]
=
8
;
v
[
2
]
=
-
4
;
v
[
2
]
=
-
4
;
...
...
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