File: tree_model.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (631 lines) | stat: -rw-r--r-- 15,964 bytes parent folder | download
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
/* 
 * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
 *
 * 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; version 2 of the
 * License.
 * 
 * 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., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "tree_model.h"
#include <grtpp_util.h>
#include <boost/lexical_cast.hpp>
#include <iomanip>

using namespace grt;
using namespace bec;

Pool<std::vector<size_t> > *NodeId::_pool = 0;

// NodeId will get index from the pool
NodeId::NodeId()
  : index(0)
{
  index = pool()->get();
}

//--------------------------------------------------------------------------------------------------

NodeId::NodeId(const NodeId &copy)
  : index(0)
{
  index = pool()->get();
  if (copy.index != NULL)
    *index = *copy.index;
}

//--------------------------------------------------------------------------------------------------

NodeId::NodeId(size_t i)
  : index(0)
{
  index = pool()->get();
  index->push_back(i);
}

//--------------------------------------------------------------------------------------------------

NodeId::NodeId(const std::string &str)
  : index(0)
{
  index = pool()->get();
  try
  {
    const char* chr = str.c_str();
    size_t size = str.length();
    std::string num;
    num.reserve(size);

    for (size_t i = 0; i < size; i++)
    {
      if (isdigit(chr[i]))
        num.push_back(chr[i]);
      else if ('.' == chr[i] || ':' == chr[i])
      {
        if (!num.empty())
        {
          index->push_back(base::atoi<int>(num, 0));
          num.clear();
        }
      }
      else
        throw std::runtime_error("Wrong format of NodeId");
    }

    if (!num.empty())
      index->push_back(base::atoi<int>(num, 0));
  }
  catch (...)
  {
    index->clear();
    pool()->put(index);
    index = 0;
    throw;
  }
}

//--------------------------------------------------------------------------------------------------

NodeId::~NodeId()
{
  index->clear();
  pool()->put(index);
  index = 0;
}

//--------------------------------------------------------------------------------------------------

bool NodeId::operator < (const NodeId &r) const
{
  bool ret = true;

  if (index && r.index)
  {
    // Shorter node ids must go before longer. For example in a list ["0.1", "0.1.1"]
    // longer nodeid is a subnode of the "0.1", so in case of deletion subnode deleted first
    // (That's true only when traversing list from the end)
    if (index->size() < r.index->size())
      ret = true;
    else if (index->size() > r.index->size())
      ret = false;
    else
    {
      // It is assumed that this node id is less than @r. Walk index vectors. If current value
      // from this->index is less than or equal to the corresponding value from r.index the pair is skipped
      // as it complies with assumption that this node is less than @r.
      // Once current value becomes greater than @r's the assumption about current node's 
      // less than @r becomes false, therefore this node is greater than @r.
      for (size_t i = 0; i < index->size(); ++i)
      {
        if ((*index)[i] >= (*r.index)[i])
        {
          ret = false;
          break;
        }
      }
    }
  }

  return ret;
}

//--------------------------------------------------------------------------------------------------

bool NodeId::equals(const NodeId &node) const
{
  // TODO: Check if we need to compare content of the index and node.index vectors
  return index && node.index && *node.index == *index;
}

//--------------------------------------------------------------------------------------------------

size_t& NodeId::operator[] (size_t i) const
{
  if (i < index->size())
    return const_cast<size_t&>((*index)[i]);
  throw std::range_error("invalid index");
}

//--------------------------------------------------------------------------------------------------

size_t NodeId::end() const
{
  if (index->size() > 0)
    return (*index)[index->size() - 1];
  throw std::logic_error("invalid node id. NodeId::end applied to an empty NodeId instance.");
}

//--------------------------------------------------------------------------------------------------

/**
 * Sets leaf to the previous index, e.g. for node with path "1.3.2" it will become "1.3.1".
 */
bool NodeId::previous() const
{
  bool ret = false;
  if (index->size() > 0)
  {
    --((*index)[index->size() - 1]);
    ret = true;
  }
  return ret;
}

//--------------------------------------------------------------------------------------------------

/**
 *	Sets leaf to the next index, e.g. for node with path "1.3.2" it will become "1.3.3".
 */
bool NodeId::next() const
{
  bool ret = false;
  if (index->size() > 0)
  {
    ++((*index)[index->size() - 1]);
    ret = true;
  }
  return ret;
}

//--------------------------------------------------------------------------------------------------

NodeId NodeId::parent() const
{
  if (depth() < 2)
    return NodeId();
  NodeId copy(*this);
  copy.index->pop_back();
  return copy;
}

