File: z29.html

package info (click to toggle)
ebook-dev-ggad 199908-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,264 kB
  • ctags: 1,163
  • sloc: sh: 44; makefile: 35
file content (1680 lines) | stat: -rw-r--r-- 67,573 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>
      Data Structures
    </title>
    <meta name="GENERATOR" content=
    "Modular DocBook HTML Stylesheet Version 1.45">
    <link rel="HOME" title="GTK+ / Gnome Application Development"
    href="ggad.html">
    <link rel="UP" title="glib: Portability and Utility" href= 
    "cha-glib.html">
    <link rel="PREVIOUS" title="glib: Portability and Utility"
    href="cha-glib.html">
    <link rel="NEXT" title="Other Features" href="z35.html">
  </head>
  <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= 
  "#840084" alink="#0000FF">
    <div class="NAVHEADER">
      <table width="100%" border="0" bgcolor="#ffffff" cellpadding= 
      "1" cellspacing="0">
        <tr>
          <th colspan="4" align="center">
            <font color="#000000" size="2">GTK+ / Gnome Application
            Development</font>
          </th>
        </tr>
        <tr>
          <td width="25%" bgcolor="#ffffff" align="left">
            <a href="cha-glib.html"><font color="#0000ff" size="2">
            <b>&lt;&lt;&lt; Previous</b></font></a>
          </td>
          <td width="25%" colspan="2" bgcolor="#ffffff" align= 
          "center">
            <font color="#0000ff" size="2"><b><a href="ggad.html">
            <font color="#0000ff" size="2"><b>
            Home</b></font></a></b></font>
          </td>
          <td width="25%" bgcolor="#ffffff" align="right">
            <a href="z35.html"><font color="#0000ff" size="2"><b>
            Next &gt;&gt;&gt;</b></font></a>
          </td>
        </tr>
      </table>
    </div>
    <div class="SECT1">
      <h1 class="SECT1">
        <a name="Z29">Data Structures</a>
      </h1>
      <p>
        glib implements many common data structures, so you don't
        have to reinvent the wheel every time you want a linked
        list. This section covers glib's implementation of linked
        lists, sorted binary trees, N-ary trees, and hash tables.
      </p>
      <div class="SECT2">
        <h2 class="SECT2">
          <a name="Z30">Lists</a>
        </h2>
        <p>
          glib provides generic single and doubly linked lists,
          <span class="STRUCTNAME">GSList</span> and <span class= 
          "STRUCTNAME">GList</span>, respectively. These are
          implemented as lists of <span class="STRUCTNAME">
          gpointer</span>; you can use them to hold integers with
          the <tt class="FUNCTION">GINT_TO_POINTER</tt> and <tt
          class="FUNCTION">GPOINTER_TO_INT</tt> macros. <span
          class="STRUCTNAME">GSList</span> and <span class= 
          "STRUCTNAME">GList</span> have identical API's, except
          that there is a <tt class="FUNCTION">
          g_list_previous()</tt> function and no <tt class=
          "FUNCTION">g_slist_previous()</tt>. This section will
          discuss <span class="STRUCTNAME">GSList</span> but
          everything also applies to the doubly linked list.
        </p>
        <p>
          In the glib implementation, the empty list is simply a
          <span class="STRUCTNAME">NULL</span> pointer. It's always
          safe to pass <span class="STRUCTNAME">NULL</span> to list
          functions since it's a valid list of length 0. Code to
          create a list and add one element might look like this:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;GSList* list = NULL;
gchar* element = g_strdup("a string");
list = g_slist_append(list, element);&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          glib lists have a noticeable Lisp influence; the empty
          list is a special "nil" value for that reason. <tt class= 
          "FUNCTION">g_slist_prepend()</tt> works much like <tt
          class="APPLICATION">cons</tt>---it's a constant-time
          operation that adds a new cell to the front of the list.
        </p>
        <p>
          Notice that you must replace the list passed to
          list-modifying functions with their return value, in case
          the head of the list changes. glib will handle memory
          issues, deallocating and allocating list links as needed.
        </p>
        <p>
          For example, the following code would remove the
          above-added element and empty the list:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;list = g_slist_remove(list, element);&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          <span class="STRUCTNAME">list</span> is now <span class= 
          "STRUCTNAME">NULL</span>. You still have to free <span
          class="STRUCTNAME">element</span> yourself, of course. To
          clear an entire list, use <tt class="FUNCTION">
          g_slist_free()</tt>, which removes all the links in one
          fell swoop. <tt class="FUNCTION">g_slist_free()</tt> has
          no return value because it would always be <span class= 
          "STRUCTNAME">NULL</span>, and you can simply assign that
          value to your list if you like. Obviously, <tt class= 
          "FUNCTION">g_slist_free()</tt> frees only the list cells;
          it has no way of knowing what to do with the list
          contents.
        </p>
        <p>
          To access a list element, you refer to the <span class= 
          "STRUCTNAME">GSList</span> struct directly:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;gchar* my_data = list-&gt;data;&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          To iterate over the list, you might write code like this:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;GSList* tmp = list;
while (tmp != NULL)
  {
    printf("List data: %p\n", tmp-&gt;data);
    tmp = g_slist_next(tmp);
  }&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          <a href="z29.html#FL-LISTCHANGING">Figure 13</a> shows
          the basic functions for changing <span class=
          "STRUCTNAME">GSList</span> contents. For all of these,
          you must assign the return value to your list pointer in
          case the head of the list changes. Note that glib does <i
          class="EMPHASIS">not</i> store a pointer to the tail of
          the list, so prepending is a constant-time operation,
          while append, insert, and remove are proportional to the
          list's size.
        </p>
        <p>
          In particular, this means that constructing a list using
          <tt class="FUNCTION">g_slist_append()</tt> is a <i class= 
          "EMPHASIS">terrible</i> idea; use <tt class="FUNCTION">
          g_slist_prepend()</tt> and then call <tt class=
          "FUNCTION">g_slist_reverse()</tt> if you need items in a
          particular order. If you anticipate frequently appending
          to a list, you can also keep a pointer to the last
          element. The following code can be used to perform
          efficient appends:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;void
