~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/mod/mth/shl/Rsi.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2011-03-16 21:31:18 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110316213118-gk4k3ez3e5d2huna
Tags: 2.0.0-1
* QA upload.
* New upstream release
* Debian source format is 3.0 (quilt)
* Fix debhelper-but-no-misc-depends
* Fix ancient-standards-version
* Fix package-contains-linda-override
* debhelper compatibility is 7
* Fix dh-clean-k-is-deprecated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - Rsi.cpp                                                                 -
 
3
// - afnix:mth module - real sparse interface implementation                 -
 
4
// ---------------------------------------------------------------------------
 
5
// - This program is free software;  you can redistribute it  and/or  modify -
 
6
// - it provided that this copyright notice is kept intact.                  -
 
7
// -                                                                         -
 
8
// - This program  is  distributed in  the hope  that it will be useful, but -
 
9
// - without  any  warranty;  without  even   the   implied    warranty   of -
 
10
// - merchantability or fitness for a particular purpose.  In no event shall -
 
11
// - the copyright holder be liable for any  direct, indirect, incidental or -
 
12
// - special damages arising in any way out of the use of this software.     -
 
13
// ---------------------------------------------------------------------------
 
14
// - copyright (c) 1999-2011 amaury darsch                                   -
 
15
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Rsi.hpp"
 
18
#include "Real.hpp"
 
19
#include "Vector.hpp"
 
20
#include "Integer.hpp"
 
21
#include "Runnable.hpp"
 
22
#include "QuarkZone.hpp"
 
23
#include "Exception.hpp"
 
24
 
 
25
namespace afnix {
 
26
 
 
27
  // -------------------------------------------------------------------------
 
28
  // - class section                                                         -
 
29
  // -------------------------------------------------------------------------
 
30
 
 
31
  // create a default sparse object
 
32
 
 
33
  Rsi::Rsi (void) {
 
34
    d_size = 0;
 
35
  }
 
36
 
 
37
  // create a sparse object by size
 
38
 
 
39
  Rsi::Rsi (const t_long size) {
 
40
    // check the size
 
41
    if (size < 0) {
 
42
      throw Exception ("size-error", "invalid sparse object size");
 
43
    }
 
44
    d_size = size;
 
45
  }
 
46
 
 
47
  // get the sparse object size
 
48
 
 
49
  t_long Rsi::getsize (void) const {
 
50
    rdlock ();
 
51
    try {
 
52
      t_long result = d_size;
 
53
      unlock ();
 
54
      return result;
 
55
    } catch (...) {
 
56
      unlock ();
 
57
      throw;
 
58
    }
 
59
  }
 
60
 
 
61
  // -------------------------------------------------------------------------
 
62
  // - object section                                                        -
 
63
  // -------------------------------------------------------------------------
 
64
 
 
65
  // the quark zone
 
66
  static const long QUARK_ZONE_LENGTH = 5;
 
67
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
68
 
 
69
  // the rsi supported quarks
 
70
  static const long QUARK_SET     = zone.intern ("set");
 
71
  static const long QUARK_LENGTH  = zone.intern ("length");
 
72
  static const long QUARK_GETIDX  = zone.intern ("get-index");
 
73
  static const long QUARK_GETVAL  = zone.intern ("get-value");
 
74
  static const long QUARK_GETSIZE = zone.intern ("get-size");
 
75
 
 
76
  // return true if the given quark is defined
 
77
 
 
78
  bool Rsi::isquark (const long quark, const bool hflg) const {
 
79
    rdlock ();
 
80
    if (zone.exists (quark) == true){
 
81
      unlock ();
 
82
      return true;
 
83
    }
 
84
    bool result = hflg ? Object::isquark (quark, hflg) : false;
 
85
    unlock ();
 
86
    return result;
 
87
  }
 
88
  
 
89
  // apply this object with a set of arguments and a quark
 
90
  
 
91
  Object* Rsi::apply (Runnable* robj, Nameset* nset, const long quark,
 
92
                      Vector* argv) {
 
93
    // get the number of arguments
 
94
    long argc = (argv == nilp) ? 0 : argv->length ();
 
95
 
 
96
    // dispatch 0 argument
 
97
    if (argc == 0) {
 
98
      if (quark == QUARK_LENGTH)  return new Integer (length  ());
 
99
      if (quark == QUARK_GETSIZE) return new Integer (getsize ());
 
100
    }
 
101
    // dispatch 1 argument
 
102
    if (argc == 1) {
 
103
      if (quark == QUARK_GETIDX) {
 
104
        t_long pos = argv->getlong (0);
 
105
        return new Integer (getidx (pos));
 
106
      }
 
107
      if (quark == QUARK_GETVAL) {
 
108
        t_long pos = argv->getlong (0);
 
109
        return new Real (getval (pos));
 
110
      }
 
111
    }
 
112
    // dispatch 2 arguments
 
113
    if (argc == 2) {
 
114
      if (quark == QUARK_SET) {
 
115
        t_long sidx = argv->getlong (0);
 
116
        t_real sval = argv->getreal (1);
 
117
        return new Integer (set (sidx, sval));
 
118
      }
 
119
    }
 
120
    // call the object
 
121
    return Object::apply (robj, nset, quark, argv);
 
122
  }
 
123
}