//--------------------------------------------------------------------------------------------------

std::string NodeId::description() const
{
  return toString();
}

//--------------------------------------------------------------------------------------------------

std::string NodeId::toString(const char separator) const
{
  std::stringstream out;
  for (size_t i = 0; i < index->size(); i++)
  {
    if (i > 0)
      out << separator;
    out << (*index)[i];
  }
  return out.str();
}

//--------------------------------------------------------------------------------------------------

NodeId& NodeId::append(size_t i)
{
  // TODO: does it really make sense to cast to a signed type if the parameter can only be unsigned?
  //       It would require that the caller takes a signed value and casts it to an unsigned one.
  //       Very unlikely to happen.
  //       If these checks are removed we can make append and prepend inline again.
  if ((ssize_t)i < 0)
    throw std::invalid_argument("negative node index is invalid");
  index->push_back(i);
  return *this;
}

//--------------------------------------------------------------------------------------------------

NodeId& NodeId::prepend(size_t i)
{
  if ((ssize_t)i < 0)
    throw std::invalid_argument("negative node index is invalid");
  index->insert(index->begin(), i);
  return *this;
}

//----------------- ListModel ----------------------------------------------------------------------

Type ListModel::get_field_type(const NodeId &node, ColumnId column)
{
  throw std::logic_error("not implemented");
}

//--------------------------------------------------------------------------------------------------

bool ListModel::get_field_grt(const NodeId &node, ColumnId column, grt::ValueRef &value)
{
  return false;
}

//--------------------------------------------------------------------------------------------------

bool ListModel::get_field(const NodeId &node, ColumnId column, std::string &value)
{
  ValueRef v;

  if (!get_field_grt(node, column, v))
    return false;

  value = v.toString();

  return true;
}

//--------------------------------------------------------------------------------------------------

bool ListModel::get_field(const NodeId &node, ColumnId column, ssize_t &value)
{
  ValueRef v(0);

  if (!get_field_grt(node, column, v))
    return false;

  switch (v.type())
  {
  case IntegerType:
    value = IntegerRef::cast_from(v);
    return true;

  default:
    return false;
  }
}

//--------------------------------------------------------------------------------------------------

bool ListModel::get_field(const NodeId &node, ColumnId column, bool &value)
{
  ssize_t i;
  if (!get_field(node, column, i))
    return false;
  
  value = (i != 0);
  return true;
}

//--------------------------------------------------------------------------------------------------

bool ListModel::get_field(const NodeId &node, ColumnId column, double &value)
{
  ValueRef v(0);

  if (!get_field_grt(node, column, v))
    return false;

  switch (v.type())
  {
  case DoubleType:
    value= DoubleRef::cast_from(v);
    return true;

  case IntegerType:
    value= (double)(int)IntegerRef::cast_from(v);
    return true;

  default:
    return false;
  }
}

//--------------------------------------------------------------------------------------------------

ValueRef ListModel::get_grt_value(const NodeId &node, ColumnId column)
{
  ValueRef value;

  get_field_grt(node, column, value);

  return value;
}

//--------------------------------------------------------------------------------------------------

std::string ListModel::get_field_description(const NodeId &node, ColumnId column)
{
  return "";
}

//--------------------------------------------------------------------------------------------------

IconId ListModel::get_field_icon(const NodeId &node, ColumnId column, IconSize size)
{
  return 0;
}

//--------------------------------------------------------------------------------------------------

bool ListModel::set_field(const NodeId &node, ColumnId column, const std::string &value)
{ 
  return false;
}

//--------------------------------------------------------------------------------------------------

bool ListModel::set_field(const NodeId &node, ColumnId column, ssize_t value)
{
  return false; 
}

//--------------------------------------------------------------------------------------------------

bool ListModel::set_field(const NodeId &node, ColumnId column, double value)
{
  return false; 
}

//--------------------------------------------------------------------------------------------------

bool ListModel::set_convert_field(const NodeId &node, ColumnId column, const std::string &value)
{
  switch (get_field_type(node, column))
  {
  case IntegerType:
  try
  {
    set_field(node, column, boost::lexical_cast<ssize_t>(value));
  } catch(...)
  {
    return false;
  }

  case DoubleType:
  try
  {
    set_field(node, column, boost::lexical_cast<double>(value));
  } catch(...)
  {
    return false;
  }

  case StringType:
    return set_field(node, column, value);

  default:
    return false;
  }
  
  return true;
}

//--------------------------------------------------------------------------------------------------