efficient_append(GSList** list, GSList** list_end, gpointer data)
{
  g_return_if_fail(list != NULL);
  g_return_if_fail(list_end != NULL);

  if (*list == NULL)
    {
      g_assert(*list_end == NULL);
      
      *list = g_slist_append(*list, data);
      *list_end = *list;     
    }
  else 
    {
      *list_end = g_slist_append(*list_end, data)-&gt;next;
    }
} &#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          To use this function, you would store the list and its
          end somewhere, and pass their address to <tt class= 
          "FUNCTION">efficient_append()</tt>:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;  GSList* list = NULL;
  GSList* list_end = NULL;

  efficient_append(&amp;list, &amp;list_end, g_strdup("Foo"));
  efficient_append(&amp;list, &amp;list_end, g_strdup("Bar"));
  efficient_append(&amp;list, &amp;list_end, g_strdup("Baz"));&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          Of course you have to be careful not to use any list
          functions that might change the end of the list without
          updating <span class="STRUCTNAME">list_end</span>.
        </p>
        <div class="FIGURE">
          <a name="FL-LISTCHANGING"></a>
          <div class="FUNCSYNOPSIS">
            <a name="FL-LISTCHANGING.SYNOPSIS"></a>
            <table border="0" bgcolor="#E0E0E0" width="100%">
              <tr>
                <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                </td>
              </tr>
            </table>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_append</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, gpointer <tt
              class="PARAMETER"><i>data</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_prepend</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, gpointer <tt
              class="PARAMETER"><i>data</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_insert</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, gpointer <tt
              class="PARAMETER"><i>data</i></tt>, gint <tt class= 
              "PARAMETER"><i>position</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_remove</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, gpointer <tt
              class="PARAMETER"><i>data</i></tt>);</code>
            </p>
          </div>
          <p>
            <b>Figure 13. Changing linked list contents</b>
          </p>
        </div>
        <p>
          For accessing list elements, the functions in <a href= 
          "z29.html#FL-LISTACCESS">Figure 14</a> are provided. None
          of these change the list's structure. <tt class=
          "FUNCTION">g_slist_foreach()</tt> applies a <span class= 
          "STRUCTNAME">GFunc</span> to each element of the list. A
          <span class="STRUCTNAME">GFunc</span> is defined as
          follows:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;typedef void (*GFunc)(gpointer data, gpointer user_data);&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          Used in <tt class="FUNCTION">g_slist_foreach()</tt>, your
          <span class="STRUCTNAME">GFunc</span> will be called on
          each <span class="STRUCTNAME">list-&gt;data</span> in
          <span class="STRUCTNAME">list</span>, passing the <span
          class="STRUCTNAME">user_data</span> you provided to <tt
          class="FUNCTION">g_slist_foreach()</tt>. <tt class= 
          "FUNCTION">g_slist_foreach()</tt> is comparable to
          Scheme's "map" function.
        </p>
        <p>
          For example, you might have a list of strings, and you
          might want to be able to create a parallel list with some
          transformation applied to the strings. Here is some code,
          using the <tt class="FUNCTION">efficient_append()</tt>
          function from an earlier example:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;typedef struct _AppendContext AppendContext;
struct _AppendContext {
  GSList* list;
  GSList* list_end;
  const gchar* append;
};

static void 
append_foreach(gpointer data, gpointer user_data)
{
  AppendContext* ac = (AppendContext*) user_data;
  gchar* oldstring = (gchar*) data;

  efficient_append(&amp;ac-&gt;list, &amp;ac-&gt;list_end, 
                   g_strconcat(oldstring, ac-&gt;append, NULL));
}

