All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ttmathobjects.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TTMath Mathematical Library
3  * and is distributed under the (new) BSD licence.
4  * Author: Tomasz Sowa <t.sowa@ttmath.org>
5  */
6 
7 /*
8  * Copyright (c) 2006-2010, Tomasz Sowa
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * * Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * * Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * * Neither the name Tomasz Sowa nor the names of contributors to this
22  * project may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35  * THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 
39 #ifndef headerfilettmathobject
40 #define headerfilettmathobject
41 
47 #include <string>
48 #include <vector>
49 #include <list>
50 #include <map>
51 
52 #include "ttmathtypes.h"
53 #include "ttmathmisc.h"
54 
55 
56 namespace ttmath
57 {
58 
65 class Objects
66 {
67 public:
68 
69 
74  struct Item
75  {
76  // name of a variable of a function
77  // internally we store variables and funcions as std::string (not std::wstring even when wide characters are used)
78  std::string value;
79 
80  // number of parameters required by the function
81  // (if there's a variable this 'param' is ignored)
82  int param;
83 
84  Item() {}
85  Item(const std::string & v, int p) : value(v), param(p) {}
86  };
87 
88  // 'Table' is the type of our table
89  typedef std::map<std::string, Item> Table;
90  typedef Table::iterator Iterator;
91  typedef Table::const_iterator CIterator;
92 
93 
94 
102  static bool CorrectCharacter(int c, bool can_be_digit)
103  {
104  if( (c>='a' && c<='z') || (c>='A' && c<='Z') )
105  return true;
106 
107  if( can_be_digit && ((c>='0' && c<='9') || c=='_') )
108  return true;
109 
110  return false;
111  }
112 
113 
117  template<class string_type>
118  static bool IsNameCorrect(const string_type & name)
119  {
120  if( name.empty() )
121  return false;
122 
123  if( !CorrectCharacter(name[0], false) )
124  return false;
125 
126  typename string_type::const_iterator i = name.begin();
127 
128  for(++i ; i!=name.end() ; ++i)
129  if( !CorrectCharacter(*i, true) )
130  return false;
131 
132  return true;
133  }
134 
135 
139  bool IsDefined(const std::string & name)
140  {
141  Iterator i = table.find(name);
142 
143  if( i != table.end() )
144  // we have this object in our table
145  return true;
146 
147  return false;
148  }
149 
150 
151 
152 #ifndef TTMATH_DONT_USE_WCHAR
153 
157  bool IsDefined(const std::wstring & name)
158  {
159  // we should check whether the name (in wide characters) are correct
160  // before calling AssignString() function
161  if( !IsNameCorrect(name) )
162  return false;
163 
164  Misc::AssignString(str_tmp1, name);
165 
166  return IsDefined(str_tmp1);
167  }
168 
169 #endif
170 
171 
175  ErrorCode Add(const std::string & name, const std::string & value, int param = 0)
176  {
177  if( !IsNameCorrect(name) )
178  return err_incorrect_name;
179 
180  Iterator i = table.find(name);
181 
182  if( i != table.end() )
183  // we have this object in our table
184  return err_object_exists;
185 
186  table.insert( std::make_pair(name, Item(value, param)) );
187 
188  return err_ok;
189  }
190 
191 
192 #ifndef TTMATH_DONT_USE_WCHAR
193 
197  ErrorCode Add(const std::wstring & name, const std::wstring & value, int param = 0)
198  {
199  // we should check whether the name (in wide characters) are correct
200  // before calling AssignString() function
201  if( !IsNameCorrect(name) )
202  return err_incorrect_name;
203 
204  Misc::AssignString(str_tmp1, name);
205  Misc::AssignString(str_tmp2, value);
206 
207  return Add(str_tmp1, str_tmp2, param);
208  }
209 
210 #endif
211 
212 
216  bool Empty() const
217  {
218  return table.empty();
219  }
220 
221 
225  void Clear()
226  {
227  return table.clear();
228  }
229 
230 
234  CIterator Begin() const
235  {
236  return table.begin();
237  }
238 
239 
244  CIterator End() const
245  {
246  return table.end();
247  }
248 
249 
253  ErrorCode EditValue(const std::string & name, const std::string & value, int param = 0)
254  {
255  if( !IsNameCorrect(name) )
256  return err_incorrect_name;
257 
258  Iterator i = table.find(name);
259 
260  if( i == table.end() )
261  return err_unknown_object;
262 
263  i->second.value = value;
264  i->second.param = param;
265 
266  return err_ok;
267  }
268 
269 
270 #ifndef TTMATH_DONT_USE_WCHAR
271 
272 
276  ErrorCode EditValue(const std::wstring & name, const std::wstring & value, int param = 0)
277  {
278  // we should check whether the name (in wide characters) are correct
279  // before calling AssignString() function
280  if( !IsNameCorrect(name) )
281  return err_incorrect_name;
282 
283  Misc::AssignString(str_tmp1, name);
284  Misc::AssignString(str_tmp2, value);
285 
286  return EditValue(str_tmp1, str_tmp2, param);
287  }
288 
289 #endif
290 
291 
295  ErrorCode EditName(const std::string & old_name, const std::string & new_name)
296  {
297  if( !IsNameCorrect(old_name) || !IsNameCorrect(new_name) )
298  return err_incorrect_name;
299 
300  Iterator old_i = table.find(old_name);
301  if( old_i == table.end() )
302  return err_unknown_object;
303 
304  if( old_name == new_name )
305  // the new name is the same as the old one
306  // we treat it as a normal situation
307  return err_ok;
308 
309  ErrorCode err = Add(new_name, old_i->second.value, old_i->second.param);
310 
311  if( err == err_ok )
312  {
313  old_i = table.find(old_name);
314  TTMATH_ASSERT( old_i != table.end() )
315 
316  table.erase(old_i);
317  }
318 
319  return err;
320  }
321 
322 
323 
324 #ifndef TTMATH_DONT_USE_WCHAR
325 
326 
330  ErrorCode EditName(const std::wstring & old_name, const std::wstring & new_name)
331  {
332  // we should check whether the name (in wide characters) are correct
333  // before calling AssignString() function
334  if( !IsNameCorrect(old_name) || !IsNameCorrect(new_name) )
335  return err_incorrect_name;
336 
337  Misc::AssignString(str_tmp1, old_name);
338  Misc::AssignString(str_tmp2, new_name);
339 
340  return EditName(str_tmp1, str_tmp2);
341  }
342 
343 #endif
344 
345 
349  ErrorCode Delete(const std::string & name)
350  {
351  if( !IsNameCorrect(name) )
352  return err_incorrect_name;
353 
354  Iterator i = table.find(name);
355 
356  if( i == table.end() )
357  return err_unknown_object;
358 
359  table.erase( i );
360 
361  return err_ok;
362  }
363 
364 
365 #ifndef TTMATH_DONT_USE_WCHAR
366 
367 
371  ErrorCode Delete(const std::wstring & name)
372  {
373  // we should check whether the name (in wide characters) are correct
374  // before calling AssignString() function
375  if( !IsNameCorrect(name) )
376  return err_incorrect_name;
377 
378  Misc::AssignString(str_tmp1, name);
379 
380  return Delete(str_tmp1);
381  }
382 
383 #endif
384 
385 
389  ErrorCode GetValue(const std::string & name, std::string & value) const
390  {
391  if( !IsNameCorrect(name) )
392  return err_incorrect_name;
393 
394  CIterator i = table.find(name);
395 
396  if( i == table.end() )
397  {
398  value.clear();
399  return err_unknown_object;
400  }
401 
402  value = i->second.value;
403 
404  return err_ok;
405  }
406 
407 
408 #ifndef TTMATH_DONT_USE_WCHAR
409 
413  ErrorCode GetValue(const std::wstring & name, std::wstring & value)
414  {
415  // we should check whether the name (in wide characters) are correct
416  // before calling AssignString() function
417  if( !IsNameCorrect(name) )
418  return err_incorrect_name;
419 
420  Misc::AssignString(str_tmp1, name);
421  ErrorCode err = GetValue(str_tmp1, str_tmp2);
422  Misc::AssignString(value, str_tmp2);
423 
424  return err;
425  }
426 
427 #endif
428 
429 
434  ErrorCode GetValue(const std::string & name, const char ** value) const
435  {
436  if( !IsNameCorrect(name) )
437  return err_incorrect_name;
438 
439  CIterator i = table.find(name);
440 
441  if( i == table.end() )
442  {
443  *value = 0;
444  return err_unknown_object;
445  }
446 
447  *value = i->second.value.c_str();
448 
449  return err_ok;
450  }
451 
452 
453 #ifndef TTMATH_DONT_USE_WCHAR
454 
459  ErrorCode GetValue(const std::wstring & name, const char ** value)
460  {
461  // we should check whether the name (in wide characters) are correct
462  // before calling AssignString() function
463  if( !IsNameCorrect(name) )
464  return err_incorrect_name;
465 
466  Misc::AssignString(str_tmp1, name);
467 
468  return GetValue(str_tmp1, value);
469  }
470 
471 #endif
472 
473 
478  ErrorCode GetValueAndParam(const std::string & name, std::string & value, int * param) const
479  {
480  if( !IsNameCorrect(name) )
481  return err_incorrect_name;
482 
483  CIterator i = table.find(name);
484 
485  if( i == table.end() )
486  {
487  value.empty();
488  *param = 0;
489  return err_unknown_object;
490  }
491 
492  value = i->second.value;
493  *param = i->second.param;
494 
495  return err_ok;
496  }
497 
498 
499 #ifndef TTMATH_DONT_USE_WCHAR
500 
505  ErrorCode GetValueAndParam(const std::wstring & name, std::wstring & value, int * param)
506  {
507  // we should check whether the name (in wide characters) are correct
508  // before calling AssignString() function
509  if( !IsNameCorrect(name) )
510  return err_incorrect_name;
511 
512  Misc::AssignString(str_tmp1, name);
513  ErrorCode err = GetValueAndParam(str_tmp1, str_tmp2, param);
514  Misc::AssignString(value, str_tmp2);
515 
516  return err;
517  }
518 
519 #endif
520 
521 
527  ErrorCode GetValueAndParam(const std::string & name, const char ** value, int * param) const
528  {
529  if( !IsNameCorrect(name) )
530  return err_incorrect_name;
531 
532  CIterator i = table.find(name);
533 
534  if( i == table.end() )
535  {
536  *value = 0;
537  *param = 0;
538  return err_unknown_object;
539  }
540 
541  *value = i->second.value.c_str();
542  *param = i->second.param;
543 
544  return err_ok;
545  }
546 
547 
548 #ifndef TTMATH_DONT_USE_WCHAR
549 
550 
557  ErrorCode GetValueAndParam(const std::wstring & name, const char ** value, int * param)
558  {
559  // we should check whether the name (in wide characters) are correct
560  // before calling AssignString() function
561  if( !IsNameCorrect(name) )
562  return err_incorrect_name;
563 
564  Misc::AssignString(str_tmp1, name);
565 
566  return GetValueAndParam(str_tmp1, value, param);
567  }
568 
569 
570 #endif
571 
572 
577  {
578  return &table;
579  }
580 
581 
582 private:
583 
584  Table table;
585  std::string str_tmp1, str_tmp2;
586 
587 }; // end of class Objects
588 
589 
590 
591 
592 
593 
594 
604 template<class ValueType>
605 class History
606 {
611  struct Item
612  {
613  ValueType key, value;
614  ErrorCode err;
615  };
616 
617 
624  typedef std::list<Item> buffer_type;
625  buffer_type buffer;
626  typename buffer_type::size_type buffer_max_size;
627 
628 public:
629 
635  {
636  buffer_max_size = 15;
637  }
638 
639 
644  History(typename buffer_type::size_type new_size)
645  {
646  buffer_max_size = new_size;
647  }
648 
649 
655  void Add(const ValueType & key, const ValueType & value, ErrorCode err)
656  {
657  Item item;
658  item.key = key;
659  item.value = value;
660  item.err = err;
661 
662  buffer.insert( buffer.end(), item );
663 
664  if( buffer.size() > buffer_max_size )
665  buffer.erase(buffer.begin());
666  }
667 
668 
676  bool Get(const ValueType & key, ValueType & value, ErrorCode & err)
677  {
678  typename buffer_type::iterator i = buffer.begin();
679 
680  for( ; i != buffer.end() ; ++i )
681  {
682  if( i->key == key )
683  {
684  value = i->value;
685  err = i->err;
686  return true;
687  }
688  }
689 
690  return false;
691  }
692 
693 
700  bool Remove(const ValueType & key)
701  {
702  typename buffer_type::iterator i = buffer.begin();
703 
704  for( ; i != buffer.end() ; ++i )
705  {
706  if( i->key == key )
707  {
708  buffer.erase(i);
709  return true;
710  }
711  }
712 
713  return false;
714  }
715 
716 
717 }; // end of class History
718 
719 
720 
738 template<class ValueType>
739 struct CGamma
740 {
752  std::vector<ValueType> fact;
753 
754 
770  std::vector<ValueType> bern;
771 
772 
780 
781 
800  void InitAll();
801  // definition is in ttmath.h
802 };
803 
804 
805 
806 
807 } // namespace
808 
809 #endif
void Clear()
Definition: ttmathobjects.h:225
Definition: ttmathtypes.h:373
bool Empty() const
Definition: ttmathobjects.h:216
bool IsDefined(const std::string &name)
Definition: ttmathobjects.h:139
Item()
Definition: ttmathobjects.h:84
some helpful functions
ErrorCode EditValue(const std::string &name, const std::string &value, int param=0)
Definition: ttmathobjects.h:253
History(typename buffer_type::size_type new_size)
Definition: ttmathobjects.h:644
std::vector< ValueType > bern
Definition: ttmathobjects.h:770
Definition: ttmathobjects.h:605
void InitAll()
Definition: ttmath.h:2813
void Add(const ValueType &key, const ValueType &value, ErrorCode err)
Definition: ttmathobjects.h:655
ErrorCode Delete(const std::string &name)
Definition: ttmathobjects.h:349
Table::const_iterator CIterator
Definition: ttmathobjects.h:91
ErrorCode GetValue(const std::string &name, std::string &value) const
Definition: ttmathobjects.h:389
Definition: ttmathobjects.h:74
ErrorCode Add(const std::string &name, const std::string &value, int param=0)
Definition: ttmathobjects.h:175
std::string value
Definition: ttmathobjects.h:78
ErrorCode EditValue(const std::wstring &name, const std::wstring &value, int param=0)
Definition: ttmathobjects.h:276
std::map< std::string, Item > Table
Definition: ttmathobjects.h:89
ErrorCode GetValue(const std::wstring &name, std::wstring &value)
Definition: ttmathobjects.h:413
bool Remove(const ValueType &key)
Definition: ttmathobjects.h:700
History< ValueType > history
Definition: ttmathobjects.h:779
ErrorCode GetValueAndParam(const std::string &name, const char **value, int *param) const
Definition: ttmathobjects.h:527
ErrorCode
Definition: ttmathtypes.h:349
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
CIterator End() const
Definition: ttmathobjects.h:244
History()
Definition: ttmathobjects.h:634
constants used in the library
bool Get(const ValueType &key, ValueType &value, ErrorCode &err)
Definition: ttmathobjects.h:676
static bool IsNameCorrect(const string_type &name)
Definition: ttmathobjects.h:118
#define TTMATH_ASSERT(expression)
Definition: ttmathtypes.h:660
Table * GetTable()
Definition: ttmathobjects.h:576
Definition: ttmathtypes.h:367
ErrorCode GetValue(const std::string &name, const char **value) const
Definition: ttmathobjects.h:434
ErrorCode GetValueAndParam(const std::string &name, std::string &value, int *param) const
Definition: ttmathobjects.h:478
ErrorCode EditName(const std::string &old_name, const std::string &new_name)
Definition: ttmathobjects.h:295
int param
Definition: ttmathobjects.h:82
Definition: ttmathobjects.h:739
ErrorCode GetValueAndParam(const std::wstring &name, std::wstring &value, int *param)
Definition: ttmathobjects.h:505
std::vector< ValueType > fact
Definition: ttmathobjects.h:752
bool IsDefined(const std::wstring &name)
Definition: ttmathobjects.h:157
CIterator Begin() const
Definition: ttmathobjects.h:234
Item(const std::string &v, int p)
Definition: ttmathobjects.h:85
ErrorCode GetValue(const std::wstring &name, const char **value)
Definition: ttmathobjects.h:459
static bool CorrectCharacter(int c, bool can_be_digit)
Definition: ttmathobjects.h:102
ErrorCode EditName(const std::wstring &old_name, const std::wstring &new_name)
Definition: ttmathobjects.h:330
Definition: ttmathtypes.h:351
Table::iterator Iterator
Definition: ttmathobjects.h:90
Definition: ttmathobjects.h:65
ErrorCode GetValueAndParam(const std::wstring &name, const char **value, int *param)
Definition: ttmathobjects.h:557
ErrorCode Delete(const std::wstring &name)
Definition: ttmathobjects.h:371
Definition: ttmathtypes.h:374
ErrorCode Add(const std::wstring &name, const std::wstring &value, int param=0)
Definition: ttmathobjects.h:197