Blame glib/gprimes.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gprimes.h"
Packit ae235b
Packit ae235b
Packit ae235b
static const guint g_primes[] =
Packit ae235b
{
Packit ae235b
  11,
Packit ae235b
  19,
Packit ae235b
  37,
Packit ae235b
  73,
Packit ae235b
  109,
Packit ae235b
  163,
Packit ae235b
  251,
Packit ae235b
  367,
Packit ae235b
  557,
Packit ae235b
  823,
Packit ae235b
  1237,
Packit ae235b
  1861,
Packit ae235b
  2777,
Packit ae235b
  4177,
Packit ae235b
  6247,
Packit ae235b
  9371,
Packit ae235b
  14057,
Packit ae235b
  21089,
Packit ae235b
  31627,
Packit ae235b
  47431,
Packit ae235b
  71143,
Packit ae235b
  106721,
Packit ae235b
  160073,
Packit ae235b
  240101,
Packit ae235b
  360163,
Packit ae235b
  540217,
Packit ae235b
  810343,
Packit ae235b
  1215497,
Packit ae235b
  1823231,
Packit ae235b
  2734867,
Packit ae235b
  4102283,
Packit ae235b
  6153409,
Packit ae235b
  9230113,
Packit ae235b
  13845163,
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_spaced_primes_closest:
Packit ae235b
 * @num: a #guint
Packit ae235b
 *
Packit ae235b
 * Gets the smallest prime number from a built-in array of primes which
Packit ae235b
 * is larger than @num. This is used within GLib to calculate the optimum
Packit ae235b
 * size of a #GHashTable.
Packit ae235b
 *
Packit ae235b
 * The built-in array of primes ranges from 11 to 13845163 such that
Packit ae235b
 * each prime is approximately 1.5-2 times the previous prime.
Packit ae235b
 *
Packit ae235b
 * Returns: the smallest prime number from a built-in array of primes
Packit ae235b
 *     which is larger than @num
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_spaced_primes_closest (guint num)
Packit ae235b
{
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < G_N_ELEMENTS (g_primes); i++)
Packit ae235b
    if (g_primes[i] > num)
Packit ae235b
      return g_primes[i];
Packit ae235b
Packit ae235b
  return g_primes[G_N_ELEMENTS (g_primes) - 1];
Packit ae235b
}