00001 /* 00002 * All of the documentation and software included in the 00003 * Alchemy Software is copyrighted by Stanley Kok, Parag 00004 * Singla, Matthew Richardson, Pedro Domingos, Marc 00005 * Sumner, Hoifung Poon, and Daniel Lowd. 00006 * 00007 * Copyright [2004-07] Stanley Kok, Parag Singla, Matthew 00008 * Richardson, Pedro Domingos, Marc Sumner, Hoifung 00009 * Poon, and Daniel Lowd. All rights reserved. 00010 * 00011 * Contact: Pedro Domingos, University of Washington 00012 * (pedrod@cs.washington.edu). 00013 * 00014 * Redistribution and use in source and binary forms, with 00015 * or without modification, are permitted provided that 00016 * the following conditions are met: 00017 * 00018 * 1. Redistributions of source code must retain the above 00019 * copyright notice, this list of conditions and the 00020 * following disclaimer. 00021 * 00022 * 2. Redistributions in binary form must reproduce the 00023 * above copyright notice, this list of conditions and the 00024 * following disclaimer in the documentation and/or other 00025 * materials provided with the distribution. 00026 * 00027 * 3. All advertising materials mentioning features or use 00028 * of this software must display the following 00029 * acknowledgment: "This product includes software 00030 * developed by Stanley Kok, Parag Singla, Matthew 00031 * Richardson, Pedro Domingos, Marc Sumner, Hoifung 00032 * Poon, and Daniel Lowd in the Department of Computer Science and 00033 * Engineering at the University of Washington". 00034 * 00035 * 4. Your publications acknowledge the use or 00036 * contribution made by the Software to your research 00037 * using the following citation(s): 00038 * Stanley Kok, Parag Singla, Matthew Richardson and 00039 * Pedro Domingos (2005). "The Alchemy System for 00040 * Statistical Relational AI", Technical Report, 00041 * Department of Computer Science and Engineering, 00042 * University of Washington, Seattle, WA. 00043 * http://www.cs.washington.edu/ai/alchemy. 00044 * 00045 * 5. Neither the name of the University of Washington nor 00046 * the names of its contributors may be used to endorse or 00047 * promote products derived from this software without 00048 * specific prior written permission. 00049 * 00050 * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY OF WASHINGTON 00051 * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 00052 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00053 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00054 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY 00055 * OF WASHINGTON OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 00056 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00057 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00058 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00059 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00060 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00061 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00062 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00063 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00064 * 00065 */ 00066 #ifndef UTIL_H_OCT_17_2005 00067 #define UTIL_H_OCT_17_2005 00068 00069 #include <string> 00070 using namespace std; 00071 #include <sstream> 00072 #include <sys/time.h> 00073 #include <sys/resource.h> 00074 00075 class Util 00076 { 00077 public: 00078 00079 static bool substr(const string& pred, unsigned int& cur, string& outstr, 00080 const char* const & delimiter) 00081 { 00082 outstr.clear(); 00083 while (cur < pred.length() && isspace(pred.at(cur))) cur++; 00084 string::size_type dlt = pred.find(delimiter, cur); 00085 if (dlt == string::npos) return false; 00086 unsigned int cur2 = dlt-1; 00087 while (cur2 >= 0 && isspace(pred.at(cur2))) cur2--; 00088 if (cur2 < cur) return false; 00089 outstr = pred.substr(cur, cur2+1-cur); 00090 cur = dlt+1; 00091 return true; 00092 } 00093 00094 00095 static string trim(const string& str) 00096 { 00097 if (str.empty()) return str; 00098 int lt = 0; 00099 while (lt < (int)str.length() && isspace(str.at(lt))) lt++; 00100 int rt = str.length()-1; 00101 while (rt >= 0 && isspace(str.at(rt))) rt--; 00102 return str.substr(lt, rt+1-lt); 00103 } 00104 00105 /* this function converts an integer to a string */ 00106 static string intToString(int i) { 00107 ostringstream myStream; 00108 myStream<<i; 00109 return myStream.str(); 00110 } 00111 00112 /* this function converts a double to a string */ 00113 static string doubleToString(double i) { 00114 ostringstream myStream; 00115 myStream<<i; 00116 return myStream.str(); 00117 } 00118 00119 static int factorial(int n) 00120 { 00121 int f = 1; 00122 for (int i = 1; i <= n; i++) 00123 f = f*i; 00124 return f; 00125 } 00126 00127 static int permute(int n, int k) 00128 { 00129 int result = 1; 00130 if (n == 0 || k == 0) 00131 return result; 00132 for (int i = n; i > (n-k); i--) 00133 result = result*i; 00134 return result; 00135 } 00136 00137 static double elapsed_seconds() 00138 { 00139 double answer; 00140 00141 static struct rusage prog_rusage; 00142 static long prev_rusage_seconds = 0; 00143 static long prev_rusage_micro_seconds = 0; 00144 00145 getrusage(0, &prog_rusage); 00146 answer = (double)(prog_rusage.ru_utime.tv_sec-prev_rusage_seconds) 00147 + ((double)(prog_rusage.ru_utime.tv_usec-prev_rusage_micro_seconds)) 00148 / 1000000.0 ; 00149 prev_rusage_seconds = prog_rusage.ru_utime.tv_sec ; 00150 prev_rusage_micro_seconds = prog_rusage.ru_utime.tv_usec ; 00151 return answer; 00152 } 00153 00154 }; 00155 00156 #endif