Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
O
ogs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dmitri Naumov
ogs
Commits
f7f068bf
Commit
f7f068bf
authored
12 years ago
by
Dmitri Naumov
Browse files
Options
Downloads
Patches
Plain Diff
Style.
parent
dd14a3ae
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
BaseLib/Histogram.h
+126
-124
126 additions, 124 deletions
BaseLib/Histogram.h
with
126 additions
and
124 deletions
BaseLib/Histogram.h
+
126
−
124
View file @
f7f068bf
...
@@ -21,7 +21,6 @@
...
@@ -21,7 +21,6 @@
namespace
BaseLib
namespace
BaseLib
{
{
/** Basic Histogram implementation.
/** Basic Histogram implementation.
*
*
* Creates histogram from input data of type \c T.
* Creates histogram from input data of type \c T.
...
@@ -29,122 +28,126 @@ namespace BaseLib
...
@@ -29,122 +28,126 @@ namespace BaseLib
template
<
typename
T
>
template
<
typename
T
>
class
Histogram
class
Histogram
{
{
public:
public:
typedef
typename
std
::
vector
<
T
>
Data
;
///< Underlying input data vector
typedef
typename
std
::
vector
<
T
>
Data
;
///< Underlying input data vector
/// type.
/// type.
public:
public:
/** Creates histogram of the given element in the range \c [first, last).
/** Creates histogram of the given element in the range \c [first, last).
*
*
* Input data is copied into \c std::vector.
* Input data is copied into \c std::vector.
*
*
* \param data Range of elements to create histogram from.
* \param data Range of elements to create histogram from.
* \param nr_bins Number of bins in histogram.
* \param nr_bins Number of bins in histogram.
* \param computeHistogram Compute histogram if set. If not set user must
* \param computeHistogram Compute histogram if set. If not set user must
* call \c update() before accessing data.
* call \c update() before accessing data.
*/
*/
template
<
typename
InputIterator
>
template
<
typename
InputIterator
>
Histogram
(
InputIterator
first
,
InputIterator
last
,
const
int
nr_bins
=
16
,
Histogram
(
InputIterator
first
,
InputIterator
last
,
const
int
nr_bins
=
16
,
const
bool
computeHistogram
=
true
)
const
bool
computeHistogram
=
true
)
:
_data
(
first
,
last
),
_nr_bins
(
nr_bins
)
:
_data
(
first
,
last
),
_nr_bins
(
nr_bins
)
{
{
init
(
computeHistogram
);
init
(
computeHistogram
);
}
}
/** Creates histogram from \c std::vector.
/** Creates histogram from \c std::vector.
* \param data Input vector.
* \param data Input vector.
* \param nr_bins Number of bins in histogram.
* \param nr_bins Number of bins in histogram.
* \param computeHistogram Compute histogram if set. If not set user must call
* \param computeHistogram Compute histogram if set. If not set user must call
* \c update() before accessing data.
* \c update() before accessing data.
*/
*/
Histogram
(
std
::
vector
<
T
>
const
&
data
,
const
unsigned
int
nr_bins
=
16
,
Histogram
(
std
::
vector
<
T
>
const
&
data
,
const
unsigned
int
nr_bins
=
16
,
const
bool
computeHistogram
=
true
)
const
bool
computeHistogram
=
true
)
:
_data
(
data
),
_nr_bins
(
nr_bins
)
:
_data
(
data
),
_nr_bins
(
nr_bins
)
{
{
init
(
computeHistogram
);
init
(
computeHistogram
);
}
}
/** Updates histogram using sorted \c _data vector.
/** Updates histogram using sorted \c _data vector.
*
*
* Start histogram creation with first element. Then find first element in
* Start histogram creation with first element. Then find first element in
* the next histogram bin. Number of elments in the bin is the difference
* the next histogram bin. Number of elments in the bin is the difference
* between these two iterators.
* between these two iterators.
* \verbatim
* \verbatim
[0.1, 0.2, ..., 0.7 , ..., 0.7+binWidth = 0.9, 1.0 , ..., last]
[0.1, 0.2, ..., 0.7 , ..., 0.7+binWidth = 0.9, 1.0 , ..., last]
it itEnd - 1 itEnd
it itEnd - 1 itEnd
\endverbatim
\endverbatim
*/
*/
void
void
update
()
update
()
{
{
if
(
!
_dirty
)
if
(
!
_dirty
)
return
;
return
;
_bin_width
=
(
_max
-
_min
)
/
_nr_bins
;
_bin_width
=
(
_max
-
_min
)
/
_nr_bins
;
typedef
typename
Data
::
const_iterator
DataCI
;
typedef
typename
Data
::
const_iterator
DataCI
;
DataCI
it
=
_data
.
begin
();
DataCI
it
=
_data
.
begin
();
DataCI
itEnd
;
DataCI
itEnd
;
for
(
unsigned
int
bin
=
0
;
bin
<
_nr_bins
;
bin
++
)
{
for
(
unsigned
int
bin
=
0
;
bin
<
_nr_bins
;
bin
++
)
itEnd
=
std
::
upper_bound
(
it
,
(
DataCI
)
_data
.
end
(),
{
_min
+
(
bin
+
1
)
*
_bin_width
);
itEnd
=
std
::
upper_bound
(
it
,
(
DataCI
)
_data
.
end
(),
_histogram
[
bin
]
=
std
::
distance
(
it
,
itEnd
);
_min
+
(
bin
+
1
)
*
_bin_width
);
it
=
itEnd
;
_histogram
[
bin
]
=
std
::
distance
(
it
,
itEnd
);
}
it
=
itEnd
;
_dirty
=
false
;
}
}
_dirty
=
false
;
}
void
setMinimum
(
const
T
&
minimum
)
{
_min
=
minimum
;
_dirty
=
true
;
}
void
setMaximum
(
const
T
&
maximum
)
{
_max
=
maximum
;
_dirty
=
true
;
}
void
setMinimum
(
const
T
&
minimum
)
{
_min
=
minimum
;
_dirty
=
true
;
}
void
setMaximum
(
const
T
&
maximum
)
{
_max
=
maximum
;
_dirty
=
true
;
}
const
Data
&
getSortedData
()
const
{
return
_data
;
}
const
std
::
vector
<
std
::
size_t
>&
getBinCounts
()
const
{
return
_histogram
;
}
const
Data
&
getSortedData
()
const
{
return
_data
;
}
const
unsigned
int
&
getNrBins
()
const
{
return
_nr_bins
;
}
const
std
::
vector
<
std
::
size_t
>&
getBinCounts
()
const
{
return
_histogram
;
}
const
T
&
getMinimum
()
const
{
return
_min
;
}
const
unsigned
int
&
getNrBins
()
const
{
return
_nr_bins
;
}
const
T
&
getMaximum
()
const
{
return
_max
;
}
const
T
&
getMinimum
()
const
{
return
_min
;
}
const
T
&
getBinWidth
()
const
{
return
_bin_width
;
}
const
T
&
getMaximum
()
const
{
return
_max
;
}
const
T
&
getBinWidth
()
const
{
return
_bin_width
;
}
void
prettyPrint
(
std
::
ostream
&
os
,
const
unsigned
int
line_width
=
16
)
const
void
{
prettyPrint
(
std
::
ostream
&
os
,
const
unsigned
int
line_width
=
16
)
const
const
std
::
size_t
count_max
=
*
std
::
max_element
(
_histogram
.
begin
(),
_histogram
.
end
());
{
for
(
unsigned
int
bin
=
0
;
bin
<
_nr_bins
;
++
bin
)
{
const
std
::
size_t
count_max
=
os
<<
"["
<<
_min
+
bin
*
_bin_width
<<
", "
<<
_min
+
(
bin
+
1
)
*
_bin_width
<<
")
\t
"
;
*
std
::
max_element
(
_histogram
.
begin
(),
_histogram
.
end
());
os
<<
_histogram
[
bin
]
<<
"
\t
"
;
for
(
unsigned
int
bin
=
0
;
bin
<
_nr_bins
;
++
bin
)
{
const
int
n_stars
=
std
::
ceil
(
line_width
*
((
double
)
_histogram
[
bin
]
/
count_max
));
os
<<
"["
<<
_min
+
bin
*
_bin_width
<<
", "
<<
_min
+
for
(
int
star
=
0
;
star
<
n_stars
;
star
++
)
(
bin
+
1
)
*
_bin_width
<<
")
\t
"
;
os
<<
"*"
;
os
<<
_histogram
[
bin
]
<<
"
\t
"
;
os
<<
"
\n
"
;
}
const
int
n_stars
=
}
std
::
ceil
(
line_width
*
((
double
)
_histogram
[
bin
]
/
count_max
));
for
(
int
star
=
0
;
star
<
n_stars
;
star
++
)
protected
:
os
<<
"*"
;
/** Initialize class members after constructor call.
os
<<
"
\n
"
;
*/
}
void
init
(
const
bool
computeHistogram
=
true
)
}
{
std
::
sort
(
_data
.
begin
(),
_data
.
end
());
protected
:
_histogram
.
resize
(
_nr_bins
);
/** Initialize class members after constructor call.
_min
=
_data
.
front
();
*/
_max
=
_data
.
back
();
void
init
(
const
bool
computeHistogram
=
true
)
_bin_width
=
(
_max
-
_min
)
/
_nr_bins
;
{
std
::
sort
(
_data
.
begin
(),
_data
.
end
());
_dirty
=
true
;
_histogram
.
resize
(
_nr_bins
);
if
(
computeHistogram
)
_min
=
_data
.
front
();
update
();
_max
=
_data
.
back
();
}
_bin_width
=
(
_max
-
_min
)
/
_nr_bins
;
protected
:
_dirty
=
true
;
Data
_data
;
if
(
computeHistogram
)
const
unsigned
int
_nr_bins
;
update
();
std
::
vector
<
std
::
size_t
>
_histogram
;
}
T
_min
,
_max
;
///< Minimum and maximum input data values.
T
_bin_width
;
protected
:
Data
_data
;
private
:
const
unsigned
int
_nr_bins
;
bool
_dirty
;
///< When set \c update() will recompute histogram.
std
::
vector
<
std
::
size_t
>
_histogram
;
T
_min
,
_max
;
///< Minimum and maximum input data values.
T
_bin_width
;
private
:
bool
_dirty
;
///< When set \c update() will recompute histogram.
};
};
/** Writes histogram to output stream.
/** Writes histogram to output stream.
...
@@ -156,14 +159,13 @@ template <typename T>
...
@@ -156,14 +159,13 @@ template <typename T>
std
::
ostream
&
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Histogram
<
T
>&
h
)
operator
<<
(
std
::
ostream
&
os
,
const
Histogram
<
T
>&
h
)
{
{
os
<<
h
.
getNrBins
()
<<
" "
os
<<
h
.
getNrBins
()
<<
" "
<<
h
.
getMinimum
()
<<
" "
<<
h
.
getMinimum
()
<<
" "
<<
h
.
getMaximum
()
<<
" "
;
<<
h
.
getMaximum
()
<<
" "
;
std
::
copy
(
h
.
getBinCounts
().
begin
(),
h
.
getBinCounts
().
end
(),
std
::
copy
(
h
.
getBinCounts
().
begin
(),
h
.
getBinCounts
().
end
(),
std
::
ostream_iterator
<
T
>
(
os
,
" "
));
std
::
ostream_iterator
<
T
>
(
os
,
" "
));
return
os
<<
std
::
endl
;
return
os
<<
std
::
endl
;
}
}
}
// namespace BaseLib
}
// namespace BaseLib
#endif // BASELIB_HISTOGRAM_H
#endif // BASELIB_HISTOGRAM_H
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment