File: coretable.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (525 lines) | stat: -rw-r--r-- 11,059 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
/*
   FALCON - The Falcon Programming Language.
   FILE: coretable.cpp

   Table support Interface for Falcon.
   -------------------------------------------------------------------
   Author: Giancarlo Niccolai
   Begin: Sat, 20 Sep 2008 15:15:56 +0200

   -------------------------------------------------------------------
   (C) Copyright 2008: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/coretable.h>
#include <falcon/iterator.h>
#include <falcon/traits.h>
#include <falcon/itemtraits.h>
#include <falcon/vm.h>

namespace Falcon {

//===============================================================


CoreTable::CoreTable():
   m_currentPage(0),
   m_pages(&traits::t_voidp()),
   m_headerData( &traits::t_item() ),
   m_heading( &traits::t_string(), &traits::t_int() ),
   m_currentPageId(noitem),
   m_order(noitem),
   m_biddingVals(0),
   m_biddingSize(0)
{
}

CoreTable::CoreTable( const CoreTable& other ):
   m_currentPage( other.m_currentPage ),
   m_pages(other.m_pages),
   m_headerData( other.m_headerData ),
   m_heading( other.m_heading ),
   m_currentPageId( other.m_currentPageId ),
   m_order( other.m_order ),
   m_biddingVals(0),
   m_biddingSize(0)
{
}

CoreTable::~CoreTable()
{
   if ( m_biddingVals != 0 ) {
      memFree( m_biddingVals );
      m_biddingVals = 0;
   }
}

bool CoreTable::setHeader( CoreArray *header )
{
   return setHeader( header->items() );
}


bool CoreTable::setHeader( const ItemArray& header )
{
   if ( m_order != noitem )
   {
      if( m_order != header.length() )
         return false;
   }

   uint32 len = header.length();
   m_headerData.reserve( len );
   m_headerData.resize(0);
   m_heading.clear();

   for( uint32 i = 0; i < len; i++ )
   {
      const Item &itm = header[i];

      // we accept only strings and future bindings
      if ( itm.isFutureBind() )
      {
         // string + value
         m_heading.insert( itm.asLBind(), &i );
         m_headerData.push( &itm.asFBind()->origin() );
      }
      else if ( itm.isString() ) {
         Item nil;
         m_heading.insert( itm.asString(), &i );
         m_headerData.push( &nil );
      }
      else
         return false;
   }

   m_order = len;
   return true;
}

bool CoreTable::insertRow(  CoreArray *ca, uint32 pos, uint32 page )
{
   if ( m_order == 0 || m_order != ca->length() )
      return false;

   CoreArray *tgt;
   if ( page == noitem )
   {
      tgt = currentPage();
      if ( tgt == 0 )
         return false;
   }
   else {
      if( m_pages.size() <= page )
         return false;

      tgt = *(CoreArray **) m_pages.at(page);
   }

   if ( pos < ca->length() )
      tgt->insert( ca, pos );
   else
      tgt->append( ca );

   return true;
}


bool CoreTable::removeRow( uint32 pos, uint32 page )
{
   CoreArray *tgt;
   if ( page == noitem )
   {
      tgt = currentPage();
      if ( tgt == 0 )
         return false;
   }
   else {
      if( m_pages.size() <= page )
         return false;

      tgt = *(CoreArray **) m_pages.at(page);
   }

   if ( pos >= tgt->length() )
      return false;
   tgt->remove( pos );
   return true;
}


const String &CoreTable::heading( uint32 pos ) const
{
   fassert( pos < order() );

   MapIterator mi = m_heading.begin();
   while( mi.hasCurrent() )
   {
      uint32* p = (uint32*) mi.currentValue();
      if ( *p == pos )
         return *(String *) mi.currentKey();
      mi.next();
   }

   fassert( false );

   // have a nice crash...
   return *(String *) 0;
}


uint32 CoreTable::getHeaderPos( const String &name ) const
{
   uint32* pos = (uint32*) m_heading.find( &name );
   if ( pos == 0 )
      return noitem;
   return *pos;
}


Item *CoreTable::getHeaderData( uint32 pos ) const
{
   if ( pos >= m_headerData.size() )
      return 0;
   return (Item *) m_headerData.at(pos);
}

bool CoreTable::insertPage( CoreObject *self, CoreArray *data, uint32 pos )
{
   uint32 i;

   // may be long zero; it's ok
   for( i = 0; i < data->length(); i ++ )
   {
      if ( ! (*data)[i].isArray() || (*data)[i].asArray()->length() != m_order )
         return false;
   }

   for( i = 0; i < data->length(); i ++ )
   {
      (*data)[i].asArray()->table( self );
   }

   // ok we have a good page to add.
   if( pos < m_pages.size() )
      m_pages.insert( data, pos );
   else
      m_pages.push( data );

   if ( m_currentPageId >= pos )
      m_currentPageId++;

   return true;
}

bool CoreTable::removePage( uint32 pos )
{
   if ( m_pages.size() == 1 || pos >= m_pages.size() )
   {
      // can't delete the only page left.
      return false;
   }

   // declare the page dead
   page(pos)->gcMark(1);

   // are we going to remove the current page?
   if ( m_currentPageId == pos )
   {
      m_pages.remove(pos);
      m_currentPage = *(CoreArray **) m_pages.at(0);
      m_currentPageId = 0;
   }
   else {
      m_pages.remove(pos);
   }

   // If it was equal, it became 0
   if( m_currentPageId > pos )
      m_currentPageId--;

   return true;
}


const Item &CoreTable::front() const
{
   static Item fake;
   fassert( m_currentPage != 0 );
   fassert( m_currentPage->length() != 0 );

   fake = (*m_currentPage)[0];
   return fake;
}


void CoreTable::append( const Item &data )
{
   if ( ! data.isArray() )
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
         .origin( e_orig_runtime )
         .extra( "A" ) );

   insertRow( data.asArray() );
}

/** Prepend an item at the beginning of the sequence. */
void CoreTable::prepend( const Item &data )
{
      if ( ! data.isArray() )
      throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
         .origin( e_orig_runtime )
         .extra( "A" ) );

   insertRow( data.asArray(), 0 );
}

