1 /* Copyright Massachusetts Institute of Technology 1985 */
8 #include <X11/Xresource.h>
21 void emacs_insque (struct qelem
*elem
, struct qelem
*prev
);
24 * XMakeAssoc - Insert data into an XAssocTable keyed on an XId.
25 * Data is inserted into the table only once. Redundant inserts are
26 * meaningless (but cause no problems). The queue in each association
27 * bucket is sorted (lowest XId to highest XId).
30 XMakeAssoc(register Display
*dpy
, register XAssocTable
*table
, register XID x_id
, register caddr_t data
)
33 register XAssoc
*bucket
;
34 register XAssoc
*Entry
;
35 register XAssoc
*new_entry
;
37 /* Hash the XId to get the bucket number. */
38 hash
= x_id
& (table
->size
- 1);
39 /* Look up the bucket to get the entries in that bucket. */
40 bucket
= &table
->buckets
[hash
];
41 /* Get the first entry in the bucket. */
44 /* If (Entry != bucket), the bucket is empty so make */
45 /* the new entry the first entry in the bucket. */
46 /* if (Entry == bucket), the we have to search the */
48 if (Entry
!= bucket
) {
49 /* The bucket isn't empty, begin searching. */
50 /* If we leave the for loop then we have either passed */
51 /* where the entry should be or hit the end of the bucket. */
52 /* In either case we should then insert the new entry */
53 /* before the current value of "Entry". */
54 for (; Entry
!= bucket
; Entry
= Entry
->next
) {
55 if (Entry
->x_id
== x_id
) {
56 /* Entry has the same XId... */
57 if (Entry
->display
== dpy
) {
58 /* Entry has the same Display... */
59 /* Therefore there is already an */
60 /* entry with this XId and Display, */
61 /* reset its data value and return. */
65 /* We found an association with the right */
66 /* id but the wrong display! */
69 /* If the current entry's XId is greater than the */
70 /* XId of the entry to be inserted then we have */
71 /* passed the location where the new XId should */
73 if (Entry
->x_id
> x_id
) break;
77 /* If we are here then the new entry should be inserted just */
78 /* before the current value of "Entry". */
79 /* Create a new XAssoc and load it with new provided data. */
80 new_entry
= (XAssoc
*) malloc(sizeof(XAssoc
));
81 new_entry
->display
= dpy
;
82 new_entry
->x_id
= x_id
;
83 new_entry
->data
= data
;
85 /* Insert the new entry. */
86 emacs_insque((struct qelem
*)new_entry
, (struct qelem
*)Entry
->prev
);