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
3f2a0c2b
Commit
3f2a0c2b
authored
Feb 10, 2013
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added an operator>> for matrix objects which allows you to read in
ASCII matrices using the format used by operator<<.
parent
010f87a6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
155 additions
and
0 deletions
+155
-0
matrix.h
dlib/matrix.h
+1
-0
matrix.h
dlib/matrix/matrix.h
+16
-0
matrix_abstract.h
dlib/matrix/matrix_abstract.h
+28
-0
matrix_read_from_istream.h
dlib/matrix/matrix_read_from_istream.h
+110
-0
No files found.
dlib/matrix.h
View file @
3f2a0c2b
...
...
@@ -11,6 +11,7 @@
#include "matrix/matrix_la.h"
#include "matrix/symmetric_matrix_cache.h"
#include "matrix/matrix_conv.h"
#include "matrix/matrix_read_from_istream.h"
#ifdef DLIB_USE_BLAS
#include "matrix/matrix_blas_bindings.h"
...
...
dlib/matrix/matrix.h
View file @
3f2a0c2b
...
...
@@ -1786,6 +1786,22 @@ namespace dlib
return
out
;
}
/*
template <
typename T,
long NR,
long NC,
typename MM,
typename L
>
std::istream& operator>> (
std::istream& in,
matrix<T,NR,NC,MM,L>& m
);
This function is defined inside the matrix_read_from_istream.h file.
*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
...
...
dlib/matrix/matrix_abstract.h
View file @
3f2a0c2b
...
...
@@ -703,6 +703,34 @@ namespace dlib
- returns out
!*/
template
<
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
std
::
istream
&
operator
>>
(
std
::
istream
&
in
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
m
);
/*!
ensures
- Tries to read a matrix from the given input stream and store it into #m.
- The format expected is the text format output by the above operator<<().
That is, the format should be a grid of text such as:
2 3 4
5 2 6
- The separation between numbers can be any number of whitespace characters or
commas.
- The matrix data is assumed to end upon the first blank line or end-of-file,
whichever comes first. This means you can create an input stream with
multiple matrices in it by separating them with empty lines.
- returns in.
- If there was a formatting error or something which prevents the input data
from being parsed into a matrix then #in.fail() == true.
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
EXP
>
...
...
dlib/matrix/matrix_read_from_istream.h
0 → 100644
View file @
3f2a0c2b
// Copyright (C) 2013 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_MATRIx_READ_FROM_ISTREAM_H___
#define DLIB_MATRIx_READ_FROM_ISTREAM_H___
#include "matrix.h"
#include <vector>
#include <iostream>
namespace
dlib
{
// ----------------------------------------------------------------------------------------
namespace
impl
{
inline
bool
next_is_whitespace
(
std
::
istream
&
in
)
{
return
in
.
peek
()
==
'\n'
||
in
.
peek
()
==
' '
||
in
.
peek
()
==
','
||
in
.
peek
()
==
'\t'
||
in
.
peek
()
==
'\r'
;
}
}
template
<
typename
T
,
long
NR
,
long
NC
,
typename
MM
,
typename
L
>
std
::
istream
&
operator
>>
(
std
::
istream
&
in
,
matrix
<
T
,
NR
,
NC
,
MM
,
L
>&
m
)
{
using
namespace
dlib
::
impl
;
long
num_rows
=
0
;
std
::
vector
<
T
>
buf
;
buf
.
reserve
(
100
);
// eat any leading whitespace
while
(
next_is_whitespace
(
in
))
in
.
get
();
bool
at_start_of_line
=
true
;
bool
stop
=
false
;
while
(
!
stop
&&
in
.
peek
()
!=
EOF
)
{
T
temp
;
in
>>
temp
;
if
(
!
in
)
return
in
;
buf
.
push_back
(
temp
);
if
(
at_start_of_line
)
{
at_start_of_line
=
false
;
++
num_rows
;
}
// Eat next block of whitespace but also note if we hit the start of the next
// line.
int
last_ch
=
'a'
;
// set last_ch to anything other than '\n'
while
(
next_is_whitespace
(
in
))
{
if
(
last_ch
==
'\n'
&&
in
.
peek
()
==
'\n'
)
{
stop
=
true
;
break
;
}
last_ch
=
in
.
get
();
if
(
last_ch
==
'\n'
)
at_start_of_line
=
true
;
}
}
// It's an error for there to not be any matrix data in the input stream
if
(
num_rows
==
0
)
{
in
.
clear
(
in
.
rdstate
()
|
std
::
ios
::
failbit
);
return
in
;
}
const
long
num_cols
=
buf
.
size
()
/
num_rows
;
// It's also an error if the sizes don't make sense.
if
(
num_rows
*
num_cols
!=
(
long
)
buf
.
size
()
||
(
NR
!=
0
&&
NR
!=
num_rows
)
||
(
NC
!=
0
&&
NC
!=
num_cols
))
{
in
.
clear
(
in
.
rdstate
()
|
std
::
ios
::
failbit
);
return
in
;
}
m
=
reshape
(
mat
(
buf
),
num_rows
,
buf
.
size
()
/
num_rows
);
if
(
in
.
eof
())
{
// Clear the eof and fail bits since this is caused by peeking at the EOF.
// But in the current case, we have successfully read the matrix.
in
.
clear
(
in
.
rdstate
()
&
(
~
(
std
::
ios
::
eofbit
|
std
::
ios
::
failbit
)));
}
return
in
;
}
}
// ----------------------------------------------------------------------------------------
#endif // DLIB_MATRIx_READ_FROM_ISTREAM_H___
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