2 * Programming Language SOOPY
3 * (Simple Object Oriented Programming sYstem)
5 * Copyright (C) 2002 SUZUKI Jun
7 * URL: http://sourceforge.jp/projects/soopy/
8 * License: GPL(GNU General Public License)
11 * $Id: Array.cpp,v 1.13 2004/03/24 11:53:44 randy Exp $
22 SpBaseArray::SpBaseArray(uintvec& v)
25 depth = dimension.size();
29 SpValue& SpBaseArray::getDepth(SpValue& self)
31 SpArray* array = self.asArray();
32 // static SpValue result;
33 // result.setInt(array->depth);
35 return SpIntResult(array->depth);
38 SpValue& SpBaseArray::getDimension(SpValue& self)
41 SpArray* array = self.asArray();
43 it = array->dimension.begin();
45 SpCons* head = new SpCons(temp);
48 for(; it != array->dimension.end(); it++){
53 ptr = (SpCons*)v.asList();
55 // static SpValue result;
56 // result.setNewObject(head);
58 return SpObjectResult(head);
61 // init Message Handler
62 void SpBaseArray::init()
64 SpValue SymDepth(new SpSymbol("depth"));
65 SpValue PrimDepth(new SpPrim1(getDepth));
66 ArrayMsgHandler.append(SymDepth, PrimDepth);
68 SpValue SymDimension(new SpSymbol("dimension"));
69 SpValue PrimDimension(new SpPrim1(getDimension));
70 ArrayMsgHandler.append(SymDimension, PrimDimension);
78 SpObjectArray::SpObjectArray(SpValue& t, uintvec& v)
81 // depth = dimension.size();
84 it = dimension.begin();
85 for(; it != dimension.end(); it++){
88 pvec = new SpValueVector(allsize);
91 SpValue& SpObjectArray::toString()
103 uintvec::iterator it;
104 it = dimension.begin();
106 *str += itoa(*it, buf, 10);
108 sprintf(buf, "%d", *it);
112 for(; it != dimension.end(); it++){
115 *str += itoa(*it, buf, 10);
117 sprintf(buf, "%d", *it);
124 // s.setNewObject(str);
126 return SpObjectResult(str);
129 SpValue& SpObjectArray::operator[](SpValue& tuple)
132 SpTuple* t = dynamic_cast<SpTuple*>(tuple.getObject());
133 if((SpInt)depth != t->length()){
134 throw SpException("array depth mismatch");
136 uintvec::iterator uit;
137 SpValueVector::iterator tit;
138 uit = dimension.begin();
140 unsigned int offset = 0;
141 unsigned int times = 1;
142 for(; uit != dimension.end(); uit++, tit++){
143 SpValue v = tit->eval();
145 throw SpException("not int (access array)");
147 unsigned int index = v.getInt();
148 if((*uit - 1) < index){
149 throw SpException("too large index (access array)");
151 offset += times * index;
154 return (*pvec)[offset];
157 SpValue v = tuple.eval();
159 throw SpException("not int (access array)");
162 throw SpException("depth not match (access array)");
164 unsigned int index = v.getInt();
165 if((dimension[0] - 1) < index){
166 throw SpException("over range (access array)");
168 return (*pvec)[index];
172 void SpObjectArray::set(SpValue& tuple, SpValue& value)
174 (*this)[tuple] = value;
177 SpValue& SpObjectArray::onMessage(SpValue& rec, SpValue& msg)
179 if(ArrayMsgHandler.hasMessage(msg)){
180 return ArrayMsgHandler(rec, msg);
182 SpValue val = msg.eval();
184 return operator[](val);
187 return operator[](val);
191 static char buf[256];
192 strncpy(buf, rec.toCStringWithEncoder(), 255);
194 static char buf2[256];
195 strncpy(buf2, msg.toCStringWithEncoder(), 255);
197 throw SpNoMethodException("no such a feature", buf, buf2);
205 SpByteArray::SpByteArray(uintvec& v)
209 uintvec::iterator it;
210 it = dimension.begin();
211 for(; it != dimension.end(); it++){
214 // buf = new char(allsize);
215 buf = (unsigned char*)malloc(allsize);
217 throw SpException("out of memory");
221 SpValue& SpByteArray::toString()
226 str = new SpString();
227 *str = "Array:byte (";
229 uintvec::iterator it;
230 it = dimension.begin();
232 *str += itoa(*it, buf, 10);
234 sprintf(buf, "%d", *it);
238 for(; it != dimension.end(); it++){
241 *str += itoa(*it, buf, 10);
243 sprintf(buf, "%d", *it);
250 // s.setNewObject(str);
252 return SpObjectResult(str);
255 unsigned char& SpByteArray::operator[](SpValue& tuple)
258 SpTuple* t = dynamic_cast<SpTuple*>(tuple.getObject());
259 if((SpInt)depth != t->length()){
260 throw SpException("array depth mismatch");
262 uintvec::iterator uit;
263 SpValueVector::iterator tit;
264 uit = dimension.begin();
266 unsigned int offset = 0;
267 unsigned int times = 1;
268 for(; uit != dimension.end(); uit++, tit++){
269 SpValue v = tit->eval();
271 throw SpException("not int (access array)");
273 unsigned int index = v.getInt();
274 if((*uit - 1) < index){
275 throw SpException("too large index (access array)");
277 offset += times * index;
283 SpValue v = tuple.eval();
285 throw SpException("not int (access array)");
288 throw SpException("depth not match (access array)");
290 unsigned int index = v.getInt();
291 if((dimension[0] - 1) < index){
292 throw SpException("over range (access array)");
298 SpValue& SpByteArray::getFeatureType()
303 SpValue& SpByteArray::onMessage(SpValue& rec, SpValue& msg)
305 // static SpValue result;
307 if(ArrayMsgHandler.hasMessage(msg)){
308 return ArrayMsgHandler(rec, msg);
310 SpValue val = msg.eval();
312 // result.setInt(operator[](val));
314 return SpIntResult(operator[](val));
317 // result.setInt(operator[](val));
319 return SpIntResult(operator[](val));
323 static char buf[256];
324 strncpy(buf, rec.toCStringWithEncoder(), 255);
326 static char buf2[256];
327 strncpy(buf2, msg.toCStringWithEncoder(), 255);
329 throw SpNoMethodException("no such a feature", buf, buf2);
337 SpValue& MakeArray::eval()
339 uintvec intVec; // int vector
340 SpTuple* tuple = vec.asTuple();
341 SpValueVector::iterator it;
343 for(; it != tuple->end(); it++){
344 SpValue v = it->eval();;
345 if(!v.isInt()){ throw SpException("not int (make array)"); }
346 intVec.push_back(v.getInt());
349 // static SpValue result;
351 // result.setNewObject(new SpByteArray(intVec));
352 return SpObjectResult(new SpByteArray(intVec));
354 // result.setNewObject(new SpObjectArray(typ, intVec));
355 return SpObjectResult(new SpObjectArray(typ, intVec));
360 SpValue& MakeArray::toString()
365 str = new SpString();
372 SpTuple* tuple = vec.asTuple();
373 SpValueVector::iterator it;
377 for(; it != tuple->end(); it++){
384 // s.setNewObject(str);
386 return SpObjectResult(str);