~ubuntu-branches/ubuntu/hoary/scilab/hoary

« back to all changes in this revision

Viewing changes to macros/mtlb/createstruct.sci

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2005-01-09 22:58:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050109225821-473xr8vhgugxxx5j
Tags: 3.0-12
changed configure.in to build scilab's own malloc.o, closes: #255869

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function  M=createstruct(index,N)
 
2
// Authors: S. Steer, F. Delebecque, V. Couvert
 
3
// Copyright INRIA 
 
4
// Create a struct with field pointed by index equal to N
 
5
 
 
6
// If index is not a list, creation is done by routines
 
7
if type(index)==15 then
 
8
  
 
9
  // When only two indexes
 
10
  if size(index)==2 then
 
11
    
 
12
    // Second index is a fieldname
 
13
    if type(index(2))==10 then
 
14
      
 
15
      // First index is a numerical value
 
16
      if type(index(1))==1 then
 
17
        // X(p).f = y  -> index=list(p,f)
 
18
        if mtlb_mode() then
 
19
          Dims=[1 index(1)]
 
20
        else
 
21
          Dims=[index(1) 1]
 
22
        end
 
23
        M=mlist(["st","dims",index(2)],int32(Dims));
 
24
        nmax=prod(Dims);
 
25
        Li=list();
 
26
        for kl=1:nmax
 
27
          Li(kl)=[];
 
28
        end
 
29
        Li($)=N;
 
30
        setfield(3,Li,M);
 
31
        return;
 
32
      // First index is a list of numerical values      
 
33
      elseif type(index(1))==15
 
34
        // X(p,q[,...]).f=z -> index=list(list(p,q[,...]),f)
 
35
        Dims=[];
 
36
        for kDims=1:lstsize(index(1))
 
37
          Dims=[Dims,int32(index(1)(kDims))];
 
38
        end
 
39
        Li=list();
 
40
        for kl=1:prod(double(Dims))
 
41
          Li(kl)=[];
 
42
        end
 
43
        Li($)=N;
 
44
        M=mlist(["st","dims",index(2)],Dims,Li);
 
45
        return;
 
46
      // First index is also a fieldname        
 
47
      else
 
48
        // X.f.g=y -> index=list(f,g)
 
49
        M=mlist(["st","dims",index(1)],int32([1,1]),...
 
50
            mlist(["st","dims",index(2)],int32([1,1]),N));
 
51
        return;
 
52
      end
 
53
    
 
54
    // Second index is a fieldname  
 
55
    else
 
56
      // X.f(p[,q,...])=y : index=list(f,list(p[,q,...]))  
 
57
       
 
58
      if typeof(N)=="st" then // When recursive call of createstruct
 
59
        if type(index(2))==15 then // index=list(f,list(p,q[,...])) 
 
60
          Dims=list2vec(index(2))'
 
61
        else // index=list(f,p)
 
62
          if mtlb_mode() then
 
63
            Dims=[1 index(2)]
 
64
          else
 
65
            Dims=[index(2) 1]
 
66
          end
 
67
        end
 
68
        kmax=prod(Dims)
 
69
        z=list()
 
70
        for kl=1:kmax
 
71
          z(kl)=[]
 
72
        end
 
73
        z(kmax)=getfield(3,N)
 
74
        z=mlist(getfield(1,N),int32(Dims),z);
 
75
      else
 
76
        z(index(2)(:))=N;
 
77
      end
 
78
      M=mlist(["st","dims",index(1)],int32([1,1]),z);
 
79
      return;
 
80
    end
 
81
  
 
82
  // Any number of indexes <> 2  
 
83
  else
 
84
  
 
85
    // Last index is a fieldname -> init struct
 
86
    if type(index($))==10 then
 
87
      M=mlist(["st","dims",index($)],int32([1,1]),N);
 
88
      index($)=null();
 
89
    // Last index is a numerical value or a list of numerical values  
 
90
    elseif type(index($))==1 | type(index($))==15 then
 
91
 
 
92
      // If only one index value (happens when recursive calls to createstruct)
 
93
      if size(index)==1 then
 
94
        if typeof(N)=="st" then
 
95
          if type(index($))==15 then
 
96
            Dims=list2vec(index($))'
 
97
          else
 
98
            if mtlb_mode() then
 
99
              Dims=[1 index($)]
 
100
            else
 
101
              Dims=[index($) 1]
 
102
            end
 
103
          end
 
104
          kmax=prod(Dims)
 
105
          z=list()
 
106
          for kl=1:kmax
 
107
            z(kl)=[]
 
108
          end
 
109
          z(kmax)=getfield(3,N)
 
110
          M=mlist(getfield(1,N),int32(Dims),z);
 
111
        else
 
112
          M(index($)(:))=N;
 
113
        end
 
114
        index($)=null()
 
115
      // More than one index value      
 
116
      else
 
117
        if typeof(N)=="st" then
 
118
          if type(index($))==15 then
 
119
            Dims=list2vec(index($))'
 
120
          else
 
121
            if mtlb_mode() then
 
122
              Dims=[1 index($)]
 
123
            else
 
124
              Dims=[index($) 1]
 
125
            end
 
126
          end
 
127
          kmax=prod(Dims)
 
128
          z=list()
 
129
          for kl=1:kmax
 
130
            z(kl)=[]
 
131
          end
 
132
          z(kmax)=getfield(3,N)
 
133
          z=mlist(getfield(1,N),int32(Dims),z);
 
134
        else
 
135
          z(index($)(:))=N;
 
136
        end
 
137
        M=mlist(["st","dims",index($-1)],int32([1,1]),z);
 
138
        index($)=null()
 
139
        index($)=null()
 
140
      end
 
141
    else
 
142
      // This case should not happen (unknown kind of index)
 
143
      error("Not implemented");
 
144
    end
 
145
    if index==list() then
 
146
      return
 
147
    end
 
148
    M=createstruct(index,M);
 
149
    return;
 
150
  end
 
151
else
 
152
  if type(index)==10 then
 
153
    M=mlist(["st","dims",index($)],int32([1,1]),N);
 
154
  else
 
155
    error("Not implemented");
 
156
  end
 
157
end
 
158
endfunction