Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

ProgressBar.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal 00006 // 00007 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 00038 00039 /* ******************************************************* 00040 * $Id: ProgressBar.cc,v 1.9 2004/07/21 16:30:50 chrish42 Exp $ 00041 * AUTHORS: Pascal Vincent 00042 * This file is part of the PLearn library. 00043 ******************************************************* */ 00044 00045 //#include <iomanip> 00046 #include "ProgressBar.h" 00047 #include "stringutils.h" 00048 00049 #if USING_MPI 00050 #include <plearn/sys/PLMPI.h> 00051 #endif //USING_MPI 00052 00053 namespace PLearn { 00054 using namespace std; 00055 00056 00057 PP<ProgressBarPlugin> ProgressBar::plugin; // = new TextProgressBarPlugin(cerr); 00058 00059 PP<ProgressBarPlugin> ProgressBar::getCurrentPlugin() 00060 { 00061 if (plugin == NULL) 00062 plugin = new TextProgressBarPlugin(cerr); 00063 return plugin; 00064 } 00065 00066 ProgressBar::ProgressBar(string _title, unsigned long the_maxpos) 00067 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false) 00068 { 00069 if (plugin == NULL) 00070 plugin = new TextProgressBarPlugin(cerr); 00071 00072 plugin->addProgressBar(this); 00073 } 00074 00075 ProgressBar::ProgressBar(ostream& _out, string _title, unsigned long the_maxpos) 00076 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false) 00077 { 00078 if (plugin == NULL) 00079 plugin = new TextProgressBarPlugin(cerr); 00080 00081 plugin->addProgressBar(this); 00082 } 00083 ProgressBar::ProgressBar(PStream& _out, string _title, unsigned long the_maxpos) 00084 :title(_title),currentpos(0), maxpos(the_maxpos),closed(false) 00085 { 00086 if (plugin == NULL) 00087 plugin = new TextProgressBarPlugin(cerr); 00088 00089 plugin->addProgressBar(this); 00090 } 00091 00092 ProgressBar::~ProgressBar() 00093 { 00094 close(); 00095 } 00096 00097 void ProgressBar::close() 00098 { 00099 if(closed) 00100 return; 00101 closed=true; 00102 if(currentpos<maxpos) 00103 operator()(maxpos); 00104 plugin->killProgressBar(this); 00105 } 00106 00107 TextProgressBarPlugin::TextProgressBarPlugin(ostream& _out) 00108 :out(&_out) 00109 { 00110 out.outmode=PStream::raw_ascii; 00111 } 00112 00113 TextProgressBarPlugin::TextProgressBarPlugin(PStream& _out) 00114 :out(_out) 00115 { 00116 } 00117 00118 void TextProgressBarPlugin::addProgressBar(ProgressBar * pb) 00119 { 00120 #if USING_MPI 00121 if(PLMPI::rank==0) 00122 { 00123 #endif 00124 string fulltitle = string(" ") + pb->title + " (" + tostring(pb->maxpos) + ") "; 00125 out << "[" + center(fulltitle,100,'-') + "]\n["; 00126 out.flush(); 00127 #if USING_MPI 00128 } 00129 #endif 00130 } 00131 00132 void TextProgressBarPlugin::update(ProgressBar * pb,unsigned long newpos) 00133 { 00134 #if USING_MPI 00135 if(PLMPI::rank==0) 00136 { 00137 #endif 00138 // this handles the case where we reuse the same progress bar 00139 if(newpos < pb->currentpos) 00140 { 00141 pb->currentpos=0; 00142 string fulltitle = string(" ") + pb->title + " (" + tostring(pb->maxpos) + ") "; 00143 out << "\n[" + center(fulltitle,100,'-') + "]\n["; 00144 out.flush(); 00145 } 00146 00147 if(!pb->maxpos || newpos>pb->maxpos) 00148 return; 00149 int ndots = int(newpos / (double(pb->maxpos) / 100)) - int(pb->currentpos / (double(pb->maxpos) / 100)); 00150 if (ndots < 0) 00151 PLERROR("In TextProgressBarPlugin::update - Trying to plot an infinite number of dots"); 00152 while(ndots--) 00153 out << '.'; 00154 out.flush(); 00155 pb->currentpos = newpos; 00156 if(pb->currentpos==pb->maxpos) 00157 { 00158 out << "]"; 00159 out << endl; 00160 } 00161 #if USING_MPI 00162 } 00163 #endif 00164 } 00165 00166 00167 } // end of namespace PLearn 00168 00169 00170 00171

Generated on Tue Aug 17 16:03:15 2004 for PLearn by doxygen 1.3.7