/* * Copyright 2018 The Cartographer Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace CartographerSharp.Metrics; /// /// Histogram metric for tracking value distributions. /// public abstract class Histogram { /// /// Bucket boundaries for histogram. /// public class BucketBoundaries : List { public BucketBoundaries() : base() { } public BucketBoundaries(IEnumerable collection) : base(collection) { } } /// /// Histogram instance that does nothing. Safe for use in static initializers. /// public static Histogram Null() { return new NullHistogram(); } /// /// Creates fixed-width bucket boundaries. /// public static BucketBoundaries FixedWidth(double width, int numFiniteBuckets) { var result = new BucketBoundaries(); double boundary = 0; for (int i = 0; i < numFiniteBuckets; i++) { boundary += width; result.Add(boundary); } return result; } /// /// Creates scaled powers-of bucket boundaries. /// public static BucketBoundaries ScaledPowersOf(double baseValue, double scaleFactor, double maxValue) { if (baseValue <= 1) throw new ArgumentException("Base must be greater than 1", nameof(baseValue)); if (scaleFactor <= 0) throw new ArgumentException("Scale factor must be greater than 0", nameof(scaleFactor)); var result = new BucketBoundaries(); double boundary = scaleFactor; while (boundary < maxValue) { result.Add(boundary); boundary *= baseValue; } return result; } /// /// Observes a value in the histogram. /// public abstract void Observe(double value); } /// /// Null implementation of histogram that does nothing. /// internal class NullHistogram : Histogram { public override void Observe(double value) { // Do nothing } }