libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/sort.h
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Pierre Sermanet   *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 #ifndef SORT_H_
00034 #define SORT_H_
00035 
00036 #include <algorithm>
00037 #include <cstdlib>
00038 #include <functional>
00039 #include <fstream>
00040 #include <iostream>
00041 #include <iterator>
00042 #include <string>
00043 #include <vector>
00044 #include <list>
00045 #include <map>
00046 #include "defines.h"
00047 
00048 using namespace std;
00049 
00050 namespace ebl {
00051 
00053   class EXPORT int_span {
00054     int _ws;
00055     int _zeros;
00056     const char *_value;
00057     const char *_end;
00058 
00059   public:
00060     int_span(const char *src)
00061       {
00062         const char *start = src;
00063 
00064         // Save and skip leading whitespace
00065         while (isspace(*(unsigned char*)src)) ++src;
00066         _ws = src - start;
00067 
00068         // Save and skip leading zeros
00069         start = src;
00070         while (*src == '0') ++src;
00071         _zeros = src - start;
00072 
00073         // Save the edges of the value
00074         _value = src;
00075         while (isdigit(*(unsigned char*)src)) ++src;
00076         _end = src;
00077       }
00078 
00079     bool is_int() const { return _value != _end; }
00080     const char *value() const { return _value; }
00081     int whitespace() const { return _ws; }
00082     int zeros() const { return _zeros; }
00083     int digits() const { return _end - _value; }
00084     int non_value() const { return whitespace() + zeros(); }
00085   };
00086 
00089   EXPORT bool natural_compare_less(const string& a, const string& b);
00091   EXPORT int natural_compare(const string& a, const string& b);
00093   EXPORT int natural_compare(const string& a, const string* b);
00094 
00097   struct EXPORT natural_less: binary_function<string, string, bool> {
00098     bool operator()(const string& a, const string& b) const;
00099   };
00100 
00103   struct EXPORT natural_less_pointer: binary_function<string, string*, bool> {
00104     bool operator()(const string& a, const string* b) const;
00105   };
00106 
00109   struct EXPORT map_natural_less
00110     : binary_function<map<string,string>, map<string,string>, bool> {
00111     map_natural_less(list<string> &keys);
00112     bool operator()(const map<string,string>& m1, 
00113                     const map<string,string>& m2) const;
00114     // members
00115     list<string> keys;
00116   };
00117   
00120   struct EXPORT map_natural_less_pointer
00121     : binary_function<map<string,string*>, map<string,string*>, bool> {
00122     map_natural_less_pointer(list<string> &keys);
00123     bool operator()(const map<string,string*>& m1, 
00124                     const map<string,string*>& m2) const;
00125     // members
00126     list<string> keys;
00127   };
00128   
00131   struct EXPORT map_natural_less_uint
00132     : binary_function<map<uint,uint>, map<uint,uint>, bool> {
00133     map_natural_less_uint(list<uint> &keys, vector<string> &vals);
00134     bool operator()(const map<uint,uint>& m1, 
00135                     const map<uint,uint>& m2) const;
00136     // members
00137     list<uint> &keys;
00138     vector<string> &vals;
00139   };
00140   
00141 } // end namespace ebl
00142 
00143 #endif /* METAPLOT_H_ */