<?php /*~ common.inc.php
.---------------------------------------------------------------------------.
|  Software: General Purpose Interval Arithmetics Library                   |
|   Version: 0.1                                                            |
|   Contact: via http://www.uniwits.com/contact                             |
|      Info: http://www.uniwits.com/                                        |
|   Support: http://www.uniwits.com/support                                 |
|      Note: This library is designed for Linux and compatible systems.     |
| ------------------------------------------------------------------------- |
|     Admin: Yuxiang Mao (project admininistrator)                          |
|   Authors: Yuxiang Mao (Uniwits) contact@uniwits.com                      |
|   Founder: Yuxiang Mao                                                    |
| Copyright (c) 2010-2012, Yuxiang Mao. All Rights Reserved.                |
| ------------------------------------------------------------------------- |
|   License: Distributed under Uniwits Shareware License (USL)              |
|            http://www.uniwits.com/licenses/shareware/                     |
| This program is distributed as a part of software provided by Uniwits     |
| System WITHOUT ANY WARRANTY; without even the implied warranty of         |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                      |
| ------------------------------------------------------------------------- |
*/ function intervals_sort($intervals,$startkey='start',$endkey='end') { $cascades=array(); foreach ($intervals as &$elem) { if (!isset($cascades[$elem[$startkey]][$elem[$endkey]])) $cascades[$elem[$startkey]][$elem[$endkey]]=array($elem); else array_push($cascades[$elem[$startkey]][$elem[$endkey]],$elem); } ksort($cascades); $sorted=array(); foreach ($cascades as &$byend) { ksort($byend); foreach ($byend as &$intervals) foreach ($intervals as &$elem) array_push($sorted,$elem); } return $sorted; } function intervals_union($intervals,$startkey='start',$endkey='end',$datakey=false) { $intervalqueue=intervals_sort($intervals); $intervals_unioned=array(array_shift($intervalqueue)); while ($intervalqueue) { $last_unioned=array_pop($intervals_unioned); $next_interval=array_shift($intervalqueue); if ($next_interval[$endkey]<$last_unioned[$endkey]) { if ($datakey) $last_unioned[$datakey]=array_merge($last_unioned[$datakey],$next_interval[$datakey]); array_push($intervals_unioned,$last_unioned); } else if ($next_interval[$startkey]<=$last_unioned[$endkey]) { $last_unioned[$endkey]=$next_interval[$endkey]; if ($datakey) $last_unioned[$datakey]=array_merge($last_unioned[$datakey],$next_interval[$datakey]); array_push($intervals_unioned,$last_unioned); } else { array_push($intervals_unioned,$last_unioned,$next_interval); } } return $intervals_unioned; } function intervals_minus($minuends,$subtrahends,$startkey='start',$endkey='end') { $difference=array(); while ($minuends) { $minuend=array_shift($minuends); foreach ($subtrahends as $subtrahend) { if ($subtrahend[$endkey]<=$minuend[$startkey] || $minuend[$endkey]<=$subtrahend[$startkey]) continue; if ($subtrahend[$startkey]<=$minuend[$startkey] && $minuend[$endkey]<=$subtrahend[$endkey]) { $minuend=false; break; } if ($subtrahend[$endkey]<$minuend[$endkey]) { $partminuend=$minuend; $minuend[$endkey]=$partminuend[$startkey]=$subtrahend[$endkey]; array_unshift($minuends,$partminuend); } if ($subtrahend[$startkey]<=$minuend[$startkey]) { $minuend=false; break; } else { $minuend[$endkey]=$subtrahend[$startkey]; } } if ($minuend && $minuend[$startkey]<$minuend[$endkey]) array_push($difference,$minuend); } return $difference; } ?>