<commit_before>/*
 * Copyright (C) 2010, 2011 Igalia S.L.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:grl-util
 * @short_description: utility functions
 */

#include "grl-util.h"

#include <string.h>

/**
 * grl_paging_translate:
 * @skip: number of elements to skip
 * @count: number of elements to retrieve
 * @max_page_size: maximum value for page size (0 for unlimited size)
 * @page_size: optimal page size
 * @page_number: page which contain the first element to retrieve (starting at 1)
 * @internal_offset: in the @page_number, offset where first element can be found (starting at 0)
 *
 * Grilo browsing implements a paging mechanism through @skip and @count values.
 *
 * But there are some services (like Jamendo or Flickr) where paging is done
 * through a page number and page size: user request all elements in a page,
 * specifying in most cases what is the page size.
 *
 * This function is a helper for this task, computing from @skip and @count what
 * is the optimal value of page size (limited by @max_page_size), which page
 * should the user request, and where requested data start inside the page.
 *
 * By optimal we mean that it computes those values so only one page is required
 * to satisfy the data, using the smallest page size. If user is limiting page
 * size, then more requests to services might be needed. But still page size
 * will be an optimal value.
 *
 * Since: 0.1.6
 **/
void grl_paging_translate (guint skip,
                           guint count,
                           guint max_page_size,
                           guint *page_size,
                           guint *page_number,
                           guint *internal_offset)
{
  gulong _page_size;
  gulong last_element;

  if (skip < count) {
    _page_size = skip + count;
    if (max_page_size > 0) {
      _page_size = CLAMP (_page_size, 0, max_page_size);
    }
  } else {
    _page_size = count;
    last_element = skip + count - 1;
    while (skip/_page_size != last_element/_page_size &&
           (max_page_size == 0 || _page_size < max_page_size)) {
      _page_size++;
    }
  }
  _page_size = CLAMP (_page_size, 0, G_MAXUINT);

  if (page_size) {
    *page_size = _page_size;
  }

  if (page_number) {
    *page_number = skip/_page_size + 1;
  }

  if (internal_offset) {
    *internal_offset = skip%_page_size;
  }
}
<commit_msg>core: Adjust page_size to max_page_size<commit_after>/*
 * Copyright (C) 2010, 2011 Igalia S.L.
 *
 * Contact: <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**
 * SECTION:grl-util
 * @short_description: utility functions
 */

#include "grl-util.h"

#include <string.h>

/**
 * grl_paging_translate:
 * @skip: number of elements to skip
 * @count: number of elements to retrieve
 * @max_page_size: maximum value for page size (0 for unlimited size)
 * @page_size: optimal page size
 * @page_number: page which contain the first element to retrieve (starting at 1)
 * @internal_offset: in the @page_number, offset where first element can be found (starting at 0)
 *
 * Grilo browsing implements a paging mechanism through @skip and @count values.
 *
 * But there are some services (like Jamendo or Flickr) where paging is done
 * through a page number and page size: user request all elements in a page,
 * specifying in most cases what is the page size.
 *
 * This function is a helper for this task, computing from @skip and @count what
 * is the optimal value of page size (limited by @max_page_size), which page
 * should the user request, and where requested data start inside the page.
 *
 * By optimal we mean that it computes those values so only one page is required
 * to satisfy the data, using the smallest page size. If user is limiting page
 * size, then more requests to services might be needed. But still page size
 * will be an optimal value.
 *
 * If @page_size is @NULL, then page size will be @max_page_size. If the later
 * is also 0, then page size will be #G_MAXUINT.
 *
 * Since: 0.1.6
 **/
void grl_paging_translate (guint skip,
                           guint count,
                           guint max_page_size,
                           guint *page_size,
                           guint *page_number,
                           guint *internal_offset)
{
  gulong _page_size;
  gulong last_element;

  if (!page_size) {
    if (max_page_size > 0) {
      _page_size = max_page_size;
    } else {
      _page_size = G_MAXUINT;
    }
  } else {
    if (skip < count) {
      _page_size = skip + count;
      if (max_page_size > 0) {
        _page_size = CLAMP (_page_size, 0, max_page_size);
      }
    } else {
      _page_size = count;
      last_element = skip + count - 1;
      while (skip/_page_size != last_element/_page_size &&
             (max_page_size == 0 || _page_size < max_page_size)) {
        _page_size++;
      }
    }
    _page_size = CLAMP (_page_size, 0, G_MAXUINT);
  }

  if (page_size) {
    *page_size = _page_size;
  }

  if (page_number) {
    *page_number = skip/_page_size + 1;
  }

  if (internal_offset) {
    *internal_offset = skip%_page_size;
  }
}

{'fraction_non_alphanumeric': 0.0800122624156959, 'fraction_numerical': 0.010423053341508276, 'mean_word_length': 2.9001793185893603, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2679860', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1901, 'n_words': 807}