Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 505fc29
  • /
  • src
  • /
  • Statistics.cpp
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge
swh:1:cnt:96be38690b28a91dd8e1c702332545e11b3beb24
directory badge
swh:1:dir:6b077e51a4e0137699fb0df51458390d621719a6

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
Statistics.cpp
#include "Statistics.h"

#include <iostream>
#include <algorithm>
#include <cmath>

void Statistics::AddDatum(float datum)
{
	if (std::isnan(datum) || std::isinf(datum))
		return;
	if (data.size() > 0 && datum < data.back())
		isSorted = false;
	data.push_back(datum);
	sum += datum;
}

void Statistics::Clear()
{
	sum = 0;
	data.clear();
	isSorted = true;
	globalShift = 0;
}

std::size_t Statistics::Size() const
{
	return data.size();
}

void Statistics::SetGlobalShift(float shift)
{
	globalShift = shift;
}

float Statistics::Average() const
{
	return sum / Size() + globalShift;
}
float Statistics::Median()
{
	return Percentile(0.5f);
}

//p between 0 and 1
float Statistics::Percentile(float p)
{
	if (Size() == 0)
		return std::numeric_limits<float>::quiet_NaN();
	Sort();
	float index = p * (Size() - 1);

	auto lower = std::floor(index);
	auto lowerWeight = 1 + lower - index;

	float percentile = lowerWeight * data[(int)lower];
	if (lowerWeight != 1)
		percentile += (1 - lowerWeight) * data[(int)lower + 1];

	return percentile + globalShift;
}

float Statistics::Variance() const
{
	auto avg = Average();

	float variance = 0;
	for (auto r = data.begin(); r != data.end(); ++r)
	{
		float d = (*r + globalShift) - avg;
		variance += d * d;
	}
	return variance / (Size() - 1);
}

float Statistics::Min()
{
	Sort();
	return data[0];
}

float Statistics::Max()
{
	Sort();
	return data.back();
}

void Statistics::Sort()
{
	if (isSorted)
		return;
	std::sort(data.begin(), data.end());
	isSorted = true;
}

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API