-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablas.H
1345 lines (1155 loc) · 31.3 KB
/
tablas.H
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
# include <autosprintf.h>
# include <fstream>
# include <parse-csv.H>
# include <tpl_dynSetHash.H>
# include <tpl_dynMapTree.H>
# include <tpl_olhash.H>
# include <tpl_dynArrayHeap.H>
# include <ahSort.H>
using namespace gnu;
extern bool verbose;
using Uid = unsigned long;
using fmt = gnu::autosprintf;
template <typename Key, class Cmp>
using HashType = DynSetHash<Key, Cmp>;
template <class T>
struct CmpLessId
{
bool operator () (const T & i1, const T & i2) const noexcept
{ return i1.id < i2.id; }
};
template <class T>
struct CmpEqualId
{
bool operator () (const T & i1, const T & i2) const noexcept
{ return i1.id == i2.id; }
};
template <class T>
size_t hash_id(const T & i) { return SuperFastHash(i.id); }
struct Socio
{
string rif;
string nombre;
char nacionalidad;
// lista de empresas en la cual this es accionista. Son pares rif-empresa y %
DynList<pair<string, float>> empresas;
Socio() {}
Socio(const string & r, const string & nom, char nac)
: rif(r), nombre(nom), nacionalidad(nac) {}
Socio(const string & r) : rif(r) {}
struct CmpLess
{
bool operator ()(const Socio & s1, const Socio & s2) const noexcept
{ return s1.rif < s2.rif; }
};
string to_string() const
{
string ret(rif + " " + nacionalidad + " " +
nombre + "\n" + " Accionistas: ");
empresas.for_each([&ret] (auto p)
{
ret += p.first + " " + ::to_string(p.second);
});
return ret;
}
friend ostream & operator << (ostream & out, const Socio & s)
{
return out << s.to_string() << endl;
}
void save(ostream & out) const
{
out << rif << endl
<< nombre << endl
<< nacionalidad << endl
<< empresas.size();
empresas.for_each([&out] (auto p)
{
out << " " << p.first << " " << p.second;
});
out << endl;
}
void load(istream & in)
{
in >> rif;
in.ignore();
getline(in, nombre);
in >> nacionalidad;
size_t n;
in >> n;
for (size_t i = 0; i < n; ++i)
{
pair<string, float> p;
in >> p.first >> p.second;
empresas.append(p);
}
}
};
struct TablaSocios : public DynSetTree<Socio, Avl_Tree, Socio::CmpLess>
{
TablaSocios(const char * name)
{
stringstream s;
ifstream in(name);
if (in.fail())
{
s << "No se pudo abrir " << name;
throw domain_error(s.str());
}
if (::verbose)
cout << "Loading socios ..." << endl;
size_t empty_counter = 0;
auto header = csv_read_row(in, ',');
const size_t n = header.size();
for (size_t i = 0; in.good(); ++i)
{
auto row = csv_read_row(in, ',');
if (row.size() != n)
{
s << "number of fields of row" << row.size() << " is not " << n;
throw domain_error(s.str());
}
char nac = stoi(row(5)) == 1 ? 'V' : 'E';
const string & unidad_economica_rif = row(2);
string & rif_pasaporte = row(3);
if (rif_pasaporte == "" or rif_pasaporte.size() == 0)
{
stringstream s;
s << "Sin-numero-" << empty_counter++;
rif_pasaporte = s.str();
}
auto s = search_or_insert(Socio(rif_pasaporte, row(4), nac));
s->empresas.append(make_pair(unidad_economica_rif, stof(row(7))));
}
if (::verbose)
cout << "done!" << endl
<< endl;
}
Socio * operator () (const string & rif) const
{
return search(Socio(rif));
}
void save(ostream & out) const
{
if (::verbose)
cout << "Saving socios ..." << endl;
out << size() << endl;
for_each([&out] (auto s) { s.save(out); });
if (::verbose)
cout << "done!" << endl
<< endl;
}
};
struct TablaMetaSocios : public DynArray<Socio>
{
TablaMetaSocios() {}
TablaMetaSocios(ifstream & in)
{
if (::verbose)
cout << "Loading socios ..." << endl;
size_t n;
in >> n;
reserve(n);
for (size_t i = 0; i < n; ++i)
access(i).load(in);
if (::verbose)
cout << "done!" << endl
<< endl;
}
void save(ostream & out) const
{
if (::verbose)
cout << "Saving socios ..." << endl;
out << size() << endl;
for_each([&out] (auto s) { s.save(out); });
if (::verbose)
cout << "done!" << endl
<< endl;
}
Socio * operator () (const string & rif) const noexcept
{
return bsearch(*this, Socio(rif), Socio::CmpLess());
}
};
struct UnidadEconomica
{
string rif = "No definido";
string nombre = "Sin Nombre";
UnidadEconomica(const string & r = "No definido",
const string & nom = "Sin Nombre") : rif(r), nombre(nom) {}
string to_string() const { return rif + " " + nombre; }
friend ostream & operator << (ostream & out, const UnidadEconomica & u)
{
return out << u.to_string();
}
struct CmpEqual
{
bool operator () (const UnidadEconomica & u1,
const UnidadEconomica & u2) const noexcept
{
return u1.rif == u2.rif;
}
};
};
struct TablaUnidadesEconomicas
: public HashType<UnidadEconomica, UnidadEconomica::CmpEqual>
{
using HashTable = HashType<UnidadEconomica, UnidadEconomica::CmpEqual>;
static size_t hash(const UnidadEconomica & u) noexcept
{
return SuperFastHash(u.rif);
}
void init_hash(const char * name)
{
stringstream s;
ifstream in(name);
if (in.fail())
{
s << "No se pudo abrir " << name;
throw std::domain_error(s.str());
}
if (::verbose)
cout << "Loading economical unities ..." << endl;
auto header = csv_read_row(in, ',');
const size_t n = header.size();
for (size_t i = 0; in.good(); ++i)
{
auto row = csv_read_row(in, ',');
if (row.size() != n)
{
s << "number of fields of row" << row.size() << " is not " << n;
throw domain_error(s.str());
}
emplace(row(0), row(2));
}
if (::verbose)
cout << "done!" << endl
<< endl;
}
TablaUnidadesEconomicas(const char * name)
: HashTable(8000, hash, UnidadEconomica::CmpEqual())
{
init_hash(name);
}
UnidadEconomica * operator () (const string rif) const
{
return search(UnidadEconomica(rif));
}
};
const string BLANK = " ";
struct Planta
{
Uid id;
string rif;
string nombre;
double cap;
Planta(Uid i = -1) noexcept : id(i) {}
Planta(Uid i, const string & r, const string & str, double c)
: id(i), rif(r), nombre(str), cap(c) {}
string to_string() const
{
return ::to_string(id) + " " + rif + " " + nombre + " " + ::to_string(cap);
}
friend ostream & operator << (ostream & out, const Planta & p)
{
return out << p.to_string();
}
void save(ostream & out) const
{
out << id << endl << nombre << endl << cap << endl;
}
void load(istream & in)
{
in >> id;
in.ignore();
getline(in, nombre);
in >> cap;
}
using CmpEqual = CmpEqualId<Planta>;
};
struct TablaPlantas : public HashType<Planta, Planta::CmpEqual>
{
using HashTable = HashType<Planta, Planta::CmpEqual>;
TablaPlantas() {}
TablaPlantas(const char * name)
: HashTable(2171, hash_id<Planta>, Planta::CmpEqual())
{
stringstream s;
ifstream in(name);
if (in.fail())
{
s << "No se pudo abrir " << name;
throw std::domain_error(s.str());
}
if (::verbose)
cout << "Loading subeconomical unities" << endl;
auto header = csv_read_row(in, ',');
const size_t n = header.size();
for (size_t i = 0; in.good(); ++i)
{
auto row = csv_read_row(in, ',');
if (row.size() != n)
{
s << "number of fields of row" << row.size() << " is not " << n;
throw domain_error(s.str());
}
emplace(atol(row(0).c_str()), row(1), row(2), stod(row(25)));
}
if (::verbose)
cout << "Done! " << endl
<< endl;
}
void save(ostream & out) const
{
if (::verbose)
cout << "Saving Plantas..." << endl;
out << size() << endl;
for_each([&out] (const auto & p) { p.save(out); });
if (::verbose)
cout << "done!" << endl;
}
TablaPlantas(istream & in)
: HashTable(2171, hash_id<Planta>, Planta::CmpEqual())
{
if (::verbose)
cout << "Loading Plantas ..." << endl;
size_t n;
in >> n;
for (auto i = 0; i < n; ++i)
{
Planta p;
p.load(in);
insert(p);
}
if (::verbose)
cout << "done!" << endl
<< endl;
}
Planta * operator () (Uid id) const
{
return search(Planta(id));
}
};
// insumo_id, cod_aran, cantidad, arco_id
using LinearFactor = tuple<Uid, string, double, Uid>;
struct Producto
{
Uid id;
string nombre;
Uid planta_id;
string cod_aran;
double cantidad_decl; // si producto == lo producido
double cantidad_real; // si producto == lo vendido
double coste;
// combinación lineal que define al producto. Cada par es un insumo_id
// y la cantidad requerida para fabricar una unidad del
// producto. Sirve como huella dactilar de un producto pero también
// para filtrar arcos en un grafo
DynList<LinearFactor> comb;
Producto(Uid i = -1) : id(i) {}
Producto(Uid i, const string & nom, const string & cod, Uid pla_id)
: id(i), nombre(nom), planta_id(pla_id), cod_aran(cod) {}
Producto(Uid pid, const string & cod) : planta_id(pid), cod_aran(cod) {}
string to_string() const
{
return ::to_string(id) + " " + nombre + " " + cod_aran + " " +
::to_string(planta_id);
}
friend ostream & operator << (ostream & out, const Producto & p)
{
return out << p.to_string();
}
struct CmpLessCodAran // Orden según planta_d,cod_aran
{
bool operator () (const Producto & p1, const Producto & p2) const noexcept
{
return p1.cod_aran < p2.cod_aran;
}
};
using CmpEqual = CmpEqualId<Producto>;
};
struct TablaP : public HashType<Producto, Producto::CmpEqual>
{
using HashTable = HashType<Producto, Producto::CmpEqual>;
TablaP(const char * name)
: HashTable(166611, hash_id<Producto>, Producto::CmpEqual())
{
stringstream s;
ifstream in(name);
if (in.fail())
{
s << "No se pudo abrir " << name;
throw std::domain_error(s.str());
}
if (::verbose)
cout << "Loading " << name << endl;
auto header = csv_read_row(in, ',');
const size_t n = header.size();
for (size_t i = 0; in.good(); ++i)
{
auto row = csv_read_row(in, ',');
if (row.size() != n)
{
s << "number of fields of row" << row.size() << " is not " << n;
throw domain_error(s.str());
}
emplace(atol(row(0).c_str()), row(1), row(3), atol(row(4).c_str()));
}
if (::verbose)
cout << "Done!" << endl
<< endl;
}
void cargar_costes(long anho, const char * costs_name)
{
stringstream s;
ifstream in(costs_name);
if (in.fail())
{
s << "No se pudo abrir " << costs_name;
throw std::domain_error(s.str());
}
if (::verbose)
cout << "Loading " << costs_name << endl;
auto header = csv_read_row(in, ',');
const size_t n = header.size();
for (size_t i = 0; in.good(); ++i)
{
auto row = csv_read_row(in, ',');
if (row.size() != n)
{
s << "number of fields of row" << row.size() << " is not " << n;
throw domain_error(s.str());
}
if (anho != atol(row(2).c_str()))
continue;
Producto * producto_ptr = search(atol(row(1).c_str()));
assert(producto_ptr != nullptr);
producto_ptr->cantidad_decl = atof(row(3).c_str());
producto_ptr->cantidad_real = atof(row(4).c_str());
producto_ptr->coste = atof(row(5).c_str());
emplace(atol(row(0).c_str()), row(1), row(3), atol(row(4).c_str()));
}
if (::verbose)
cout << "Done!" << endl
<< endl;
}
Producto * operator () (Uid id) const
{
auto p = search(Producto(id));
if (p == nullptr)
throw std::domain_error("Producto id %ld no encontrado");
return p;
}
};
struct TablaProductos : public TablaP
{
DynArray<Producto*> index;
Producto::CmpLessCodAran cmp;
TablaProductos(const char * prod_name)
: TablaP(prod_name), cmp(Producto::CmpLessCodAran())
{
if (::verbose)
cout << " Building index for " << prod_name << endl;
index.reserve(size());
size_t i = 0;
mutable_for_each([this, &i] (auto & p) { index(i++) = &p; });
in_place_sort(index, [] (auto p1, auto p2)
{ return p1->cod_aran < p2->cod_aran; });
if (::verbose)
cout << " done!" << endl
<< endl;
}
DynList<Producto*> por_cod_aran(const string & cod_aran) const
{
Producto tmp; tmp.cod_aran = cod_aran;
return bsearch_dup(index, tmp, cmp);
}
};
using Insumo = Producto;
using TablaInsumos = TablaP;
struct ProveedorInsumo
{
Uid id;
Uid proveedor_id;
Uid insumo_id;
double monto;
double cantidad;
ProveedorInsumo(Uid i = -1) noexcept : id(i) {}
ProveedorInsumo(Uid i, Uid pid, Uid iid, double m, double cant) noexcept
: id(i), proveedor_id(pid), insumo_id(iid), monto(m), cantidad(cant) {}
string to_string() const
{
return ::to_string(id) + " " + ::to_string(proveedor_id) + " " +
::to_string(insumo_id) + " " + ::to_string(monto) + " " +
::to_string(cantidad);
}
friend ostream & operator << (ostream & out, const ProveedorInsumo & p)
{
return out << p.to_string();
}
};
struct TablaProveedorInsumo : public DynArray<ProveedorInsumo>
{
struct CmpLess
{
bool operator () (const ProveedorInsumo & pi1,
const ProveedorInsumo & pi2) const noexcept
{
return pi1.insumo_id < pi2.insumo_id;
}
};
TablaProveedorInsumo(const char * name, long anho)
{
ifstream in(name);
stringstream s;
if (in.fail())
{
s << "No se pudo abrir " << name;
throw std::domain_error(s.str());
}
if (::verbose)
cout << "Loading " << name << " for " << anho << endl;
auto header = csv_read_row(in, ',');
const size_t n = header.size();
DynArrayHeap<ProveedorInsumo, CmpLess> heap;
for (size_t i = 0; in.good(); ++i)
{
auto row = csv_read_row(in, ',');
if (row.size() != n)
{
s << "number of fields of row" << row.size() << " is not " << n;
throw domain_error(s.str());
}
// long year = atol(row(4).c_str());
// if (year != anho)
// continue;
heap.emplace(atol(row(0).c_str()), atol(row[1].c_str()),
atol(row(2).c_str()), atof(row(3).c_str()),
atof(row(5).c_str()));
}
this->reserve(heap.size());
for (size_t i = 0; not heap.is_empty(); ++i)
this->access(i) = heap.getMin();
if (::verbose)
cout << "Done!" << endl
<< endl;
}
DynList<ProveedorInsumo*> operator() (Uid insumo_id) const
{
ProveedorInsumo p; p.insumo_id = insumo_id;
return bsearch_dup(*this, p, CmpLess());
}
};
struct Proveedor
{
Uid id;
string nombre;
string rif;
Uid pais_origen_id;
Uid pais_procedencia_id;
Uid planta_id;
bool nacional = true;
Proveedor(Uid i = -1) : id(i) {}
Proveedor(Uid i, const string & nom, const string & r,
Uid origen_id, Uid procedencia_id, const string & nac, Uid pid)
: id(i), nombre(nom), rif(r), pais_origen_id(origen_id),
pais_procedencia_id(procedencia_id), planta_id(pid), nacional(nac == "V")
{}
string to_string() const
{
const string type = nacional ? "VEN" : "EXT";
return type + " " + ::to_string(id) + " " + nombre + " " + rif +
" " + ::to_string(planta_id);
}
friend ostream & operator << (ostream & out, const Proveedor & p)
{
return out << p.to_string();
}
bool es_nacional() const noexcept { return nacional; }
bool es_extranjero() const noexcept { return not es_nacional(); }
};
struct TablaProveedores : public HashType<Proveedor, CmpEqualId<Proveedor>>
{
using HashTable = HashType<Proveedor, CmpEqualId<Proveedor>>;
TablaProveedores(const char * name)
: HashTable(45279, hash_id<Proveedor>, CmpEqualId<Proveedor>())
{
stringstream s;
ifstream in(name);
if (in.fail())
{
s << "No se pudo abrir " << name;
throw std::domain_error(s.str());
}
if (::verbose)
cout << "Loading " << name << endl;
auto header = csv_read_row(in, ',');
const size_t n = header.size();
for (size_t i = 0; in.good(); ++i)
{
auto row = csv_read_row(in, ',');
if (row.size() != n)
{
s << "number of fields of row" << row.size() << " is not " << n;
throw domain_error(s.str());
}
auto nac = row(5);
if (nac != "V" and nac != "E")
{
s << "Tabla prov: nacion inválida en fila " << i;
throw domain_error(s.str());
}
emplace(atol(row(0).c_str()), row(1), row(2), atol(row(3).c_str()),
atol(row(4).c_str()), nac, atol(row(6).c_str()));
}
if (::verbose)
cout << "Done! " << endl
<< endl;
}
Proveedor * operator () (Uid id) const
{
auto p = search(Proveedor(id));
if (p == nullptr)
{
stringstream s;
s << "Proveedor id " << id << " no encontrado";
throw std::domain_error(s.str());
}
return p;
}
};
inline string rif_ext(Uid origen_id, Uid procedencia_id, string nombre)
{
replace(nombre.begin(), nombre.end(), ' ', '_');
stringstream s;
s << origen_id << " " << procedencia_id;
return s.str() + " " + nombre;
}
inline pair<Uid, Uid> pais(const string & rif)
{
Uid origen_id, procedencia_id;
istringstream s(rif);
s >> origen_id >> procedencia_id;
return make_pair(origen_id, procedencia_id);
}
struct MetaProducto;
struct MetaInsumo;
struct TablaMetaProductos;
struct TablaMetaInsumos;
struct Productor
{ // cod_aran, planta_id, cantidad-declarada
using Prod = pair<Uid, tuple<string, Uid, double>>;
bool nacional;
string rif;
string nombre;
// insumo_id, cod_aran
DynMapTree<Uid, tuple<string, Uid, double>> productos; // productos que fabrica
// Lista de socios accionistas. Cada par es rif accionista y %
DynList<pair<string, float>> socios;
float total_accionistas() const noexcept
{
return socios.foldl<float>(0.0, [] (float acu, auto p)
{
return acu + p.second;
});
}
Productor() {}
Productor(bool nac, const string & r, const string & nom)
: nacional(nac), rif(r), nombre(nom) {}
string to_string() const
{
stringstream s;
s << (nacional ? "VEN" : "EXT") << BLANK << rif << BLANK << nombre << endl
<< " Productos (" << productos.size() << "): ";
productos.for_each([&s] (auto p)
{
s << "(" << get<0>(p) << "," << get<0>(get<1>(p))
<< get<1>(get<1>(p)) << get<2>(get<1>(p)) << "), ";
});
return s.str();
}
friend ostream & operator << (ostream & out, const Productor & p)
{
out << (p.nacional ? "VEN" : "EXT")
<< BLANK << p.rif << BLANK << p.nombre << endl
<< " Productos (" << p.productos.size() << "): ";
p.productos.for_each([&out] (auto p)
{
out << "(" << get<0>(p) << "," << get<0>(get<1>(p))
<< get<1>(get<1>(p)) << get<2>(get<1>(p)) << "), ";
});
out << endl;
return out;
}
void save(ostream & out) const
{
out << nacional << BLANK << rif << endl
<< nombre << endl
<< productos.size();
productos.for_each([&out] (auto p)
{
out << BLANK << get<0>(p) << BLANK << get<0>(get<1>(p))
<< BLANK << get<1>(get<1>(p))
<< BLANK << get<2>(get<1>(p));
});
out << endl
<< socios.size();
socios.for_each([&out] (auto p)
{ out << BLANK << p.first << BLANK << p.second; });
out << endl;
}
void load(istream & in)
{
in >> nacional;
if (nacional)
in >> rif;
else
{
Uid origen_id, procedencia_id;
string nom;
in >> origen_id >> procedencia_id >> nom;
rif = rif_ext(origen_id, procedencia_id, nom);
}
in.ignore();
getline(in, nombre);
size_t n;
in >> n;
for (auto i = 0; i < n; ++i)
{
Uid id;
string cod_aran;
Uid planta_id;
double cantidad;
in >> id >> cod_aran >> planta_id >> cantidad;
productos.insert(id, make_tuple(cod_aran, planta_id, cantidad));
}
in >> n; // leer socios
for (auto i = 0; i < n; ++i)
{
pair<string, float> p;
in >> p.first >> p.second;
socios.append(p);
}
}
bool es_nacional() const noexcept { return nacional; }
bool es_extranjero() const noexcept { return not nacional; }
friend bool operator < (const Productor & p1, const Productor & p2) noexcept
{
return p1.rif < p2.rif;
}
};
struct MetaProducto
{
Uid id;
string cod_aran;
string nombre;
DynSetTree<string> productores; // lista de rif de productores
DynList<LinearFactor> comb; // lista uid insumo, cantidad
string to_string() const
{
stringstream s;
s << id << BLANK << cod_aran << BLANK << nombre << endl
<< "Productores rifs (" << productores.size() << ") :";
productores.for_each([&s] (auto p) { s << BLANK << p; });
s << endl
<< "Insumos id's (" << comb.size() << ") :";
sort(comb, [] (auto c1, auto c2) { return get<0>(c1) < get<0>(c2); }).
for_each([&s] (auto c) { s << BLANK << get<0>(c); });
return s.str();
}
friend ostream & operator << (ostream & out, const MetaProducto & p)
{
return out << p.to_string() << endl;
}
MetaProducto() {}
MetaProducto(Uid i, const string & cod, const string & nom,
const DynList<LinearFactor> & c)
: id(i), cod_aran(cod), nombre(nom), comb(c) {}
void save(ostream & out) const
{
out << id << BLANK << cod_aran << endl
<< nombre << endl
<< productores.size();
productores.for_each([&out] (auto s) { out << BLANK << s; });
out << endl
<< comb.size();
comb.for_each([&out] (auto c)
{
out << " " << get<0>(c) << " " << get<1>(c) << " "
<< get<2>(c) << " " << get<3>(c);
});
out << endl;
}
void load(istream & in)
{
in >> id >> cod_aran;
in.ignore();
getline(in, nombre);
size_t n;
in >> n; // leer productores
for (auto i = 0; i < n; ++i)
{
string rif;
in >> rif;
productores.append(rif);
}
in.ignore();
string line;
getline(in, line);
stringstream s(line);
s >> n; // leer fromula insumos
for (auto i = 0; i < n; ++i)
{
Uid id;
string cod;
double factor;
Uid arc_id;
s >> id >> cod >> factor >> arc_id;
comb.append(make_tuple(id, cod, factor, arc_id));
}
assert(eq(sort(comb.map<Uid>([] (auto c) { return get<0>(c); })),
sort(unique(comb).map<Uid>([] (auto c) { return get<0>(c); }))));
}
};
struct TablaProductores;
struct TablaMetaProductos : public DynArray<MetaProducto>
{
typedef bool (*CmpCod)(const MetaProducto &, const MetaProducto &);
CmpCod cmp_cod = [] (const MetaProducto & p1, const MetaProducto & p2)
{ return p1.cod_aran < p2.cod_aran; };
typedef bool (*CmpId)(const MetaProducto &, const MetaProducto &);
CmpId cmp_id = [] (const MetaProducto & p1, const MetaProducto & p2)
{ return p1.id < p2.id; };
DynArray<MetaProducto*> index;
inline void adjuntar_productores(const TablaProductores & tabla_productores);
TablaMetaProductos() {}
TablaMetaProductos(const TablaProductos & tabla_productos,
const TablaProductores & tabla_productores)
{
if (::verbose)
cout << "Building MetaProdutos table" << endl;
size_t n = tabla_productos.size();
size_t i = 0;
reserve(n);
tabla_productos.for_each([this, &i] (auto & p)
{
access(i++) = MetaProducto(p.id, p.cod_aran, p.nombre, p.comb);
});
if (::verbose)
cout << "Done!" << endl
<< " Sorting" << endl;
in_place_sort(*this, cmp_cod);
if (::verbose)
cout << " done!" << endl
<< " Building index ... " << endl;
index = build_index_ptr(*this, cmp_id);
if (::verbose)
cout << " done!" << endl;
adjuntar_productores(tabla_productores);
}
DynList<MetaProducto*> operator () (const string & cod_aran) const noexcept
{
MetaProducto p; p.cod_aran = cod_aran;
return bsearch_dup(*this, p, cmp_cod);