forked from igraph/python-igraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattributes.c
More file actions
1895 lines (1658 loc) · 58.5 KB
/
Copy pathattributes.c
File metadata and controls
1895 lines (1658 loc) · 58.5 KB
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
/* vim:set ts=2 sw=2 sts=2 et: */
/*
IGraph library.
Copyright (C) 2006-2012 Tamas Nepusz <ntamas@gmail.com>
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., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include <Python.h>
#include "attributes.h"
#include "common.h"
#include "convert.h"
#include "py2compat.h"
#include "pyhelpers.h"
int igraphmodule_i_attribute_struct_init(igraphmodule_i_attribute_struct *attrs) {
int i;
for (i=0; i<3; i++) {
attrs->attrs[i] = PyDict_New();
if (PyErr_Occurred())
return 1;
RC_ALLOC("dict", attrs->attrs[i]);
}
attrs->vertex_name_index = 0;
return 0;
}
void igraphmodule_i_attribute_struct_destroy(igraphmodule_i_attribute_struct *attrs) {
int i;
for (i=0; i<3; i++) {
if (attrs->attrs[i]) {
RC_DEALLOC("dict", attrs->attrs[i]);
Py_DECREF(attrs->attrs[i]);
}
}
if (attrs->vertex_name_index) {
RC_DEALLOC("dict", attrs->vertex_name_index);
Py_DECREF(attrs->vertex_name_index);
}
}
int igraphmodule_i_attribute_struct_index_vertex_names(
igraphmodule_i_attribute_struct *attrs, igraph_bool_t force) {
Py_ssize_t n = 0;
PyObject *name_list, *key, *value;
if (attrs->vertex_name_index && !force)
return 0;
if (attrs->vertex_name_index == 0) {
attrs->vertex_name_index = PyDict_New();
if (attrs->vertex_name_index == 0) {
return 1;
}
} else
PyDict_Clear(attrs->vertex_name_index);
name_list = PyDict_GetItemString(attrs->attrs[1], "name");
if (name_list == 0)
return 0; /* no name attribute */
n = PyList_Size(name_list) - 1;
while (n >= 0) {
key = PyList_GET_ITEM(name_list, n); /* we don't own a reference to key */
value = PyInt_FromLong(n); /* we do own a reference to value */
if (value == 0)
return 1;
PyDict_SetItem(attrs->vertex_name_index, key, value);
/* PyDict_SetItem did an INCREF for both the key and a value, therefore we
* have to drop our reference on value */
Py_DECREF(value);
n--;
}
return 0;
}
void igraphmodule_i_attribute_struct_invalidate_vertex_name_index(
igraphmodule_i_attribute_struct *attrs) {
if (attrs->vertex_name_index == 0)
return;
Py_DECREF(attrs->vertex_name_index);
attrs->vertex_name_index = 0;
}
void igraphmodule_invalidate_vertex_name_index(igraph_t *graph) {
igraphmodule_i_attribute_struct_invalidate_vertex_name_index(ATTR_STRUCT(graph));
}
void igraphmodule_index_vertex_names(igraph_t *graph, igraph_bool_t force) {
igraphmodule_i_attribute_struct_index_vertex_names(ATTR_STRUCT(graph), force);
}
int igraphmodule_PyObject_matches_attribute_record(PyObject* object, igraph_attribute_record_t* record) {
#ifndef IGRAPH_PYTHON3
int result;
#endif
if (record == 0) {
return 0;
}
if (PyString_Check(object)) {
return PyString_IsEqualToASCIIString(object, record->name);
}
#ifndef IGRAPH_PYTHON3
/* On Python 2.x, we need to handle Unicode strings as well because
* the user might use 'from __future__ import unicode_literals', which
* would turn some igraph attribute names into Unicode strings */
if (PyUnicode_Check(object)) {
PyObject* ascii = PyUnicode_AsASCIIString(object);
if (ascii == 0) {
return 0;
}
result = PyString_IsEqualToASCIIString(ascii, record->name);
Py_DECREF(ascii);
return result;
}
#endif
return 0;
}
int igraphmodule_get_vertex_id_by_name(igraph_t *graph, PyObject* o, igraph_integer_t* vid) {
igraphmodule_i_attribute_struct* attrs = ATTR_STRUCT(graph);
PyObject* o_vid = NULL;
int tmp;
if (graph) {
attrs = ATTR_STRUCT(graph);
if (igraphmodule_i_attribute_struct_index_vertex_names(attrs, 0))
return 1;
o_vid = PyDict_GetItem(attrs->vertex_name_index, o);
}
if (o_vid == NULL) {
#ifdef IGRAPH_PYTHON3
PyErr_Format(PyExc_ValueError, "no such vertex: %R", o);
#else
PyObject* s = PyObject_Repr(o);
if (s) {
PyErr_Format(PyExc_ValueError, "no such vertex: %s", PyString_AS_STRING(s));
Py_DECREF(s);
} else {
PyErr_Format(PyExc_ValueError, "no such vertex: %p", o);
}
#endif
return 1;
}
if (!PyInt_Check(o_vid)) {
PyErr_SetString(PyExc_ValueError, "non-numeric vertex ID assigned to vertex name. This is most likely a bug.");
return 1;
}
if (PyInt_AsInt(o_vid, &tmp))
return 1;
*vid = tmp;
return 0;
}
/**
* \brief Checks whether the given graph has the given graph attribute.
*
* \param graph the graph
* \param name the name of the attribute being searched for
*/
igraph_bool_t igraphmodule_has_graph_attribute(const igraph_t *graph, const char* name) {
PyObject *dict = ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_GRAPH];
return name != 0 && dict != 0 && PyDict_GetItemString(dict, name) != 0;
}
/**
* \brief Checks whether the given graph has the given vertex attribute.
*
* \param graph the graph
* \param name the name of the attribute being searched for
*/
igraph_bool_t igraphmodule_has_vertex_attribute(const igraph_t *graph, const char* name) {
PyObject *dict = ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_VERTEX];
return name != 0 && dict != 0 && PyDict_GetItemString(dict, name) != 0;
}
/**
* \brief Checks whether the given graph has the given edge attribute.
*
* \param graph the graph
* \param name the name of the attribute being searched for
*/
igraph_bool_t igraphmodule_has_edge_attribute(const igraph_t *graph, const char* name) {
PyObject *dict = ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_EDGE];
return name != 0 && dict != 0 && PyDict_GetItemString(dict, name) != 0;
}
/**
* \brief Creates a new edge attribute and sets the values to None.
*
* This returns the actual list that we use to store the edge attributes, so
* be careful when modifying it - any modification will propagate back to the
* graph itself. You have been warned.
*
* \param graph the graph
* \param name the name of the attribute being created
* \returns a Python list of the values or \c NULL if the given
* attribute exists already (no exception set). The returned
* reference is borrowed.
*/
PyObject* igraphmodule_create_edge_attribute(const igraph_t* graph,
const char* name) {
PyObject *dict = ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_EDGE];
PyObject *values;
Py_ssize_t i, n;
if (dict == 0) {
dict = ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_EDGE] = PyDict_New();
}
if (PyDict_GetItemString(dict, name))
return 0;
n = igraph_ecount(graph);
values = PyList_New(n);
if (values == 0)
return 0;
for (i = 0; i < n; i++) {
Py_INCREF(Py_None);
PyList_SET_ITEM(values, i, Py_None); /* reference stolen */
}
if (PyDict_SetItemString(dict, name, values)) {
Py_DECREF(values);
return 0;
}
Py_DECREF(values);
return values;
}
/**
* \brief Returns the values of the given edge attribute for all edges in the
* given graph.
*
* This returns the actual list that we use to store the edge attributes, so
* be careful when modifying it - any modification will propagate back to the
* graph itself. You have been warned.
*
* \param graph the graph
* \param name the name of the attribute being searched for
* \returns a Python list or \c NULL if there is no such attribute
* (no exception set). The returned reference is borrowed.
*/
PyObject* igraphmodule_get_edge_attribute_values(const igraph_t* graph,
const char* name) {
PyObject *dict = ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_EDGE];
if (dict == 0)
return 0;
return PyDict_GetItemString(dict, name);
}
/**
* \brief Returns the values of the given edge attribute for all edges in the
* given graph, optionally creating it if it does not exist.
*
* This returns the actual list that we use to store the edge attributes, so
* be careful when modifying it - any modification will propagate back to the
* graph itself. You have been warned.
*
* \param graph the graph
* \param name the name of the attribute being searched for
* \returns a Python list (borrowed reference)
*/
PyObject* igraphmodule_create_or_get_edge_attribute_values(const igraph_t* graph,
const char* name) {
PyObject *dict = ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_EDGE], *result;
if (dict == 0)
return 0;
result = PyDict_GetItemString(dict, name);
if (result != 0)
return result;
return igraphmodule_create_edge_attribute(graph, name);
}
/* Attribute handlers for the Python interface */
/* Initialization */
static int igraphmodule_i_attribute_init(igraph_t *graph, igraph_vector_ptr_t *attr) {
igraphmodule_i_attribute_struct* attrs;
long int i, n;
attrs=(igraphmodule_i_attribute_struct*)calloc(1, sizeof(igraphmodule_i_attribute_struct));
if (!attrs)
IGRAPH_ERROR("not enough memory to allocate attribute hashes", IGRAPH_ENOMEM);
if (igraphmodule_i_attribute_struct_init(attrs)) {
PyErr_PrintEx(0);
free(attrs);
IGRAPH_ERROR("not enough memory to allocate attribute hashes", IGRAPH_ENOMEM);
}
graph->attr=(void*)attrs;
/* See if we have graph attributes */
if (attr) {
PyObject *dict=attrs->attrs[0], *value;
char *s;
n = igraph_vector_ptr_size(attr);
for (i=0; i<n; i++) {
igraph_attribute_record_t *attr_rec;
attr_rec = VECTOR(*attr)[i];
switch (attr_rec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
value=PyFloat_FromDouble((double)VECTOR(*(igraph_vector_t*)attr_rec->value)[0]);
break;
case IGRAPH_ATTRIBUTE_STRING:
igraph_strvector_get((igraph_strvector_t*)attr_rec->value, 0, &s);
if (s == 0)
value=PyString_FromString("");
else
value=PyString_FromString(s);
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
value=VECTOR(*(igraph_vector_bool_t*)attr_rec->value)[0] ? Py_True : Py_False;
Py_INCREF(value);
break;
default:
IGRAPH_WARNING("unsupported attribute type (not string, numeric or Boolean)");
value=0;
break;
}
if (value) {
if (PyDict_SetItemString(dict, attr_rec->name, value)) {
Py_DECREF(value);
igraphmodule_i_attribute_struct_destroy(attrs);
free(graph->attr); graph->attr = 0;
IGRAPH_ERROR("failed to add attributes to graph attribute hash",
IGRAPH_FAILURE);
}
Py_DECREF(value);
value=0;
}
}
}
return IGRAPH_SUCCESS;
}
/* Destruction */
static void igraphmodule_i_attribute_destroy(igraph_t *graph) {
igraphmodule_i_attribute_struct* attrs;
/* printf("Destroying attribute table\n"); */
if (graph->attr) {
attrs=(igraphmodule_i_attribute_struct*)graph->attr;
igraphmodule_i_attribute_struct_destroy(attrs);
free(attrs);
}
}
/* Copying */
static int igraphmodule_i_attribute_copy(igraph_t *to, const igraph_t *from,
igraph_bool_t ga, igraph_bool_t va, igraph_bool_t ea) {
igraphmodule_i_attribute_struct *fromattrs, *toattrs;
PyObject *key, *value, *newval, *o=NULL;
igraph_bool_t copy_attrs[3] = { ga, va, ea };
int i, j;
Py_ssize_t pos = 0;
if (from->attr) {
fromattrs=ATTR_STRUCT(from);
/* what to do with the original value of toattrs? */
toattrs=(igraphmodule_i_attribute_struct*)calloc(1, sizeof(igraphmodule_i_attribute_struct));
if (!toattrs)
IGRAPH_ERROR("not enough memory to allocate attribute hashes", IGRAPH_ENOMEM);
if (igraphmodule_i_attribute_struct_init(toattrs)) {
PyErr_PrintEx(0);
free(toattrs);
IGRAPH_ERROR("not enough memory to allocate attribute hashes", IGRAPH_ENOMEM);
}
to->attr=toattrs;
for (i=0; i<3; i++) {
if (!copy_attrs[i])
continue;
if (!PyDict_Check(fromattrs->attrs[i])) {
toattrs->attrs[i]=fromattrs->attrs[i];
Py_XINCREF(fromattrs->attrs[i]);
continue;
}
pos = 0;
while (PyDict_Next(fromattrs->attrs[i], &pos, &key, &value)) {
/* value is only borrowed, so copy it */
if (i>0) {
newval=PyList_New(PyList_GET_SIZE(value));
for (j=0; j<PyList_GET_SIZE(value); j++) {
o=PyList_GetItem(value, j);
Py_INCREF(o);
PyList_SetItem(newval, j, o);
}
} else {
newval=value;
Py_INCREF(newval);
}
PyDict_SetItem(toattrs->attrs[i], key, newval);
Py_DECREF(newval); /* compensate for PyDict_SetItem */
}
}
}
return IGRAPH_SUCCESS;
}
/* Adding vertices */
static int igraphmodule_i_attribute_add_vertices(igraph_t *graph, long int nv, igraph_vector_ptr_t *attr) {
/* Extend the end of every value in the vertex hash with nv pieces of None */
PyObject *key, *value, *dict;
long int i, j, k, l;
igraph_attribute_record_t *attr_rec;
igraph_bool_t *added_attrs=0;
Py_ssize_t pos = 0;
if (!graph->attr) return IGRAPH_SUCCESS;
if (nv<0) return IGRAPH_SUCCESS;
if (attr) {
added_attrs = (igraph_bool_t*)calloc((size_t)igraph_vector_ptr_size(attr),
sizeof(igraph_bool_t));
if (!added_attrs)
IGRAPH_ERROR("can't add vertex attributes", IGRAPH_ENOMEM);
IGRAPH_FINALLY(free, added_attrs);
}
dict=ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_VERTEX];
if (!PyDict_Check(dict))
IGRAPH_ERROR("vertex attribute hash type mismatch", IGRAPH_EINVAL);
while (PyDict_Next(dict, &pos, &key, &value)) {
if (!PyList_Check(value))
IGRAPH_ERROR("vertex attribute hash member is not a list", IGRAPH_EINVAL);
/* Check if we have specific values for the given attribute */
attr_rec=0;
if (attr) {
j=igraph_vector_ptr_size(attr);
for (i=0; i<j; i++) {
attr_rec=VECTOR(*attr)[i];
if (igraphmodule_PyObject_matches_attribute_record(key, attr_rec)) {
added_attrs[i]=1;
break;
}
attr_rec=0;
}
}
/* If we have specific values for the given attribute, attr_rec contains
* the appropriate vector. If not, it is null. */
if (attr_rec) {
for (i=0; i<nv; i++) {
char *s;
PyObject *o;
switch (attr_rec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
o=PyFloat_FromDouble((double)VECTOR(*(igraph_vector_t*)attr_rec->value)[i]);
break;
case IGRAPH_ATTRIBUTE_STRING:
igraph_strvector_get((igraph_strvector_t*)attr_rec->value, i, &s);
o=PyString_FromString(s);
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
o=VECTOR(*(igraph_vector_bool_t*)attr_rec->value)[i] ? Py_True : Py_False;
Py_INCREF(o);
break;
default:
IGRAPH_WARNING("unsupported attribute type (not string, numeric or Boolean)");
o=0;
break;
}
if (o) {
if (PyList_Append(value, o) == -1)
IGRAPH_ERROR("can't extend a vertex attribute hash member", IGRAPH_FAILURE);
else Py_DECREF(o);
}
}
/* Invalidate the vertex name index if needed */
if (!strcmp(attr_rec->name, "name"))
igraphmodule_i_attribute_struct_invalidate_vertex_name_index(ATTR_STRUCT(graph));
} else {
for (i=0; i<nv; i++) {
if (PyList_Append(value, Py_None) == -1) {
IGRAPH_ERROR("can't extend a vertex attribute hash member", IGRAPH_FAILURE);
}
}
}
}
/* Okay, now we added the new attribute values for the already existing
* attribute keys. Let's see if we have something left */
if (attr) {
l=igraph_vector_ptr_size(attr);
j=igraph_vcount(graph)-nv;
/* j contains the number of vertices EXCLUDING the new ones! */
for (k=0; k<l; k++) {
if (added_attrs[k]) continue;
attr_rec=(igraph_attribute_record_t*)VECTOR(*attr)[k];
value=PyList_New(j + nv);
if (!value) {
IGRAPH_ERROR("can't add attributes", IGRAPH_ENOMEM);
}
for (i=0; i<j; i++) {
Py_INCREF(Py_None);
PyList_SET_ITEM(value, i, Py_None);
}
for (i=0; i<nv; i++) {
char *s;
PyObject *o;
switch (attr_rec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
o=PyFloat_FromDouble((double)VECTOR(*(igraph_vector_t*)attr_rec->value)[i]);
break;
case IGRAPH_ATTRIBUTE_STRING:
igraph_strvector_get((igraph_strvector_t*)attr_rec->value, i, &s);
o=PyString_FromString(s);
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
o=VECTOR(*(igraph_vector_bool_t*)attr_rec->value)[i] ? Py_True : Py_False;
Py_INCREF(o);
break;
default:
IGRAPH_WARNING("unsupported attribute type (not string, numeric or Boolean)");
o=0;
break;
}
if (o) PyList_SET_ITEM(value, i+j, o);
}
/* Invalidate the vertex name index if needed */
if (!strcmp(attr_rec->name, "name"))
igraphmodule_i_attribute_struct_invalidate_vertex_name_index(ATTR_STRUCT(graph));
PyDict_SetItemString(dict, attr_rec->name, value);
Py_DECREF(value); /* compensate for PyDict_SetItemString */
}
free(added_attrs);
IGRAPH_FINALLY_CLEAN(1);
}
return IGRAPH_SUCCESS;
}
/* Permuting vertices */
static int igraphmodule_i_attribute_permute_vertices(const igraph_t *graph,
igraph_t *newgraph, const igraph_vector_t *idx) {
long int n, i;
PyObject *key, *value, *dict, *newdict, *newlist, *o;
Py_ssize_t pos=0;
dict=ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_VERTEX];
if (!PyDict_Check(dict)) return 1;
newdict=PyDict_New();
if (!newdict) return 1;
n=igraph_vector_size(idx);
pos=0;
while (PyDict_Next(dict, &pos, &key, &value)) {
newlist=PyList_New(n);
for (i=0; i<n; i++) {
o = PyList_GetItem(value, (Py_ssize_t)VECTOR(*idx)[i]);
if (!o) {
PyErr_Clear();
return 1;
}
Py_INCREF(o);
PyList_SET_ITEM(newlist, i, o);
}
PyDict_SetItem(newdict, key, newlist);
Py_DECREF(newlist);
}
dict = ATTR_STRUCT_DICT(newgraph)[ATTRHASH_IDX_VERTEX];
ATTR_STRUCT_DICT(newgraph)[ATTRHASH_IDX_VERTEX]=newdict;
Py_DECREF(dict);
/* Invalidate the vertex name index */
igraphmodule_i_attribute_struct_invalidate_vertex_name_index(ATTR_STRUCT(newgraph));
return 0;
}
/* Adding edges */
static int igraphmodule_i_attribute_add_edges(igraph_t *graph, const igraph_vector_t *edges, igraph_vector_ptr_t *attr) {
/* Extend the end of every value in the edge hash with ne pieces of None */
PyObject *key, *value, *dict;
Py_ssize_t pos=0;
long int i, j, k, l, ne;
igraph_bool_t *added_attrs=0;
igraph_attribute_record_t *attr_rec;
ne=igraph_vector_size(edges)/2;
if (!graph->attr) return IGRAPH_SUCCESS;
if (ne<0) return IGRAPH_SUCCESS;
if (attr) {
added_attrs = (igraph_bool_t*)calloc((size_t)igraph_vector_ptr_size(attr),
sizeof(igraph_bool_t));
if (!added_attrs)
IGRAPH_ERROR("can't add vertex attributes", IGRAPH_ENOMEM);
IGRAPH_FINALLY(free, added_attrs);
}
dict=ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_EDGE];
if (!PyDict_Check(dict))
IGRAPH_ERROR("edge attribute hash type mismatch", IGRAPH_EINVAL);
while (PyDict_Next(dict, &pos, &key, &value)) {
if (!PyList_Check(value))
IGRAPH_ERROR("edge attribute hash member is not a list", IGRAPH_EINVAL);
/* Check if we have specific values for the given attribute */
attr_rec=0;
if (attr) {
j=igraph_vector_ptr_size(attr);
for (i=0; i<j; i++) {
attr_rec=VECTOR(*attr)[i];
if (igraphmodule_PyObject_matches_attribute_record(key, attr_rec)) {
added_attrs[i]=1;
break;
}
attr_rec=0;
}
}
/* If we have specific values for the given attribute, attr_rec contains
* the appropriate vector. If not, it is null. */
if (attr_rec) {
for (i=0; i<ne; i++) {
char *s;
PyObject *o;
switch (attr_rec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
o=PyFloat_FromDouble((double)VECTOR(*(igraph_vector_t*)attr_rec->value)[i]);
break;
case IGRAPH_ATTRIBUTE_STRING:
igraph_strvector_get((igraph_strvector_t*)attr_rec->value, i, &s);
o=PyString_FromString(s);
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
o=VECTOR(*(igraph_vector_bool_t*)attr_rec->value)[i] ? Py_True : Py_False;
Py_INCREF(o);
break;
default:
IGRAPH_WARNING("unsupported attribute type (not string, numeric or Boolean)");
o=0;
break;
}
if (o) {
if (PyList_Append(value, o) == -1)
IGRAPH_ERROR("can't extend an edge attribute hash member", IGRAPH_FAILURE);
else Py_DECREF(o);
}
}
} else {
for (i=0; i<ne; i++) {
if (PyList_Append(value, Py_None) == -1) {
IGRAPH_ERROR("can't extend an edge attribute hash member", IGRAPH_FAILURE);
}
}
}
}
/*pos=0;
while (PyDict_Next(dict, &pos, &key, &value)) {
printf("key: "); PyObject_Print(key, stdout, Py_PRINT_RAW); printf("\n");
printf("value: "); PyObject_Print(value, stdout, Py_PRINT_RAW); printf("\n");
}*/
/* Okay, now we added the new attribute values for the already existing
* attribute keys. Let's see if we have something left */
if (attr) {
l=igraph_vector_ptr_size(attr);
j=igraph_ecount(graph)-ne;
/* j contains the number of edges EXCLUDING the new ones! */
for (k=0; k<l; k++) {
if (added_attrs[k]) continue;
attr_rec=(igraph_attribute_record_t*)VECTOR(*attr)[k];
value=PyList_New(j+ne);
if (!value) {
IGRAPH_ERROR("can't add attributes", IGRAPH_ENOMEM);
}
for (i=0; i<j; i++) {
Py_INCREF(Py_None);
PyList_SET_ITEM(value, i, Py_None);
}
for (i=0; i<ne; i++) {
char *s;
PyObject *o;
switch (attr_rec->type) {
case IGRAPH_ATTRIBUTE_NUMERIC:
o=PyFloat_FromDouble((double)VECTOR(*(igraph_vector_t*)attr_rec->value)[i]);
break;
case IGRAPH_ATTRIBUTE_STRING:
igraph_strvector_get((igraph_strvector_t*)attr_rec->value, i, &s);
o=PyString_FromString(s);
break;
case IGRAPH_ATTRIBUTE_BOOLEAN:
o=VECTOR(*(igraph_vector_bool_t*)attr_rec->value)[i] ? Py_True : Py_False;
Py_INCREF(o);
break;
default:
IGRAPH_WARNING("unsupported attribute type (not string, numeric or Boolean)");
o=0;
break;
}
if (o) PyList_SET_ITEM(value, i+j, o);
}
PyDict_SetItemString(dict, attr_rec->name, value);
Py_DECREF(value); /* compensate for PyDict_SetItemString */
}
free(added_attrs);
IGRAPH_FINALLY_CLEAN(1);
}
return IGRAPH_SUCCESS;
}
/* Deleting edges, currently unused */
/*
static void igraphmodule_i_attribute_delete_edges(igraph_t *graph, const igraph_vector_t *idx) {
long int n, i, ndeleted=0;
PyObject *key, *value, *dict, *o;
Py_ssize_t pos=0;
dict=ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_EDGE];
if (!PyDict_Check(dict)) return;
n=igraph_vector_size(idx);
for (i=0; i<n; i++) {
if (!VECTOR(*idx)[i]) {
ndeleted++;
continue;
}
pos=0;
while (PyDict_Next(dict, &pos, &key, &value)) {
o=PyList_GetItem(value, i);
if (!o) {
PyErr_Clear();
return;
}
Py_INCREF(o);
PyList_SetItem(value, VECTOR(*idx)[i]-1, o);
}
}
pos=0;
while (PyDict_Next(dict, &pos, &key, &value)) {
n=PySequence_Size(value);
if (PySequence_DelSlice(value, n-ndeleted, n) == -1) return;
}
return;
}
*/
/* Permuting edges */
static int igraphmodule_i_attribute_permute_edges(const igraph_t *graph,
igraph_t *newgraph, const igraph_vector_t *idx) {
long int n, i;
PyObject *key, *value, *dict, *newdict, *newlist, *o;
Py_ssize_t pos=0;
dict=ATTR_STRUCT_DICT(graph)[ATTRHASH_IDX_EDGE];
if (!PyDict_Check(dict)) return 1;
newdict=PyDict_New();
if (!newdict) return 1;
n=igraph_vector_size(idx);
pos=0;
while (PyDict_Next(dict, &pos, &key, &value)) {
newlist=PyList_New(n);
for (i=0; i<n; i++) {
o=PyList_GetItem(value, (Py_ssize_t)VECTOR(*idx)[i]);
if (!o) {
PyErr_Clear();
return 1;
}
Py_INCREF(o);
PyList_SET_ITEM(newlist, i, o);
}
PyDict_SetItem(newdict, key, newlist);
Py_DECREF(newlist);
}
dict = ATTR_STRUCT_DICT(newgraph)[ATTRHASH_IDX_EDGE];
ATTR_STRUCT_DICT(newgraph)[ATTRHASH_IDX_EDGE]=newdict;
Py_DECREF(dict);
return 0;
}
/* Auxiliary function for combining vertices/edges. Given a merge list
* (which specifies the vertex/edge IDs that were merged, the source
* attribute values and a Python callable to be called for every merge,
* returns a new list with the new attribute values. Each new attribute
* is derived by calling func with the old attributes of the set of
* merged vertices/edges as the first argument.
*/
static PyObject* igraphmodule_i_ac_func(PyObject* values,
const igraph_vector_ptr_t *merges, PyObject* func) {
long int i, len = igraph_vector_ptr_size(merges);
PyObject *res, *list, *item;
res = PyList_New(len);
for (i = 0; i < len; i++) {
igraph_vector_t *v = (igraph_vector_t*)VECTOR(*merges)[i];
long int j, n = igraph_vector_size(v);
list = PyList_New(n);
for (j = 0; j < n; j++) {
item = PyList_GET_ITEM(values, (Py_ssize_t)VECTOR(*v)[j]);
Py_INCREF(item);
PyList_SET_ITEM(list, j, item); /* reference to item stolen */
}
item = PyObject_CallFunctionObjArgs(func, list, 0);
Py_DECREF(list);
if (item == 0) {
Py_DECREF(res);
return 0;
}
PyList_SET_ITEM(res, i, item); /* reference to item stolen */
}
return res;
}
/* Auxiliary function for combining vertices/edges. Given a merge list
* (which specifies the vertex/edge IDs that were merged, the source
* attribute values and a name of a Python builtin function,
* returns a new list with the new attribute values. Each new attribute
* is derived by calling the given builtin function with the old
* attributes of the set of merged vertices/edges as the first argument.
*/
static PyObject* igraphmodule_i_ac_builtin_func(PyObject* values,
const igraph_vector_ptr_t *merges, const char* func_name) {
static PyObject* builtin_module_dict = 0;
PyObject* func = 0;
if (builtin_module_dict == 0) {
#ifdef IGRAPH_PYTHON3
PyObject* builtin_module = PyImport_ImportModule("builtins");
#else
PyObject* builtin_module = PyImport_ImportModule("__builtin__");
#endif
if (builtin_module == 0)
return 0;
builtin_module_dict = PyModule_GetDict(builtin_module);
Py_DECREF(builtin_module);
if (builtin_module_dict == 0)
return 0;
}
func = PyDict_GetItemString(builtin_module_dict, func_name);
if (func == 0) {
PyErr_Format(PyExc_NameError, "no such builtin function; %s", func_name);
return 0;
}
return igraphmodule_i_ac_func(values, merges, func);
}
/* Auxiliary function for combining vertices/edges. Given a merge list
* (which specifies the vertex/edge IDs that were merged and the source
* attribute values, returns a new list with the new attribute values.
* Each new attribute is derived from the sum of the attributes of
* the merged vertices/edges.
*/
static PyObject* igraphmodule_i_ac_sum(PyObject* values,
const igraph_vector_ptr_t *merges) {
long int i, len = igraph_vector_ptr_size(merges);
PyObject *res, *item;
res = PyList_New(len);
for (i = 0; i < len; i++) {
igraph_vector_t *v = (igraph_vector_t*)VECTOR(*merges)[i];
igraph_real_t num = 0.0, sum = 0.0;
long int j, n = igraph_vector_size(v);
for (j = 0; j < n; j++) {
item = PyList_GET_ITEM(values, (Py_ssize_t)VECTOR(*v)[j]);
if (igraphmodule_PyObject_to_real_t(item, &num)) {
PyErr_SetString(PyExc_TypeError, "product can only be invoked on numeric attributes");
Py_DECREF(res);
return 0;
}
sum += num;
}
/* reference to new float stolen */
PyList_SET_ITEM(res, i, PyFloat_FromDouble((double)sum));
}
return res;
}
/* Auxiliary function for combining vertices/edges. Given a merge list
* (which specifies the vertex/edge IDs that were merged and the source
* attribute values, returns a new list with the new attribute values.
* Each new attribute is derived from the product of the attributes of
* the merged vertices/edges.
*/
static PyObject* igraphmodule_i_ac_prod(PyObject* values,
const igraph_vector_ptr_t *merges) {
long int i, len = igraph_vector_ptr_size(merges);
PyObject *res, *item;
res = PyList_New(len);
for (i = 0; i < len; i++) {
igraph_vector_t *v = (igraph_vector_t*)VECTOR(*merges)[i];
igraph_real_t num = 1.0, prod = 1.0;
long int j, n = igraph_vector_size(v);
for (j = 0; j < n; j++) {
item = PyList_GET_ITEM(values, (Py_ssize_t)VECTOR(*v)[j]);
if (igraphmodule_PyObject_to_real_t(item, &num)) {
PyErr_SetString(PyExc_TypeError, "product can only be invoked on numeric attributes");
Py_DECREF(res);
return 0;
}
prod *= num;
}
/* reference to new float stolen */
PyList_SET_ITEM(res, i, PyFloat_FromDouble((double)prod));
}
return res;
}
/* Auxiliary function for combining vertices/edges. Given a merge list
* (which specifies the vertex/edge IDs that were merged and the source
* attribute values, returns a new list with the new attribute values.
* Each new attribute is derived from the first entry of the set of merged
* vertices/edges.
*/
static PyObject* igraphmodule_i_ac_first(PyObject* values,
const igraph_vector_ptr_t *merges) {
long int i, len = igraph_vector_ptr_size(merges);
PyObject *res, *item;
res = PyList_New(len);
for (i = 0; i < len; i++) {
igraph_vector_t *v = (igraph_vector_t*)VECTOR(*merges)[i];
long int n = igraph_vector_size(v);
if (n == 0)
continue;
item = PyList_GET_ITEM(values, (Py_ssize_t)VECTOR(*v)[0]);
Py_INCREF(item);
PyList_SET_ITEM(res, i, item); /* reference to item stolen */
}
return res;
}
/* Auxiliary function for combining vertices/edges. Given a merge list
* (which specifies the vertex/edge IDs that were merged and the source
* attribute values, returns a new list with the new attribute values.
* Each new attribute is derived from a randomly selected entry of the set of
* merged vertices/edges.
*/
static PyObject* igraphmodule_i_ac_random(PyObject* values,
const igraph_vector_ptr_t *merges) {
long int i, len = igraph_vector_ptr_size(merges);
PyObject *res, *item, *num;
PyObject *random_module = PyImport_ImportModule("random");