Code Monkey home page Code Monkey logo

coverpage's Introduction

coverpage

Many APIs giving access to list of items use the concept of pages. Some of them enforce you to request these lists of items by giving a page size and a page index.

What if you only knows which items indexes you want to obtain and you want to know the optimal set of pages you need to request in order to get all of these items?

coverpage tries to answer this question by providing you this minimal API:

const coverpage = require('coverpage')(pageSize => π => {
  // compute the cost of the paged interval π knowing its boundaries and the page size
  return cost;
});

coverpage(I1, I2, ..., IN) // Ix are integer pairs [lower bound, upper bound]

The cost function is entirely up to you, see further for what data is bound to a paged interval.

Note that it first takes the page size as its only argument and then should return a function taking a paged interval as its only argument, this later function must finally return the cost as a number.

Also note that the upper bound of an interval is the lowest integer greater than any element of the interval and is thus not part of the interval.

The following prerequisite on integer intervals passed in argument is required: I1 < I2 < ... < IN. Which means that the intervals must be disjoint and passed in ascending order. coverpage will throw an error if this prerequisite is not fulfilled.

Given all these intervals, coverpage will return a set of objects having the following structure:

{
  pageSize: pageSize, // the size of all pages used by the paged intervals
  cost: cost, // the cost of this current set of paged intervals computed with the cost function given to coverpage
  pagedIntervals: [
    [lowerBound1, upperBound1, lowerIntervalIndex1, upperIntervalIndex1],
    // ...
    [lowerBoundM, upperBoundM, lowerIntervalIndexM, upperIntervalIndexM]
  ]
]

Each paged interval is a tuple (an array) with four elements:

  • the first element is the lowest integer in the paged interval and is by construct a multiple of the page size
  • the second element is the lowest integer that is greater than any element in the paged interval and is also by construct a multiple of the page size
  • the third element is the lowest index of the interval passed as argument that is covered by the current paged interval
  • the fourth element is the lowest index of the interval that is greater than the current paged interval

In order to get page indexes, you can just divide the first and second elements by the page size:

  • the first one will provide the first page index from the paged interval
  • the second one will provide the first page index that is not part anymore of the paged interval

coverpage's People

Contributors

gautaz avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.