GSList*
copy_with_append(GSList* list_of_strings, const gchar* append)
{
  AppendContext ac;

  ac.list = NULL;
  ac.list_end = NULL;
  ac.append = append;

  g_slist_foreach(list_of_strings, append_foreach, &amp;ac);

  return ac.list;
}&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          glib and GTK+ use the "function pointer and user data"
          idiom heavily. If you have functional programming
          experience, this is much like using lambda expressions to
          create a <i class="FIRSTTERM">closure</i>. (A closure
          combines a function with an <i class="FIRSTTERM">
          environment</i>---a set of name-value bindings. In this
          case the "environment" is the user data you pass to <tt
          class="FUNCTION">append_foreach()</tt>, and the "closure"
          is the combination of the function pointer and the user
          data.)
        </p>
        <div class="FIGURE">
          <a name="FL-LISTACCESS"></a>
          <div class="FUNCSYNOPSIS">
            <a name="FL-LISTACCESS.SYNOPSIS"></a>
            <table border="0" bgcolor="#E0E0E0" width="100%">
              <tr>
                <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                </td>
              </tr>
            </table>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_find</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, gpointer <tt
              class="PARAMETER"><i>data</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_nth</tt></code>(GSList* <tt class= 
              "PARAMETER"><i>list</i></tt>, guint <tt class= 
              "PARAMETER"><i>n</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">gpointer <tt class= 
              "FUNCTION">g_slist_nth_data</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, guint <tt class= 
              "PARAMETER"><i>n</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_last</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">gint <tt class=
              "FUNCTION">g_slist_index</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, gpointer <tt
              class="PARAMETER"><i>data</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">void <tt class=
              "FUNCTION">g_slist_foreach</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, GFunc <tt class= 
              "PARAMETER"><i>func</i></tt>, gpointer <tt class= 
              "PARAMETER"><i>user_data</i></tt>);</code>
            </p>
          </div>
          <p>
            <b>Figure 14. Accessing data in a linked list</b>
          </p>
        </div>
        <p>
          There are some handy list-manipulation routines, listed
          in <a href="z29.html#FL-LISTMANIP">Figure 15</a>. With
          the exception of <tt class="FUNCTION">
          g_slist_copy()</tt>, all of these affect the lists
          in-place. Which means you must assign the return value
          and forget about the passed-in pointer, just as you do
          when adding or removing list elements. <tt class=
          "FUNCTION">g_slist_copy()</tt> returns a newly-allocated
          list, so you can continue to use both lists and must free
          both lists eventually.
        </p>
        <div class="FIGURE">
          <a name="FL-LISTMANIP"></a>
          <div class="FUNCSYNOPSIS">
            <a name="FL-LISTMANIP.SYNOPSIS"></a>
            <table border="0" bgcolor="#E0E0E0" width="100%">
              <tr>
                <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                </td>
              </tr>
            </table>
            <p>
              <code><code class="FUNCDEF">guint <tt class=
              "FUNCTION">g_slist_length</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_concat</tt></code>(GSList* <tt
              class="PARAMETER"><i>list1</i></tt>, GSList* <tt
              class="PARAMETER"><i>list2</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_reverse</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_copy</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>);</code>
            </p>
          </div>
          <p>
            <b>Figure 15. Manipulating a linked list</b>
          </p>
        </div>
        <p>
          Finally, there are some provisions for sorted lists,
          shown in <a href="z29.html#FL-LISTSORTED">Figure 16</a>.
          To use these, you must write a <span class="STRUCTNAME">
          GCompareFunc</span>, which is just like the comparison
          function in the standard C <tt class="FUNCTION">
          qsort()</tt>. Using glib types, this becomes:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;typedef gint (*GCompareFunc) (gconstpointer a, gconstpointer b);&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          If <span class="STRUCTNAME">a &lt; b</span>, the function
          should return a negative value; if <span class=
          "STRUCTNAME">a &gt; b</span> a positive value; if <span
          class="STRUCTNAME">a == b</span> it should return 0.
        </p>
        <p>
          Once you have a comparison function, you can insert an
          element into an already-sorted list, or sort an entire
          list. Lists are sorted in ascending order. You can even
          recycle your <span class="STRUCTNAME">GCompareFunc</span>
          to find list elements, using <tt class="FUNCTION">
          g_slist_find_custom()</tt>. (A word of caution: <span
          class="STRUCTNAME">GCompareFunc</span> is used
          inconsistently in glib; sometimes it glib expects an
          equality predicate instead of a <tt class="FUNCTION">
          qsort()</tt>-style function. However, the usage is
          consistent within the list API.)
        </p>
        <p>
          Be careful with sorted lists; misusing them can rapidly
          become very inefficient. For example, <tt class=
          "FUNCTION">g_slist_insert_sorted()</tt> is an O(n)
          operation, but if you use it in a loop to insert multiple
          elements the loop runs in exponential time. It's better
          to simply prepend all your elements, then call <tt class= 
          "FUNCTION">g_slist_sort()</tt>.
        </p>
        <div class="FIGURE">
          <a name="FL-LISTSORTED"></a>
          <div class="FUNCSYNOPSIS">
            <a name="FL-LISTSORTED.SYNOPSIS"></a>
            <table border="0" bgcolor="#E0E0E0" width="100%">
              <tr>
                <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                </td>
              </tr>
            </table>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_insert_sorted</tt></code>(GSList*
              <tt class="PARAMETER"><i>list</i></tt>, gpointer <tt
              class="PARAMETER"><i>data</i></tt>, GCompareFunc <tt
              class="PARAMETER"><i>func</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_sort</tt></code>(GSList* <tt
              class="PARAMETER"><i>list</i></tt>, GCompareFunc <tt
              class="PARAMETER"><i>func</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">GSList* <tt class= 
              "FUNCTION">g_slist_find_custom</tt></code>(GSList*
              <tt class="PARAMETER"><i>list</i></tt>, gpointer <tt
              class="PARAMETER"><i>data</i></tt>, GCompareFunc <tt
              class="PARAMETER"><i>func</i></tt>);</code>
            </p>
          </div>
          <p>
            <b>Figure 16. Sorted lists</b>
          </p>
        </div>
      </div>
      <div class="SECT2">
        <h2 class="SECT2">
          <a name="Z31">Trees</a>
        </h2>
        <p>
          There are two different kinds of tree in glib; <span
          class="STRUCTNAME">GTree</span> is your basic balanced
          binary tree, useful to store key-value pairs sorted by
          key; <span class="STRUCTNAME">GNode</span> stores
          arbitrary tree-structured data, such as a parse tree or
          taxonomy.
        </p>
        <div class="SECT3">
          <h3 class="SECT3">
            <a name="Z32">GTree</a>
          </h3>
          <p>
            To create and destroy a <span class="STRUCTNAME">
            GTree</span>, use the constructor-destructor pair
            displayed in <a href="z29.html#FL-TREECONSTRUCT">Figure
            17</a>. <span class="STRUCTNAME">GCompareFunc</span> is
            the same <tt class="FUNCTION">qsort()</tt>-style
            comparison function described for <span class= 
            "STRUCTNAME">GSList</span>; in this case it's used to
            compare keys in the tree.
          </p>
          <div class="FIGURE">
            <a name="FL-TREECONSTRUCT"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-TREECONSTRUCT.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">GTree* <tt class= 
                "FUNCTION">g_tree_new</tt></code>(GCompareFunc <tt
                class="PARAMETER"><i>
                key_compare_func</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">g_tree_destroy</tt></code>(GTree* <tt
                class="PARAMETER"><i>tree</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 17. Creating and destroying balanced binary
              trees</b>
            </p>
          </div>
          <p>
            Functions for manipulating the contents of the tree are
            shown in <a href="z29.html#FL-TREEMANIP">Figure 18</a>.
            All very straightforward; <tt class="FUNCTION">
            g_tree_insert()</tt> overwrites any existing value, so
            be careful if the existing value is your only pointer
            to a chunk of allocated memory. If <tt class=
            "FUNCTION">g_tree_lookup()</tt> fails to find the key,
            it returns <span class="STRUCTNAME">NULL</span>,
            otherwise it returns the associated value. Both keys
            and values have type <span class="STRUCTNAME">
            gpointer</span>, but the <tt class="FUNCTION">
            GPOINTER_TO_INT()</tt> and <tt class="FUNCTION">
            GPOINTER_TO_UINT()</tt> macros allow you to use
            integers instead.
          </p>
          <div class="FIGURE">
            <a name="FL-TREEMANIP"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-TREEMANIP.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">g_tree_insert</tt></code>(GTree* <tt
                class="PARAMETER"><i>tree</i></tt>, gpointer <tt
                class="PARAMETER"><i>key</i></tt>, gpointer <tt
                class="PARAMETER"><i>value</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">g_tree_remove</tt></code>(GTree* <tt
                class="PARAMETER"><i>tree</i></tt>, gpointer <tt
                class="PARAMETER"><i>key</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">gpointer <tt class= 
                "FUNCTION">g_tree_lookup</tt></code>(GTree* <tt
                class="PARAMETER"><i>tree</i></tt>, gpointer <tt
                class="PARAMETER"><i>key</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 18. Manipulating <span class="STRUCTNAME">
              GTree</span> contents</b>
            </p>
          </div>
          <p>
            There are two functions which give you an idea how
            large the tree is, shown in <a href= 
            "z29.html#FL-TREESIZE">Figure 19</a>.
          </p>
          <div class="FIGURE">
            <a name="FL-TREESIZE"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-TREESIZE.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">gint <tt class=
                "FUNCTION">g_tree_nnodes</tt></code>(GTree* <tt
                class="PARAMETER"><i>tree</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">gint <tt class=
                "FUNCTION">g_tree_height</tt></code>(GTree* <tt
                class="PARAMETER"><i>tree</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 19. Determining the size of a <span class= 
              "STRUCTNAME">GTree</span></b>
            </p>
          </div>
          <p>
            Using <tt class="FUNCTION">g_tree_traverse()</tt> (<a
            href="z29.html#FL-TREETRAVERSE">Figure 20</a>) you can
            walk the entire tree. To use it, you provide a <span
            class="STRUCTNAME">GTraverseFunc</span>, which is
            passed each key-value pair and a <span class=
            "STRUCTNAME">data</span> argument you give to <tt
            class="FUNCTION">g_tree_traverse()</tt>. Traversal
            continues as long as the <span class="STRUCTNAME">
            GTraverseFunc</span> returns <span class="STRUCTNAME">
            FALSE</span>; if it ever returns <span class=
            "STRUCTNAME">TRUE</span> then traversal stops. You can
            use this to search the tree by value. Here is the
            definition of <span class="STRUCTNAME">
            GTraverseFunc</span>:
          </p>
          <table border="0" bgcolor="#E0E0E0" width="100%">
            <tr>
              <td>
<pre class="PROGRAMLISTING">
&#13;typedef gint (*GTraverseFunc)(gpointer key, gpointer value, gpointer data);&#13;
</pre>
              </td>
            </tr>
          </table>
          <p>
            <span class="STRUCTNAME">GTraverseType</span> is an
            enumeration; there are four possible values. Here are
            their meanings with respect to <span class=
            "STRUCTNAME">GTree</span>.
          </p>
          <ul>
            <li>
              <p>
                <span class="STRUCTNAME">G_IN_ORDER</span> first
                recurses the left child of the node (the "lower"
                key according to your <span class="STRUCTNAME">
                GCompareFunc</span>), then calls the traversal
                function on the key-value pair of the current node,
                then recurses the right child. This traversal is in
                order from lowest to highest, according to your
                <span class="STRUCTNAME">GCompareFunc</span>.&#13;
              </p>
            </li>
            <li>
              <p>
                <span class="STRUCTNAME">G_PRE_ORDER</span> calls
                the traversal function on the key-value pair of the
                current node, then recurses the left child, then
                recurses the right child.&#13;
              </p>
            </li>
            <li>
              <p>
                <span class="STRUCTNAME">G_POST_ORDER</span>
                recurses the left child, then recurses the right
                child, and finally calls the traversal function on
                the current node's key-value pair.&#13;
              </p>
            </li>
            <li>
              <p>
                <span class="STRUCTNAME">G_LEVEL_ORDER</span> is
                only meaningful for <span class="STRUCTNAME">
                GNode</span>, it is not allowed with <span class= 
                "STRUCTNAME">GTree</span>.&#13;
              </p>
            </li>
          </ul>
          <div class="FIGURE">
            <a name="FL-TREETRAVERSE"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-TREETRAVERSE.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">g_tree_traverse</tt></code>(GTree* <tt
                class="PARAMETER"><i>tree</i></tt>, GTraverseFunc
                <tt class="PARAMETER"><i>traverse_func</i></tt>,
                GTraverseType <tt class="PARAMETER"><i>
                traverse_type</i></tt>, gpointer <tt class= 
                "PARAMETER"><i>data</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 20. Traversing <span class="STRUCTNAME">
              GTree</span></b>
            </p>
          </div>
        </div>
        <div class="SECT3">
          <h3 class="SECT3">
            <a name="Z33">GNode</a>
          </h3>
          <p>
            A <span class="STRUCTNAME">GNode</span> is an N-way
            tree, implemented as a doubly linked list with parent
            and child lists. Thus, most list operations have
            analogues in the <span class="STRUCTNAME">GNode</span>
            API. You can also walk the tree in various ways. Here's
            the declaration for a node:
          </p>
          <table border="0" bgcolor="#E0E0E0" width="100%">
            <tr>
              <td>
<pre class="PROGRAMLISTING">
&#13;typedef struct _GNode GNode;

struct _GNode
{
  gpointer data;
  GNode   *next;
  GNode   *prev;
  GNode   *parent;
  GNode   *children;
};&#13;
</pre>
              </td>
            </tr>
          </table>
          <p>
            There are macros to access <span class="STRUCTNAME">
            GNode</span> members, shown in <a href= 
            "z29.html#ML-NODEACCESS">Figure 21</a>. As with <span
            class="STRUCTNAME">GList</span>, the <span class= 
            "STRUCTNAME">data</span> member is intended to be used
            directly. These macros return the <span class= 
            "STRUCTNAME">next</span>, <span class="STRUCTNAME">
            prev</span>, and <span class="STRUCTNAME">
            children</span> members respectively; they also check
            whether their argument is <span class="STRUCTNAME">
            NULL</span> before dereferencing it, and return <span
            class="STRUCTNAME">NULL</span> if it is.
          </p>
          <div class="FIGURE">
            <a name="ML-NODEACCESS"></a>
            <div class="FUNCSYNOPSIS">
              <a name="ML-NODEACCESS.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                g_node_prev_sibling</tt></code>(<tt class=
                "PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                g_node_next_sibling</tt></code>(<tt class=
                "PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                g_node_first_child</tt></code>(<tt class=
                "PARAMETER"><i>node</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 21. Accessing <span class="STRUCTNAME">
              GNode</span> members</b>
            </p>
          </div>
          <p>
            To create a node, the usual <tt class="FUNCTION">
            _new()</tt> function is provided (<a href= 
            "z29.html#FL-NODENEW">Figure 22</a>). <tt class= 
            "FUNCTION">g_node_new()</tt> creates a childless and
            parentless node containing <span class="STRUCTNAME">
            data</span>. Typically <tt class="FUNCTION">
            g_node_new()</tt> is used only to create the root node;
            convenience macros are provided which automatically
            create new nodes as needed.
          </p>
          <div class="FIGURE">
            <a name="FL-NODENEW"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-NODENEW.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_new</tt></code>(gpointer <tt
                class="PARAMETER"><i>data</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 22. Creating a <span class="STRUCTNAME">
              GNode</span></b>
            </p>
          </div>
          <p>
            To build a tree the fundamental operations shown in <a
            href="z29.html#FL-NODEBUILD">Figure 23</a> are used.
            Each operation returns the just-added node, for
            convenience when writing loops or recursing the tree.
            Unlike <span class="STRUCTNAME">GList</span>, it is
            safe to ignore the return value.
          </p>
          <div class="FIGURE">
            <a name="FL-NODEBUILD"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-NODEBUILD.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_insert</tt></code>(GNode* <tt
                class="PARAMETER"><i>parent</i></tt>, gint <tt
                class="PARAMETER"><i>position</i></tt>, GNode* <tt
                class="PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_insert_before</tt></code>(GNode*
                <tt class="PARAMETER"><i>parent</i></tt>, GNode*
                <tt class="PARAMETER"><i>sibling</i></tt>, GNode*
                <tt class="PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_prepend</tt></code>(GNode* <tt
                class="PARAMETER"><i>parent</i></tt>, GNode* <tt
                class="PARAMETER"><i>node</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 23. Building a <span class="STRUCTNAME">
              GNode</span> tree</b>
            </p>
          </div>
          <p>
            The convenience macros shown in <a href= 
            "z29.html#ML-NODECONV">Figure 24</a> are implemented in
            terms of the fundamental operations. <tt class=
            "FUNCTION">g_node_append()</tt> is analagous to <tt
            class="FUNCTION">g_node_prepend()</tt>; the rest take a
            <span class="STRUCTNAME">data</span> argument,
            automatically allocate a node for it, and call the
            corresponding basic operation.
          </p>
          <div class="FIGURE">
            <a name="ML-NODECONV"></a>
            <div class="FUNCSYNOPSIS">
              <a name="ML-NODECONV.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                g_node_append</tt></code>(<tt class=
                "PARAMETER"><i>parent</i></tt>, <tt class=
                "PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                g_node_insert_data</tt></code>(<tt class=
                "PARAMETER"><i>parent</i></tt>, <tt class=
                "PARAMETER"><i>position</i></tt>, <tt class= 
                "PARAMETER"><i>data</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                g_node_insert_data_before</tt></code>(<tt class= 
                "PARAMETER"><i>parent</i></tt>, <tt class=
                "PARAMETER"><i>sibling</i></tt>, <tt class= 
                "PARAMETER"><i>data</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                g_node_prepend_data</tt></code>(<tt class=
                "PARAMETER"><i>parent</i></tt>, <tt class=
                "PARAMETER"><i>data</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                g_node_append_data</tt></code>(<tt class=
                "PARAMETER"><i>parent</i></tt>, <tt class=
                "PARAMETER"><i>data</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 24. Building a <span class="STRUCTNAME">
              GNode</span></b>
            </p>
          </div>
          <p>
            To remove a node from the tree, there are two functions
            shown in <a href="z29.html#FL-NODEDESTROY">Figure
            25</a>. <tt class="FUNCTION">g_node_destroy()</tt>
            removes the node from a tree, destroying it and all its
            children. <tt class="FUNCTION">g_node_unlink()</tt>
            removes a node and makes it into a root node; i.e., it
            converts a subtree into an independent tree.
          </p>
          <div class="FIGURE">
            <a name="FL-NODEDESTROY"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-NODEDESTROY.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">g_node_destroy</tt></code>(GNode* <tt
                class="PARAMETER"><i>root</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">g_node_unlink</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 25. Destroying a <span class="STRUCTNAME">
              GNode</span></b>
            </p>
          </div>
          <p>
            There are two macros for detecting the top and bottom
            of a <span class="STRUCTNAME">GNode</span> tree, shown
            in <a href="z29.html#ML-NODEEXTREMA">Figure 26</a>. A
            root node is defined as a node with no parent or
            siblings. A leaf node has no children.
          </p>
          <div class="FIGURE">
            <a name="ML-NODEEXTREMA"></a>
            <div class="FUNCSYNOPSIS">
              <a name="ML-NODEEXTREMA.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                G_NODE_IS_ROOT</tt></code>(<tt class=
                "PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF"><tt class="FUNCTION">
                G_NODE_IS_LEAF</tt></code>(<tt class=
                "PARAMETER"><i>node</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 26. Predicates for <span class=
              "STRUCTNAME">GNode</span></b>
            </p>
          </div>
          <p>
            You can ask glib to report useful information about a
            <span class="STRUCTNAME">GNode</span>, including the
            number of nodes it contains, its root node, its depth,
            and the node containing a particular data pointer.
            These functions are shown in <a href= 
            "z29.html#FL-NODEPROPERTIES">Figure 27</a>.
          </p>
          <p>
            <span class="STRUCTNAME">GTraverseType</span> was
            introduced earlier, with respect to <span class= 
            "STRUCTNAME">GTree</span>; here are the possible values
            for <span class="STRUCTNAME">GNode</span>:
          </p>
          <ul>
            <li>
              <p>
                <span class="STRUCTNAME">G_IN_ORDER</span> first
                recurses the leftmost child of the node, then
                visits the node itself, then recurses the rest of
                the node's children. This isn't very useful; mostly
                it is intended for use with <span class=
                "STRUCTNAME">GTree</span>.&#13;
              </p>
            </li>
            <li>
              <p>
                <span class="STRUCTNAME">G_PRE_ORDER</span> visits
                the current node, then recurses each child in
                turn.&#13;
              </p>
            </li>
            <li>
              <p>
                <span class="STRUCTNAME">G_POST_ORDER</span>
                recurses each child in order, then visits the
                current node.&#13;
              </p>
            </li>
            <li>
              <p>
                <span class="STRUCTNAME">G_LEVEL_ORDER</span> first
                visits the node itself; then each of the node's
                children; then the children of the children; then
                the children of the children of the children; and
                so on. That is, it visits each node of depth 0,
                then each node of depth 1, then each node of depth
                2, etc.&#13;
              </p>
            </li>
          </ul>
          <p>
            <span class="STRUCTNAME">GNode</span>'s tree-traversal
            functions have a <span class="STRUCTNAME">
            GTraverseFlags</span> argument. This is a bitfield used
            to change the nature of the traversal. Currently there
            are only three flags---you can visit only leaf nodes,
            only non-leaf nodes, or all nodes:
          </p>
          <ul>
            <li>
              <p>
                <span class="STRUCTNAME">G_TRAVERSE_LEAFS</span>
                means to traverse only leaf nodes.&#13;
              </p>
            </li>
            <li>
              <p>
                <span class="STRUCTNAME">
                G_TRAVERSE_NON_LEAFS</span> means to traverse only
                non-leaf nodes.&#13;
              </p>
            </li>
            <li>
              <p>
                <span class="STRUCTNAME">G_TRAVERSE_ALL</span> is
                simply a shortcut for <span class="STRUCTNAME">
                (G_TRAVERSE_LEAFS |
                G_TRAVERSE_NON_LEAFS)</span>.&#13;
              </p>
            </li>
          </ul>
          <div class="FIGURE">
            <a name="FL-NODEPROPERTIES"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-NODEPROPERTIES.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">guint <tt class= 
                "FUNCTION">g_node_n_nodes</tt></code>(GNode* <tt
                class="PARAMETER"><i>root</i></tt>, GTraverseFlags
                <tt class="PARAMETER"><i>flags</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_get_root</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">gboolean <tt class= 
                "FUNCTION">g_node_is_ancestor</tt></code>(GNode*
                <tt class="PARAMETER"><i>node</i></tt>, GNode* <tt
                class="PARAMETER"><i>descendant</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">guint <tt class= 
                "FUNCTION">g_node_depth</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_find</tt></code>(GNode* <tt
                class="PARAMETER"><i>root</i></tt>, GTraverseType
                <tt class="PARAMETER"><i>order</i></tt>,
                GTraverseFlags <tt class="PARAMETER"><i>
                flags</i></tt>, gpointer <tt class="PARAMETER"><i>
                data</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 27. <span class="STRUCTNAME">GNode</span>
              Properties</b>
            </p>
          </div>
          <p>
            The remaining <span class="STRUCTNAME">GNode</span>
            functions are straightforward; most of them are simply
            operations on the node's list of children. <a href= 
            "z29.html#FL-NODEACCESSORS">Figure 28</a> lists them.
            There are two function typedefs unique to <span class= 
            "STRUCTNAME">GNode</span>:
          </p>
          <table border="0" bgcolor="#E0E0E0" width="100%">
            <tr>
              <td>
<pre class="PROGRAMLISTING">
&#13;typedef gboolean (*GNodeTraverseFunc) (GNode* node, gpointer data);
typedef void (*GNodeForeachFunc) (GNode* node, gpointer data);&#13;
</pre>
              </td>
            </tr>
          </table>
          <p>
            These are called with a pointer to the node being
            visited, and the user data you provide. A <span class= 
            "STRUCTNAME">GNodeTraverseFunc</span> can return <span
            class="STRUCTNAME">TRUE</span> to stop whatever
            traversal is in progress; thus you can use <span class= 
            "STRUCTNAME">GNodeTraverseFunc</span> in combination
            with <tt class="FUNCTION">g_node_traverse()</tt> to
            search the tree by value.
          </p>
          <div class="FIGURE">
            <a name="FL-NODEACCESSORS"></a>
            <div class="FUNCSYNOPSIS">
              <a name="FL-NODEACCESSORS.SYNOPSIS"></a>
              <table border="0" bgcolor="#E0E0E0" width="100%">
                <tr>
                  <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                  </td>
                </tr>
              </table>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">g_node_traverse</tt></code>(GNode* <tt
                class="PARAMETER"><i>root</i></tt>, GTraverseType
                <tt class="PARAMETER"><i>order</i></tt>,
                GTraverseFlags <tt class="PARAMETER"><i>
                flags</i></tt>, gint <tt class="PARAMETER"><i>
                max_depth</i></tt>, GNodeTraverseFunc <tt class= 
                "PARAMETER"><i>func</i></tt>, gpointer <tt class= 
                "PARAMETER"><i>data</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">guint <tt class= 
                "FUNCTION">g_node_max_height</tt></code>(GNode* <tt
                class="PARAMETER"><i>root</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">
                g_node_children_foreach</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>, GTraverseFlags
                <tt class="PARAMETER"><i>flags</i></tt>,
                GNodeForeachFunc <tt class="PARAMETER"><i>
                func</i></tt>, gpointer <tt class="PARAMETER"><i>
                data</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">void <tt class=
                "FUNCTION">
                g_node_reverse_children</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">guint <tt class= 
                "FUNCTION">g_node_n_children</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_nth_child</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>, guint <tt
                class="PARAMETER"><i>n</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_last_child</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_find_child</tt></code>(GNode* <tt
                class="PARAMETER"><i>node</i></tt>, GTraverseFlags
                <tt class="PARAMETER"><i>flags</i></tt>, gpointer
                <tt class="PARAMETER"><i>data</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">gint <tt class=
                "FUNCTION">g_node_child_position</tt></code>(GNode*
                <tt class="PARAMETER"><i>node</i></tt>, GNode* <tt
                class="PARAMETER"><i>child</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">gint <tt class=
                "FUNCTION">g_node_child_index</tt></code>(GNode*
                <tt class="PARAMETER"><i>node</i></tt>, gpointer
                <tt class="PARAMETER"><i>data</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_first_sibling</tt></code>(GNode*
                <tt class="PARAMETER"><i>node</i></tt>);</code>
              </p>
              <p>
                <code><code class="FUNCDEF">GNode* <tt class= 
                "FUNCTION">g_node_last_sibling</tt></code>(GNode*
                <tt class="PARAMETER"><i>node</i></tt>);</code>
              </p>
            </div>
            <p>
              <b>Figure 28. Accessing a <span class="STRUCTNAME">
              GNode</span></b>
            </p>
          </div>
        </div>
      </div>
      <div class="SECT2">
        <h2 class="SECT2">
          <a name="Z34">Hash Tables</a>
        </h2>
        <p>
          <span class="STRUCTNAME">GHashTable</span> is a simple
          hash table implementation, providing an associative array
          with constant-time lookups. To use the hash table, you
          must provide a <span class="STRUCTNAME">GHashFunc</span>,
          which should return a positive integer when passed a hash
          key:
        </p>
        <table border="0" bgcolor="#E0E0E0" width="100%">
          <tr>
            <td>
<pre class="PROGRAMLISTING">
&#13;typedef guint (*GHashFunc) (gconstpointer key);&#13;
</pre>
            </td>
          </tr>
        </table>
        <p>
          Each returned <span class="STRUCTNAME">guint</span>
          (modulus the size of the table) corresponds to a "slot"
          or "bucket" in the hash; <span class="STRUCTNAME">
          GHashTable</span> handles collisions by storing a linked
          list of key-value pairs in each slot. Thus, the <span
          class="STRUCTNAME">guint</span> values returned by your
          <span class="STRUCTNAME">GHashFunc</span> must be fairly
          evenly distributed over the set of possible <span class= 
          "STRUCTNAME">guint</span> values, or the hash table will
          degenerate into a linked list. Your <span class= 
          "STRUCTNAME">GHashFunc</span> must also be fast, since it
          is used for every lookup.
        </p>
        <p>
          In addition to <span class="STRUCTNAME">GHashFunc</span>,
          a <span class="STRUCTNAME">GCompareFunc</span> is
          required to test keys for equality. Somewhat
          unpleasantly, <span class="STRUCTNAME">GHashTable</span>
          does not use <span class="STRUCTNAME">GCompareFunc</span>
          in the same way <span class="STRUCTNAME">GSList</span>
          and <span class="STRUCTNAME">GTree</span> do, although
          the function signature is the same. Here <span class= 
          "STRUCTNAME">GCompareFunc</span> is expected to be an
          equality operator, returning <span class="STRUCTNAME">
          TRUE</span> if its arguments are equal. It should <i
          class="EMPHASIS">not</i> be a <span class="STRUCTNAME">
          qsort()</span>-style comparison function. The key
          comparison function is used to find the correct key-value
          pair when hash collisions result in more than one pair in
          the same hash slot.
        </p>
        <p>
          To create and destroy a <span class="STRUCTNAME">
          GHashTable</span>, use the constructor and destructor
          listed in <a href="z29.html#FL-HASHNEW">Figure 29</a>.
          Remember that glib has no way of knowing how to destroy
          the data contained in your hash table; it only destroys
          the table itself.
        </p>
        <div class="FIGURE">
          <a name="FL-HASHNEW"></a>
          <div class="FUNCSYNOPSIS">
            <a name="FL-HASHNEW.SYNOPSIS"></a>
            <table border="0" bgcolor="#E0E0E0" width="100%">
              <tr>
                <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                </td>
              </tr>
            </table>
            <p>
              <code><code class="FUNCDEF">GHashTable* <tt class= 
              "FUNCTION">g_hash_table_new</tt></code>(GHashFunc <tt
              class="PARAMETER"><i>hash_func</i></tt>, GCompareFunc
              <tt class="PARAMETER"><i>
              key_compare_func</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">void <tt class=
              "FUNCTION">
              g_hash_table_destroy</tt></code>(GHashTable* <tt
              class="PARAMETER"><i>hash_table</i></tt>);</code>
            </p>
          </div>
          <p>
            <b>Figure 29. <span class="STRUCTNAME">
            GHashTable</span></b>
          </p>
        </div>
        <p>
          Ready-to-use hash and comparison functions are provided
          for the most common keys: integers, pointers, and
          strings. These are listed in <a href= 
          "z29.html#FL-HASHFUNCS">Figure 30</a>. The functions for
          integers accept a pointer to a <span class="STRUCTNAME">
          gint</span>, rather than the <span class="STRUCTNAME">
          gint</span> itself. If you pass <span class="STRUCTNAME">
          NULL</span> as the hash function argument to <tt class= 
          "FUNCTION">g_hash_table_new()</tt>, <tt class="FUNCTION">
          g_direct_hash()</tt> is used by default. If you pass
          <span class="STRUCTNAME">NULL</span> as the key equality
          function, then simple pointer comparison is used
          (equivalent to <span class="STRUCTNAME">
          g_direct_equal()</span>, but without a function call).
        </p>
        <div class="FIGURE">
          <a name="FL-HASHFUNCS"></a>
          <div class="FUNCSYNOPSIS">
            <a name="FL-HASHFUNCS.SYNOPSIS"></a>
            <table border="0" bgcolor="#E0E0E0" width="100%">
              <tr>
                <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                </td>
              </tr>
            </table>
            <p>
              <code><code class="FUNCDEF">guint <tt class=
              "FUNCTION">g_int_hash</tt></code>(gconstpointer <tt
              class="PARAMETER"><i>v</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">gint <tt class=
              "FUNCTION">g_int_equal</tt></code>(gconstpointer <tt
              class="PARAMETER"><i>v1</i></tt>, gconstpointer <tt
              class="PARAMETER"><i>v2</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">guint <tt class=
              "FUNCTION">g_direct_hash</tt></code>(gconstpointer
              <tt class="PARAMETER"><i>v</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">gint <tt class=
              "FUNCTION">g_direct_equal</tt></code>(gconstpointer
              <tt class="PARAMETER"><i>v1</i></tt>, gconstpointer
              <tt class="PARAMETER"><i>v2</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">guint <tt class=
              "FUNCTION">g_str_hash</tt></code>(gconstpointer <tt
              class="PARAMETER"><i>v</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">gint <tt class=
              "FUNCTION">g_str_equal</tt></code>(gconstpointer <tt
              class="PARAMETER"><i>v1</i></tt>, gconstpointer <tt
              class="PARAMETER"><i>v2</i></tt>);</code>
            </p>
          </div>
          <p>
            <b>Figure 30. Pre-written hashes/comparisons</b>
          </p>
        </div>
        <p>
          Manipulating the hash is simple. The routines are
          summarized in <a href="z29.html#FL-HASHMANIP">Figure
          31</a>. Insertions do <i class="EMPHASIS">not</i> copy
          the key or value; these are entered into the table
          exactly as you provide them, overwriting any pre-existing
          key-value pair with the same key ("same" is defined by
          your hash and equality functions, remember). If this is a
          problem, you must do a lookup or remove before you
          insert. Be especially careful if you dynamically allocate
          keys or values.
        </p>
        <p>
          The simple <tt class="FUNCTION">
          g_hash_table_lookup()</tt> returns the value it finds
          associated with <span class="STRUCTNAME">key</span>, or
          <span class="STRUCTNAME">NULL</span> if there is no
          value. Sometimes this won't do. For example, <span class= 
          "STRUCTNAME">NULL</span> may be a valid value in itself.
          If you're using strings as keys, especially dynamically
          allocated strings, knowing that a key is in the table
          might not be enough; you might want to retrieve the exact
          <span class="STRUCTNAME">gchar*</span> the hash table is
          using to represent key <span class="STRUCTNAME">
          "foo"</span>. A second lookup function is provided for
          cases like these. <tt class="FUNCTION">
          g_hash_table_lookup_extended()</tt> returns <span class= 
          "STRUCTNAME">TRUE</span> if the lookup succeeded; if it
          returns <span class="STRUCTNAME">TRUE</span>, it places
          the key and value it found in the locations it's given.
        </p>
        <div class="FIGURE">
          <a name="FL-HASHMANIP"></a>
          <div class="FUNCSYNOPSIS">
            <a name="FL-HASHMANIP.SYNOPSIS"></a>
            <table border="0" bgcolor="#E0E0E0" width="100%">
              <tr>
                <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                </td>
              </tr>
            </table>
            <p>
              <code><code class="FUNCDEF">void <tt class=
              "FUNCTION">
              g_hash_table_insert</tt></code>(GHashTable* <tt
              class="PARAMETER"><i>hash_table</i></tt>, gpointer
              <tt class="PARAMETER"><i>key</i></tt>, gpointer <tt
              class="PARAMETER"><i>value</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">void <tt class=
              "FUNCTION">g_hash_table_remove</tt></code>(GHashTable
              * <tt class="PARAMETER"><i>hash_table</i></tt>,
              gconstpointer <tt class="PARAMETER"><i>
              key</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">gpointer <tt class= 
              "FUNCTION">g_hash_table_lookup</tt></code>(GHashTable
              * <tt class="PARAMETER"><i>hash_table</i></tt>,
              gconstpointer <tt class="PARAMETER"><i>
              key</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">gboolean <tt class= 
              "FUNCTION">
              g_hash_table_lookup_extended</tt></code>(GHashTable*
              <tt class="PARAMETER"><i>hash_table</i></tt>,
              gconstpointer <tt class="PARAMETER"><i>
              lookup_key</i></tt>, gpointer* <tt class="PARAMETER">
              <i>orig_key</i></tt>, gpointer* <tt class=
              "PARAMETER"><i>value</i></tt>);</code>
            </p>
          </div>
          <p>
            <b>Figure 31. Manipulating <span class="STRUCTNAME">
            GHashTable</span></b>
          </p>
        </div>
        <p>
          <span class="STRUCTNAME">GHashTable</span> keeps an
          internal array whose size is a prime number. It also
          keeps a count of the number of key-value pairs stored in
          the table. If the average number of pairs per available
          slot drops below 0.3 (or so), the array is made smaller;
          if it goes above 3, the array is made larger to reduce
          collisions. Resizing happens automatically whenever you
          insert or remove pairs from the table. This ensures the
          hash table's memory use is optimal. Unfortunately, it is
          inefficient to rebuild the hash table over and over if
          you're doing a large number of insertions or removals. To
          solve the problem, the hash table can be <i class= 
          "FIRSTTERM">frozen</i>, meaning that resizing is
          temporarily suppressed. When you're done adding and
          removing items, you simply <i class="FIRSTTERM">thaw</i>
          the table, resulting in a single optimal-size
          calculation. (Be careful though; a frozen table can end
          up with many hash collisions if you add large quantities
          of data. This should be fine as long as you thaw before
          you do any lookups.) The functions are in <a href= 
          "z29.html#FL-HASHFREEZE">Figure 32</a>.
        </p>
        <div class="FIGURE">
          <a name="FL-HASHFREEZE"></a>
          <div class="FUNCSYNOPSIS">
            <a name="FL-HASHFREEZE.SYNOPSIS"></a>
            <table border="0" bgcolor="#E0E0E0" width="100%">
              <tr>
                <td>
<pre class="FUNCSYNOPSISINFO">
#include &lt;glib.h&gt;
</pre>
                </td>
              </tr>
            </table>
            <p>
              <code><code class="FUNCDEF">void <tt class=
              "FUNCTION">
              g_hash_table_freeze</tt></code>(GHashTable* <tt
              class="PARAMETER"><i>hash_table</i></tt>);</code>
            </p>
            <p>
              <code><code class="FUNCDEF">void <tt class=
              "FUNCTION">g_hash_table_thaw</tt></code>(GHashTable*
              <tt class="PARAMETER"><i>hash_table</i></tt>);</code>
            </p>
          </div>
          <p>
            <b>Figure 32. Freezing and thawing <span class= 
            "STRUCTNAME">GHashTable</span></b>
          </p>
        </div>
      </div>
    </div>
    <div class="NAVFOOTER">
      <br>
      <br>
      <table width="100%" border="0" bgcolor="#ffffff" cellpadding= 
      "1" cellspacing="0">
        <tr>
          <td width="25%" bgcolor="#ffffff" align="left">
            <a href="cha-glib.html"><font color="#0000ff" size="2">
            <b>&lt;&lt;&lt; Previous</b></font></a>
          </td>
          <td width="25%" colspan="2" bgcolor="#ffffff" align= 
          "center">
            <font color="#0000ff" size="2"><b><a href="ggad.html">
            <font color="#0000ff" size="2"><b>
            Home</b></font></a></b></font>
          </td>
          <td width="25%" bgcolor="#ffffff" align="right">
            <a href="z35.html"><font color="#0000ff" size="2"><b>
            Next &gt;&gt;&gt;</b></font></a>
          </td>
        </tr>
        <tr>
          <td colspan="2" align="left">
            <font color="#000000" size="2"><b>glib: Portability and
            Utility</b></font>
          </td>
          <td colspan="2" align="right">
            <font color="#000000" size="2"><b>Other
            Features</b></font>
          </td>
        </tr>
      </table>
    </div>
  </body>
</html>