File: Class.cpp

package info (click to toggle)
freemat 4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 174,736 kB
  • ctags: 67,053
  • sloc: cpp: 351,060; ansic: 255,892; sh: 40,590; makefile: 4,323; perl: 4,058; asm: 3,313; pascal: 2,718; fortran: 1,722; ada: 1,681; ml: 1,360; cs: 879; csh: 795; python: 430; sed: 162; lisp: 160; awk: 5
file content (1106 lines) | stat: -rw-r--r-- 31,651 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
/*
 * Copyright (c) 2002-2006 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "Class.hpp"
#include "Context.hpp"
#include "Struct.hpp"
#include "Array.hpp"
#include "Algorithms.hpp"

// The meta-info for a class
struct UserClassMetaInfo {
  StringVector fieldNames;
  StringVector parentClasses;
};

// Class information is mapped to a class template through an ID number
static QMutex classMutex;
static QMap<QString,UserClassMetaInfo> classTable;

// some behavioral observations on classes.
//  The first call to "class" is the definitive one.
//  The exact order of the structure fieldnames must be the same 
//     for all objects
//  The list of parent objects must also be the same for all objects
//  So, classes are stored as the following:
//     class UserClass {
//         StringVector fieldNames;
//         StringVector parentClasses;
//     }
//  Also, somewhere we require a table that
//  tracks the hierarchy relationship of the classes.
// 
// To Do:
//   change grepping code to look for classes
//   change function eval code to handle classes
//
// These are both done.  Next is the issue of parent classes.
// What does it mean when we have one or more parent classes?
// The structure is simple enough (simply add a new field with
// the name of the parent class).  But what about methods?
// When we call a method of the parent class on the current class
// what does it get passed?
//
//  The answer:
//   Suppose g is of class1, and inherits class2, and class3.
//   Then g has fields class2 and class3.
//   When we call 
//     method(g), where method is a member function of class2, then
//   effectively, the output is the same as
//     method(g.class2)
//     p = g
//     p.class2 = method(g.class2)
//   Odd - that's not quite right...  it must be more complicated than that
// Class related issues
//    rhs subscripting
//    assignment
//
// What about function pointers?   - done
//
// Need overload capability for
//   And
//   Or
//   Uplus
//   a(s1,s2,...,sn) - subsref
//   a(s1, ..., sn) - subsasgn
//   b(a) - subsindex
//   [a b] - horzcat
//   [a; b] - vertcat
//   Colon
//   end (!)
//
// More ideas on overloading classes...
//
// What happens when the parent classes are different sizes - resolved
//    force parent classes to be the same size as the created object
//
// In c++, polymorphism is done through the notion of a pointer and
// type casting.  But we can't do exactly the same thing... Because
// when we type-cast, only methods and fields from the type-cast
// object are present... 
//
// What we want is 
//   a.class1.class2.foo = 32
// In this case, a is of some class (e.g., class3).  But we want to
// call some method on a that belongs to class2.  now, inside the
// method, we want something like
//    x.foo = 32
// but _x_ has to be tagged with prefix information, because _x_ is
// really of class class3.  The tag has to be on the object because
// if there are multiple arguments to the function, they can be
// typecast at different levels.  Also, it tracks only the _instance_
// of the array, not the core array itself.  So the information has
// to be tagged on the array somehow.
//
// One idea is to replace the class name with the class path.  So if
// a is of type class3, but we want to access it as a type class2,
// we "cast" it to type class3:class1:class2.  Then, when accessing
// members of "a", we use the class list to determine the indexing
// sequence.  This casting operation can be done at the dispatch
// level.  Because the "struct" operation simply strips the class name
// from the object, it will still return the intact data array.
//
// To track precedence...
// 1.  Assume that inheritance and precedence do not interact (only the
// outermost class determines precedence).
// 2.  For each class, a list of superior classes is provided.
// 3.  A inf B <--> B sup A
// Precedence is then a simple question of testing interactions.

static Array ClassAux(const Array &s, QString classname, const StringVector &parentNames, 
		      const ArrayVector &parents) {
  QMutexLocker lock(&classMutex);
  if (!s.isEmpty() && (s.dataClass() != Struct))
    throw Exception("first argument to 'class' function must be a structure");
  // First look up the class ID
  UserClassMetaInfo meta;
  if (!classTable.contains(classname)) {
    meta.fieldNames = FieldNames(s);
    for (int i=0;i<parentNames.size();i++) {
      if (!classTable.contains(parentNames[i]))
	throw Exception("parent object types must be defined at time of class creation");
      meta.parentClasses.push_back(parentNames[i]);
    }
    classTable[classname] = meta;
  } else
    meta = classTable.value(classname);
  StringVector s_fields(FieldNames(s));
  if (meta.fieldNames != s_fields)
    throw Exception("fieldnames of structure provided must match the fieldnames for the registered class");
  // Now check to make sure all of the parent objects are the same size as the source object
  for (int i=0;i<parents.size();i++) 
    if (s.dimensions() != parents[i].dimensions())
      throw Exception("parent object much match dimensions of the structure used to make the object");
  Array retval(Struct);
  StructArray &rp(retval.structPtr());
  const StructArray &sp(s.constStructPtr());
  // First append the old fields
  for (int i=0;i<s_fields.size();i++)
    rp[s_fields.at(i)] = sp[s_fields.at(i)];
  // Now the parent members
  for (int i=0;i<parentNames.size();i++)
    rp[parentNames[i]] = BasicArray<Array>(parents[i]);
  rp.setClassPath(StringVector() << classname);
  rp.updateDims();
  return retval;
}

//!
//@Module CLASS Class Support Function
//@@Section CLASS
//@@Usage
//There are several uses for the @|class| function.  The first
//version takes a single argument, and returns the class of
//that variable.  The syntax for this form is
//@[
//  classname = class(variable)
//@]
//and it returns a string containing the name of the class for
//@|variable|.  The second form of the class function is used
//to construct an object of a specific type based on a structure
//which contains data elements for the class.  The syntax for
//this version is
//@[
//  classvar = class(template, classname, parent1, parent2,...)
//@]
//This should be called inside the constructor for the class.
//The resulting class will be of the type @|classname|, and will
//be derived from @|parent1|, @|parent2|, etc.  The @|template|
//argument should be a structure array that contains the members
//of the class.  See the @|constructors| help for some details
//on how to use the @|class| function.  Note that if the
//@|template| argument is an empty structure matrix, then the
//resulting variable has no fields beyond those inherited from
//the parent classes.
//!

//!
//@Module CONSTRUCTORS Class Constructors
//@@Section CLASS
//@@Usage
//When designing a constructor for a FreeMat class, you should
//design the constructor to take a certain form.  The following
//is the code for the sample @|mat| object 
//@[
//function p = mat(a)
//  if (nargin == 0)
//    p.c = [];
//    p = class(p,'mat');
//  elseif isa(a,'mat')
//    p = a;
//  else
//    p.c = a;
//    p = class(p,'mat');
//  end
//@]
//Generally speaking when it is provided with zero arguments, the
//constructor returns a default version of the class using a template
//structure with the right fields populated with default values.  If
//the constructor is given a single argument that matches the class
//we are trying to construct, the constructor passes through the
//argument.  This form of the constructor is used for type conversion.
//In particular, 
//@[
//   p = mat(a)
//@] 
//guarantees that @|p| is an array of class @|mat|.  The last form
//of the constructor builds a class object given the input.  The
//meaning of this form depends on what makes sense for your class.
//For example, for a polynomial class, you may want to pass in the
//coefficients of the polynomial.
//!

//!
//@Module HORZCAT Overloaded Horizontal Concatenation
//@@Section CLASS
//@@Usage
//This is a method for a class that is invoked to concatenate two or more
//variables of the same class type together.  Besides being called
//when you invoke
//@[
//   c = horzcat(a,b,c)
//@]
//when @|a| is a class, it is also called for 
//@[
//   c = [a,b,c]
//@]
//when one of these variables is a class.  The exact meaning of
//horizontal concatenation depends on the class you have designed.
//!

//!
//@Module VERTCAT Overloaded Vertical Concatenation
//@@Section CLASS
//@@Usage
//This is a method for a class that is invoked to concatenate two or more
//variables of the same class type together.  Besides being called when
//you invoke
//@[
//   c = vertcat(a,b,c)
//@]
//when @|a| is a class, it is also called for
//@[
//   c = [a;b;c]
//@]
//when one of the variables is a class.  The exact meaning of 
//vertical concatenation depends on the class you have designed.
//!
  
//!
//@Module OR Overloaded Logical Or Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked to combine two variables using a
//logical or operator, and is invoked when you call
//@[
//   c = or(a,b)
//@]
//or for 
//@[
//   c = a | b
//@]
//!

//!
//@Module AND Overloaded Logical And Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked to combine two variables using a
//logical and operator, and is invoked when you call
//@[
//   c = and(a,b)
//@]
//or for 
//@[
//   c = a & b
//@]
//!

//!
//@Module LT Overloaded Less Than Comparison Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked to compare two variables using a
//less than comparison operator, and is invoked when you call
//@[
//   c = lt(a,b)
//@]
//or for 
//@[
//   c = a < b
//@]
//!

//!
//@Module GT Overloaded Greater Than Comparison Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked to combine two variables using a
//greater than comparison operator, and is invoked when you call
//@[
//   c = gt(a,b)
//@]
//or for 
//@[
//   c = a > b
//@]
//!
  
//!
//@Module LE Overloaded Less-Than-Equals Comparison Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked to compare two variables using a
//less than or equals comparison operator, and is invoked when you call
//@[
//   c = le(a,b)
//@]
//or for 
//@[
//   c = a <= b
//@]
//!

//!
//@Module GE Overloaded Greater-Than-Equals Comparison Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked to combine two variables using a
//greater than or equals comparison operator, and is invoked when you call
//@[
//   c = ge(a,b)
//@]
//or for 
//@[
//   c = a >= b
//@]
//!
  
//!
//@Module EQ Overloaded Equals Comparison Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked to combine two variables using an
//equals comparison operator, and is invoked when you call
//@[
//   c = eq(a,b)
//@]
//or for 
//@[
//   c = a == b
//@]
//!

//!
//@Module NE Overloaded Not-Equals Comparison Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked to combine two variables using a
//not-equals comparison operator, and is invoked when you call
//@[
//   c = ne(a,b)
//@]
//or for 
//@[
//   c = a != b
//@]
//!

//!
//@Module PLUS Overloaded Addition Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when two variables are added
//and is invoked when you call
//@[
//   c = plus(a,b)
//@]
//or for
//@[
//   c = a + b
//@]
//!

//!
//@Module MINUS Overloaded Addition Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when two variables are subtracted
//and is invoked when you call
//@[
//   c = minus(a,b)
//@]
//or for
//@[
//   c = a - b
//@]
//!

//!
//@Module MTIMES Overloaded Matrix Multiplication Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when two variables are multiplied
//using the matrix operator and is invoked when you call
//@[
//   c = mtimes(a,b)
//@]
//or for
//@[
//   c = a * b
//@]
//!

//!
//@Module TIMES Overloaded Multiplication Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when two variables are multiplied
//and is invoked when you call
//@[
//   c = times(a,b)
//@]
//or for
//@[
//   c = a .* b
//@]
//!
  
//!
//@Module RDIVIDE Overloaded Right Divide Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when two variables are divided
//and is invoked when you call
//@[
//   c = rdivide(a,b)
//@]
//or for
//@[
//   c = a ./ b
//@]
//!

//!
//@Module LDIVIDE Overloaded Left Divide Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when two variables are divided
//and is invoked when you call
//@[
//   c = ldivide(a,b)
//@]
//or for
//@[
//   c = a .\ b
//@]
//!

//!
//@Module MRDIVIDE Overloaded Matrix Right Divide Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when two variables are divided
//using the matrix divide operator, and is invoked when you call
//@[
//   c = mrdivide(a,b)
//@]
//or for
//@[
//   c = a / b
//@]
//!

//!
//@Module MLDIVIDE Overloaded Matrix Left Divide Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when two variables are divided
//using the matrix (left) divide operator, and is invoked when
//you call
//@[
//   c = mldivide(a,b)
//@]
//or for
//@[
//   c = a \ b
//@]
//!


//!
//@Module UMINUS Overloaded Unary Minus Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when a variable is negated,
//and is invoked when you call
//@[
//   c = uminus(a)
//@]
//or for 
//@[
//   c = -a
//@]
//!

//!
//@Module UPLUS Overloaded Unary Plus Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when a variable is preceeded by a "+",
//and is invoked when you call
//@[
//   c = uplus(a)
//@]
//or for 
//@[
//   c = +a
//@]
//!

//!
//@Module NOT Overloaded Logical Not Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when a variable is logically
//inverted, and is invoked when you call
//@[
//   c = not(a)
//@]
//or for
//@[
//   c = ~a
//@]
//!

//!
//@Module MPOWER Overloaded Matrix Power Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when one variable is raised
//to another variable using the matrix power operator, and
//is invoked when you call
//@[
//  c = mpower(a,b)
//@]
//or
//@[
//  c = a^b
//@]
//!

//!
//@Module POWER Overloaded Power Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when one variable is raised
//to another variable using the dot-power operator, and is
//invoked when you call
//@[
//   c = power(a,b)
//@]
//or
//@[
//   c = a.^b
//@]
//!

//!
//@Module CTRANSPOSE Overloaded Conjugate Transpose Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when a variable has the
//conjugate transpose operator method applied, and is invoked
//when you call
//@[
//   c = ctranspose(a)
//@]
//or
//@[
///  c = a'
//@]
//!

//!
//@Module TRANSPOSE Overloaded Transpose Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked when a variable has the
//transpose operator method applied, and is invoked
//when you call
//@[
//   c = transpose(a)
//@]
//or
//@[
///  c = a.'
//@]
//!

//!
//@Module COLON Overloaded Colon Operator
//@@Section CLASS
//@@Usage
//This is a method that is invoked in one of two forms, either
//the two argument version
//@[
//  c = colon(a,b)
//@]
//which is also called using the notation
//@[
//  c = a:b
//@]
//and the three argument version
//@[
//  d = colon(a,b,c)
//@]
//which is also called using the notation
//@[
//  d = a:b:c
//@]
//!

//!
//@Module SUBSREF Overloaded Class Indexing
//@@Section CLASS
//@@Usage
//This method is called for expressions of the form
//@[
//  c = a(b), c = a{b}, c = a.b
//@]
//and overloading the @|subsref| method allows you
//to define the meaning of these expressions for
//objects of class @|a|.  These expressions are
//mapped to a call of the form
//@[
//  b = subsref(a,s)
//@]
//where @|s| is a structure array with two fields. The
//first field is
//\begin{itemize}
//\item @|type|  is a string containing either @|'()'| or
// @|'{}'| or @|'.'| depending on the form of the call.
//\item @|subs| is a cell array or string containing the
// the subscript information.
//\end{itemize}
//When multiple indexing experssions are combined together
//such as @|b = a(5).foo{:}|, the @|s| array contains
//the following entries
//@[
//  s(1).type = '()'  s(1).subs = {5}
//  s(2).type = '.'   s(2).subs = 'foo'
//  s(3).type = '{}'  s(3).subs = ':'
//@]
//!

//!
//@Module SUBSASGN Overloaded Class Assignment
//@@Section CLASS
//@@Usage
//This method is called for expressions of the form
//@[
//  a(b) = c, a{b} = c, a.b = c
//@]
//and overloading the @|subsasgn| method can allow you
//to define the meaning of these expressions for
//objects of class @|a|.  These expressions are mapped
//to a call of the form
//@[
//  a = subsasgn(a,s,b)
//@]
//where @|s| is a structure array with two fields.  The
//first field is
//\begin{itemize}
//\item @|type|  is a string containing either @|'()'| or
// @|'{}'| or @|'.'| depending on the form of the call.
//\item @|subs| is a cell array or string containing the
// the subscript information.
//\end{itemize}
//When multiple indexing experssions are combined together
//such as @|a(5).foo{:} = b|, the @|s| array contains
//the following entries
//@[
//  s(1).type = '()'  s(1).subs = {5}
//  s(2).type = '.'   s(2).subs = 'foo'
//  s(3).type = '{}'  s(3).subs = ':'
//@]
//!

//!
//@Module SUBSINDEX Overloaded Class Indexing
//@@Section CLASS
//@@Usage
//This method is called for classes in the expressions
//of the form
//@[
//  c = subsindex(a)
//@]
//where @|a| is an object, and @|c| is an index vector.
//It is also called for
//@[
//  c = b(a)
//@]
//in which case @|subsindex(a)| must return a vector containing
//integers between @|0| and @|N-1| where @|N| is the number
//of elements in the vector @|b|.
//@@Signature
//function class ClassFunction
//input varargin
//output classname
//!
ArrayVector ClassFunction(int nargout, const ArrayVector& arg) {
  if (arg.size() == 0)
    throw Exception("class function requires at least one argument");
  if (arg.size() == 1) 
    return ArrayVector(Array(arg[0].className()));
  ArrayVector parents;
  StringVector parentNames;
  for (int i=2;i<arg.size();i++) {
    if (!arg[i].isUserClass())
      throw Exception("parent objects must be user defined classes");
    parents.push_back(arg[i]);
    parentNames.push_back(arg[i].className());
  }
  return ArrayVector(ClassAux(arg[0],arg[1].asString(),parentNames,parents));
}

static QVector<int> MarkUserClasses(ArrayVector t) {
  QVector<int> set;
  for (int j=0;j<t.size();j++) 
    if (t[j].isUserClass()) set.push_back(j);
  return set;
}
 
static bool ClassSearchOverload(Interpreter* eval, ArrayVector t, 
				QVector<int> userset, FuncPtr &val,
				QString name) {
  bool overload = false;
  int k = 0;
  while (k<userset.size() && !overload) {
    overload = 
      eval->getContext()->lookupFunction(ClassMangleName(t[userset[k]].className(),name),val);
    if (!overload) k++;
  }
  return overload;
}

Array ClassMatrixConstructor(ArrayMatrix m, Interpreter* eval) {
  // Process the rows...
  // If a row has no user defined classes, then
  // use the standard matrixConstructor
  ArrayVector rows;
  for (int i=0;i<m.size();i++) {
    // check this row - make a list of columns that are
    // user classes
    QVector<int> userset(MarkUserClasses(m[i]));
    if (userset.empty()) {
      ArrayMatrix n;
      n.push_back(m[i]);
      rows.push_back(MatrixConstructor(n));
    } else {
      FuncPtr val;
      bool horzcat_overload = ClassSearchOverload(eval,m[i],userset,val,"horzcat");
      if (!horzcat_overload)
	throw Exception("no overloaded version of horzcat found");
      // scan through the list of user defined classes - look
      // for one that has "horzcat" overloaded
      val->updateCode(eval);
      ArrayVector p;
      p = eval->doFunction(val,m[i],1);
      if (!p.empty())
	rows.push_back(p[0]);
      else {
	eval->warningMessage("'horzcat' called for user defined class and it returned nothing.  Substituting empty array for result.");
	rows.push_back(EmptyConstructor());
      }
    }
  }
  // Check for a singleton - handle with special case
  if (rows.size() == 1)
    return rows[0];
  // At this point we have a vector arrays that have to vertically
  // concatenated.  There may not be any objects in it, so we have 
  // to rescan.
  QVector<int> userset(MarkUserClasses(rows));
  if (userset.empty()) {
    // OK - we don't have any user-defined classes anymore,
    // so we want to call matrix constructor, which needs
    // an ArrayMatrix instead of an ArrayVector.
    ArrayMatrix ref;
    for (int i=0;i<rows.size();i++)
      ref.push_back(ArrayVector(rows[i]));
    return MatrixConstructor(ref);
  } else {
    // We do have a user defined class - look for a vertcat defined
    FuncPtr val;
    bool vertcat_overload = ClassSearchOverload(eval,rows,userset,val,"vertcat");
    if (!vertcat_overload)
      throw Exception("no overloaded version of vertcat found");
    val->updateCode(eval);
    ArrayVector p;
    p = eval->doFunction(val,rows,1);
    if (!p.empty())
      return p[0];
    else
      return EmptyConstructor();
  }
  return EmptyConstructor();
}

Array ClassUnaryOperator(Array a, QString funcname, Interpreter* eval) {
  FuncPtr val;
  ArrayVector m, n;
  if (eval->getContext()->lookupFunction(ClassMangleName(a.className(),funcname),val)) {
    val->updateCode(eval);
    m.push_back(a);
    n = eval->doFunction(val,m,1);
    if (!n.empty())
      return n[0];
    else
      return EmptyConstructor();
  }
  throw Exception("Unable to find a definition of " + 
		  funcname + " for arguments of class " + a.className());
}

bool ClassResolveFunction(Interpreter* eval, Array& args, QString funcName, FuncPtr& val) {
  Context *context = eval->getContext();
  // First try to resolve to a method of the base class
  if (context->lookupFunction(ClassMangleName(args.className(),funcName),val)) {
    return true;
  } 
  if (!args.isUserClass()) 
    throw Exception("class resolve called with non user class");
  UserClassMetaInfo einfo = classTable[args.className()];
  // Now check the parent classes
  for (int i=0;i<einfo.parentClasses.size();i++) {
    if (context->lookupFunction(ClassMangleName(einfo.parentClasses.at(i),funcName),val)) {
      StringVector argClass(args.className());
      argClass.push_back(einfo.parentClasses.at(i));
      args.structPtr().setClassPath(argClass);
      return true;
    }
  }
  // Nothing matched, return
  return false;
}


// TODO - add "inferiorto", etc and class precedence

static Array ClassBiOp(Array a, Array b, FuncPtr val, Interpreter *eval) {
  val->updateCode(eval);
  ArrayVector m, n;
  m.push_back(a); m.push_back(b);
  n = eval->doFunction(val,m,1);
  if (!n.empty())
    return n[0];
  else
    return EmptyConstructor();
}

static Array ClassTriOp(Array a, Array b, Array c, FuncPtr val, Interpreter *eval) {
  val->updateCode(eval);
  ArrayVector m, n;
  m.push_back(a); m.push_back(b); m.push_back(c);
  n = eval->doFunction(val,m,1);
  if (!n.empty())
    return n[0];
  else
    return EmptyConstructor();
}

Array ClassTrinaryOperator(Array a, Array b, Array c, QString funcname,
			   Interpreter* eval) {
  FuncPtr val;
  if (a.isUserClass()) {
    if (eval->getContext()->lookupFunction(ClassMangleName(a.className(),funcname),val)) 
      return ClassTriOp(a,b,c,val,eval);
    throw Exception(QString("Unable to find a definition of ") + funcname + 
		    " for arguments of class " + a.className());
  } else if (b.isUserClass()) {
    if (eval->getContext()->lookupFunction(ClassMangleName(b.className(),funcname),val)) 
      return ClassTriOp(a,b,c,val,eval);
    throw Exception(QString("Unable to find a definition of ") + funcname + 
		    " for arguments of class " + b.className());
  } else if (c.isUserClass()) {
    if (eval->getContext()->lookupFunction(ClassMangleName(c.className(),funcname),val)) 
      return ClassTriOp(a,b,c,val,eval);
    throw Exception(QString("Unable to find a definition of ") + funcname + 
		    " for arguments of class " + c.className());
  }
  throw Exception("unexpected argument types for classtrinaryoperator");
}

Array ClassBinaryOperator(Array a, Array b, QString funcname,
			  Interpreter* eval) {
  FuncPtr val;
  if (a.isUserClass()) {
    if (eval->getContext()->lookupFunction(ClassMangleName(a.className(),funcname),val)) 
      return ClassBiOp(a,b,val,eval);
    throw Exception(QString("Unable to find a definition of ") + funcname + 
		    " for arguments of class " + a.className());
  } else if (b.isUserClass()) {
    if (eval->getContext()->lookupFunction(ClassMangleName(b.className(),funcname),val)) 
      return ClassBiOp(a,b,val,eval);
    throw Exception(QString("Unable to find a definition of ") + funcname + 
		    " for arguments of class " + b.className());
  }
  throw Exception("unexpected argument types for classbinaryoperator");
}

Array IndexExpressionToStruct(Interpreter* eval, const Tree & t, Array r) {
  StringVector fNames;
  fNames.push_back("type");
  fNames.push_back("subs");
  ArrayVector type_args;
  ArrayVector subs_args;
  for (int index=1;index < t.numChildren();index++) {
    if (t.child(index).is(TOK_PARENS)) {
      ArrayVector m;
      const Tree & s(t.child(index));
      for (int p=0;p<s.numChildren();p++)
	eval->multiexpr(s.child(p),m);
      eval->subsindex(m);
      //        m = eval->varExpressionList(t[index].children(),r);
      //        // Scan through the expressions... adjust for "colon" calls
      //        AdjustColonCalls(m,t[index].children());
      if (m.size() == 0) 
	throw Exception("Expected indexing expression!");
      // Take the arguments and push them into a cell array...
      ArrayMatrix q;	q.push_back(m);
      type_args.push_back(Array(QString("()")));
      subs_args.push_back(CellConstructor(q));
    }
    if (t.child(index).is(TOK_BRACES)) {
      ArrayVector m;
      const Tree & s(t.child(index));
      for (int p=0;p<s.numChildren();p++)
	eval->multiexpr(s.child(p),m);
      eval->subsindex(m);
      //        m = eval->varExpressionList(t[index].children(),r);
      //        AdjustColonCalls(m,t[index].children());
      if (m.size() == 0) 
	throw Exception("Expected indexing expression!");
      // Take the arguments and push them into a cell array...
      ArrayMatrix q;	q.push_back(m);
      type_args.push_back(Array(QString("{}")));
      subs_args.push_back(CellConstructor(q));
    }
    if (t.child(index).is('.')) {
      type_args.push_back(Array(QString(".")));
      subs_args.push_back(Array(t.child(index).first().text()));
    }
  }
  Array retval(Struct);
  StructArray& rp(retval.structPtr());
  rp["type"] = ArrayVectorToBasicArray(type_args);
  rp["subs"] = ArrayVectorToBasicArray(subs_args);
  rp.updateDims();
  return retval;
}
  
static ArrayVector ClassSubsrefCall(Interpreter* eval, const Tree & t, Array r, FuncPtr val) {
  ArrayVector p;
  p.push_back(r);
  p.push_back(IndexExpressionToStruct(eval,t, r));
  val->updateCode(eval);
  ArrayVector n = eval->doFunction(val,p,1);
  return n;
}

// What is special here...  Need to be able to do field indexing
// 
ArrayVector ClassRHSExpression(Array r, const Tree & t, Interpreter* eval) {
  Array q;
  Array n, p;
  FuncPtr val;
    
  // Try and look up subsref, _unless_ we are already in a method 
  // of this class
  if (!eval->inMethodCall(r.className()))
    if (ClassResolveFunction(eval,r,"subsref",val)) {
      // Overloaded subsref case
      return ClassSubsrefCall(eval,t,r,val);
    }
  ArrayVector rv;
  for (int index=1;index < t.numChildren();index++) {
    if (!rv.empty()) 
      throw Exception("Cannot reindex an expression that returns multiple values.");
    if (t.child(index).is(TOK_PARENS)) {
      ArrayVector m;
      const Tree & s(t.child(index));
      for (int p=0;p<s.numChildren();p++)
	eval->multiexpr(s.child(p),m);
      eval->subsindex(m);
      //     m = eval->varExpressionList(t->child(index).children(),r);
      if (m.size() == 0) 
	throw Exception("Expected indexing expression!");
      else if (m.size() == 1) {
	q = r.get(m.front());
	r = q;
      } else {
	q = r.get(m);
	r = q;
      }
    }
    if (t.child(index).is(TOK_BRACES)) {
      ArrayVector m;
      const Tree & s(t.child(index));
      for (int p=0;p<s.numChildren();p++)
	eval->multiexpr(s.child(p),m);
      eval->subsindex(m);
      //     m = eval->varExpressionList(t->child(index).children(),r);
      if (m.size() == 0) 
	throw Exception("Expected indexing expression!");
      else if (m.size() == 1)
	rv = ArrayVectorFromCellArray(r.get(m.front()));
      else
	rv = ArrayVectorFromCellArray(r.get(m));
      if (rv.size() == 1) {
	r = rv[0];
	rv = ArrayVector();
      } else if (rv.size() == 0) {
	throw Exception("Empty expression!");
	r = EmptyConstructor();
      }
    }
    if (t.child(index).is('.')) {
      // This is where the classname chain comes into being.
      StringVector classPath(r.constStructPtr().classPath());
      for (int i=1;i<classPath.size();i++) {
	rv = r.get(classPath.at(i));
	r = rv[0];
      }
      rv = r.get(t.child(index).first().text());
      if (rv.size() <= 1) {
	r = rv[0];
	rv = ArrayVector();
      }
    }
    if (t.child(index).is(TOK_DYN)) {
      QString field;
      try {
	Array fname(eval->expression(t.child(index).first()));
	field = fname.asString();
      } catch (Exception &e) {
	throw Exception("dynamic field reference to structure requires a string argument");
      }
      rv = r.get(field);
      if (rv.size() <= 1) {
	r = rv[0];
	rv = ArrayVector();
      }      
    }
  }
  if (rv.empty())
    rv.push_back(r);
  return rv;
}

void ClassAssignExpression(ArrayReference dst, const Tree & t, const Array& value, Interpreter* eval) {
  FuncPtr val;
  if (!ClassResolveFunction(eval,*dst,"subsasgn",val))
    throw Exception(QString("The method 'subsasgn' is not defined for objects of class ") + 
		    dst->className());
  ArrayVector p;
  p.push_back(*dst);
  p.push_back(IndexExpressionToStruct(eval,t, *dst));
  p.push_back(value);
  val->updateCode(eval);
  bool overload(eval->getStopOverload());
  eval->setStopOverload(true);
  ArrayVector n = eval->doFunction(val,p,1);
  eval->setStopOverload(overload);
  if (!n.empty())
    *dst = n[0];
  else
    eval->warningMessage(QString("'subsasgn' for class ") + dst->className() + 
			 QString(" did not return a value... operation has no effect."));
}

// Ideally, this would be the only place where the class name is mangled.
// However, currently, the same op is repeated in the Interface implementation
// code.
QString ClassMangleName(QString className, QString funcName) {
  return "@" + className + ":" + funcName;
}

void clearUserClasses() {
  classTable.clear();
}