const Item &CoreTable::back() const
{
   static Item fake;
   fassert( m_currentPage != 0 );
   fassert( m_currentPage->length() != 0 );

   fake = (*m_currentPage)[m_currentPage->length()-1];
   return fake;
}



CoreTable *CoreTable::clone() const
{
   return new CoreTable( *this );
}


void CoreTable::clear()
{
   if ( m_currentPage != 0 )
      m_currentPage->resize(0);
}


void CoreTable::gcMark( uint32 gen )
{
   uint32 i;

   // mark the header data...
   for ( i = 0; i < m_headerData.size(); i ++ )
   {
      memPool->markItem( *((Item *) m_headerData.at(i)) );
   }

   // and all the tables.
   for( i = 0; i < m_pages.size(); i++ )
   {
      CoreArray* page = *(CoreArray**)m_pages.at(i);
      page->gcMark( gen );
   }
}

void CoreTable::reserveBiddings( uint32 size )
{
   if ( size > m_biddingSize )
   {
      if ( m_biddingVals != 0 )
         memFree( m_biddingVals );
      m_biddingSize = size;
      m_biddingVals = (numeric *) memAlloc( size * sizeof( numeric ) );
   }
}

void CoreTable::renameColumn( uint32 pos, const String &name )
{
   fassert( pos < order() );

   MapIterator mi = m_heading.begin();
   while( mi.hasCurrent() )
   {
      uint32* p = (uint32*) mi.currentValue();
      if ( *p == pos ) {
         m_heading.erase( mi );
         m_heading.insert( &name, &pos );
         return;
      }
      mi.next();
   }

   fassert( false );

}