NodeId ListModel::get_node(size_t index)
{
  return index;
}

//--------------------------------------------------------------------------------------------------

bool ListModel::has_next(const NodeId &node)
{
  return node[0] + 1 < count();
}

//--------------------------------------------------------------------------------------------------

NodeId ListModel::get_next(const NodeId &node)
{
  if (node[0] + 1 < count())
    return node[0] + 1;
  throw std::out_of_range("invalid child");
}

//--------------------------------------------------------------------------------------------------

ValueRef ListModel::parse_value(Type type, const std::string &value)
{
  switch (type)
  {
  case IntegerType:
    try
    {
      return IntegerRef(boost::lexical_cast<ssize_t>(value));
    } catch(...)
    {
      break;
    }

  case DoubleType:
    try
    {
      return DoubleRef(boost::lexical_cast<double>(value));
    } catch(...)
    {
      break;
    }

  case AnyType:
  case StringType:
    return StringRef(value);

  default:
    break;
  }
  return ValueRef();
}

//--------------------------------------------------------------------------------------------------

/**
 * Move the given node one index down in this list.
 */
void ListModel::reorder_up(const NodeId &node)
{
  if (node.end() > 0)
    reorder(node, node.end() - 1);
}

//--------------------------------------------------------------------------------------------------

/**
 * Move the given node one index up in this list.
 */
void ListModel::reorder_down(const NodeId &node)
{
  reorder(node, node.end() + 1);
}

//--------------------------------------------------------------------------------------------------

void ListModel::dump(int show_field)
{
  g_print("\nDumping list model:\n");
  for (size_t i = 0, c= count(); i < c; i++)
  {
    NodeId child(i);
    std::string value;
    
    if (!get_field(child, show_field, value))
      value= "???";
    
    g_print("- %s\n", value.c_str());
  }  
  g_print("\nFinished dumping list model.");
}

//----------------- TreeModel ----------------------------------------------------------------------

size_t TreeModel::count()
{
  return count_children(get_root());
}

//--------------------------------------------------------------------------------------------------

NodeId TreeModel::get_node(size_t index)
{
  return get_child(get_root(), index);
}

//--------------------------------------------------------------------------------------------------

bool TreeModel::has_next(const NodeId &node)
{
  NodeId parent(get_parent(node));
    
  return node.end() < count_children(parent)-1;
}

//--------------------------------------------------------------------------------------------------

NodeId TreeModel::get_next(const NodeId &node)
{
  if (node.depth() < 2)
    return ListModel::get_next(node);
  else
  {
    NodeId parent(get_parent(node));
    
    if (node.end() < count_children(parent)-1)
      return parent.append(node.end()+1);

    throw std::out_of_range("last node");
  }
}

//--------------------------------------------------------------------------------------------------

size_t TreeModel::get_node_depth(const NodeId &node)
{
  return node.depth();
}

//--------------------------------------------------------------------------------------------------

NodeId TreeModel::get_root() const
{
  return NodeId();
}

//--------------------------------------------------------------------------------------------------

bool TreeModel::expand_node(const NodeId &node)
{
  return false;
}

//--------------------------------------------------------------------------------------------------

void TreeModel::collapse_node(const NodeId &node)
{
}

//--------------------------------------------------------------------------------------------------

bool TreeModel::is_expanded(const NodeId &node)
{
  return false;
}

//--------------------------------------------------------------------------------------------------

bool TreeModel::is_expandable(const NodeId &node_id)
{
  return count_children(node_id) > 0;
}

//--------------------------------------------------------------------------------------------------

static void dump_node(TreeModel *model, int show_field, const NodeId &node_id)
{
  for (size_t i = 0, c = model->count_children(node_id); i < c; i++)
  {
    NodeId child= model->get_child(node_id, i);
    std::string value;
    char* left= (char*) "-"; // Cast needed to avoid deprecation warning with GCC 4.3

    if (!model->get_field(child, show_field, value))
      value= "???";

    if (model->is_expandable(node_id))
      left= (char*) "+";

    std::stringstream ss;
    ss << std::setw(child.depth());
    ss << left;
    g_print("%s %s\n", ss.str().c_str(), value.c_str());

    dump_node(model, show_field, child);
  }
}

//--------------------------------------------------------------------------------------------------

void TreeModel::dump(int show_field)
{
  g_print("\nDumping tree model:\n");
  dump_node(this, show_field, NodeId());
  g_print("\nFinished dumping tree model.");
}

//--------------------------------------------------------------------------------------------------