void CoreTable::insertColumn( uint32 pos, const String &name, const Item &data, const Item &dflt )
{
   // if pos >= order, we're lucky. Append.
   if ( pos >= m_order )
   {
      pos = m_order;
      m_heading.insert( &name, &pos );
   }
   else
   {
      // first, move all following elements 1 forward
      MapIterator mi = m_heading.begin();
      while( mi.hasCurrent() )
      {
         uint32* p = (uint32*) mi.currentValue();
         if ( *p >= pos ) {
            *p = *p+1;
         }
         mi.next();
      }
      // then insert the new entry
      m_heading.insert( &name, &pos );
   }

   m_order++;
   // add the column data.
   m_headerData.insert( (void *) &data, pos );

   // now, for each page, for each row, insert the default item.
   for( uint32 pid = 0; pid < m_pages.size(); pid++ )
   {
      CoreArray *pg = page( pid );
      for( uint32 rowid = 0; rowid < pg->length(); rowid++ )
      {
         CoreArray *row = pg->at(rowid).asArray();
         // modify is forbidden if the array has a table.
         CoreObject *save = row->table();
         row->table(0);
         row->insert( dflt, pos );
         row->table(save);
      }
   }
}

bool CoreTable::removeColumn( uint32 pos )
{
   // if pos >= order, we're lucky. Append.
   if ( pos >= m_order )
   {
     return false;
   }

   // first, move all following elements 1 forward
   MapIterator mi = m_heading.begin();
   while( mi.hasCurrent() )
   {
      uint32* p = (uint32*) mi.currentValue();

      // when we find the foobar'd column, remove it.
      if ( *p == pos )
      {
         m_heading.erase( mi );
         continue;
      }

      // else, take other columns back
      if ( *p > pos ) {
         *p = *p-1;
      }
      mi.next();
   }

   // add the column data.
   m_headerData.remove( pos );
   m_order--;

   // now, for each page, for each row, insert the default item.
   for( uint32 pid = 0; pid < m_pages.size(); pid++ )
   {
      CoreArray *pg = page( pid );
      for( uint32 rowid = 0; rowid < pg->length(); rowid++ )
      {
         CoreArray *row = pg->at(rowid).asArray();
         // modify is forbidden if the array has a table.
         CoreObject *save = row->table();
         row->table(0);
         row->remove( pos );
         row->table(save);
      }
   }

   return true;
}


//============================================
// Iterator implementation
//============================================

void CoreTable::getIterator( Iterator& tgt, bool tail ) const
{
   // give up the ownership of the iterator to the current page.
   tgt.sequence( &m_currentPage->items() );
   m_currentPage->items().getIterator( tgt, tail );
}

void CoreTable::copyIterator( Iterator& tgt, const Iterator& source ) const
{
   // actually never called
}

void CoreTable::insert( Iterator &iter, const Item &data )
{
   // actually never called
}

void CoreTable::erase( Iterator &iter )
{
   // actually never called
}

bool CoreTable::hasNext( const Iterator &iter ) const
{
   // actually never called
   return false;
}

bool CoreTable::hasPrev( const Iterator &iter ) const
{
   // actually never called
   return false;
}

bool CoreTable::hasCurrent( const Iterator &iter ) const
{
   // actually never called
   return false;
}

bool CoreTable::next( Iterator &iter ) const
{
   // actually never called
   return false;
}

bool CoreTable::prev( Iterator &iter ) const
{
   // actually never called
   return false;
}

Item& CoreTable::getCurrent( const Iterator &iter )
{
   // actually never called
   throw new CodeError( ErrorParam( e_invalid_iter, __LINE__ ) );
}

Item& CoreTable::getCurrentKey( const Iterator &iter )
{
   // actually never called
   throw new CodeError( ErrorParam( e_invalid_iter, __LINE__ ) );
}

bool CoreTable::equalIterator( const Iterator &first, const Iterator &second ) const
{
   // actually never called
   return false;
}


}

/* end of coretable.cpp */