merge master
[emacs.git] / oldXMenu / XMakeAssoc.c
blob9bbde2cf94d726c91cf5cdc0d5f6d50b9b06c575
1 /* Copyright Massachusetts Institute of Technology 1985 */
3 #include "copyright.h"
6 #include "XMenuInt.h"
7 #include <X11/Xresource.h>
8 #include <errno.h>
10 #ifndef NULL
11 #define NULL 0
12 #endif
15 * XMakeAssoc - Insert data into an XAssocTable keyed on an XId.
16 * Data is inserted into the table only once. Redundant inserts are
17 * meaningless (but cause no problems). The queue in each association
18 * bucket is sorted (lowest XId to highest XId).
20 void
21 XMakeAssoc(register Display *dpy, register XAssocTable *table, register XID x_id, register void *data)
23 int hash;
24 register XAssoc *bucket;
25 register XAssoc *Entry;
26 register XAssoc *new_entry;
28 /* Hash the XId to get the bucket number. */
29 hash = x_id & (table->size - 1);
30 /* Look up the bucket to get the entries in that bucket. */
31 bucket = &table->buckets[hash];
32 /* Get the first entry in the bucket. */
33 Entry = bucket->next;
35 /* If (Entry != bucket), the bucket is empty so make */
36 /* the new entry the first entry in the bucket. */
37 /* if (Entry == bucket), the we have to search the */
38 /* bucket. */
39 if (Entry != bucket) {
40 /* The bucket isn't empty, begin searching. */
41 /* If we leave the for loop then we have either passed */
42 /* where the entry should be or hit the end of the bucket. */
43 /* In either case we should then insert the new entry */
44 /* before the current value of "Entry". */
45 for (; Entry != bucket; Entry = Entry->next) {
46 if (Entry->x_id == x_id) {
47 /* Entry has the same XId... */
48 if (Entry->display == dpy) {
49 /* Entry has the same Display... */
50 /* Therefore there is already an */
51 /* entry with this XId and Display, */
52 /* reset its data value and return. */
53 Entry->data = data;
54 return;
56 /* We found an association with the right */
57 /* id but the wrong display! */
58 continue;
60 /* If the current entry's XId is greater than the */
61 /* XId of the entry to be inserted then we have */
62 /* passed the location where the new XId should */
63 /* be inserted. */
64 if (Entry->x_id > x_id) break;
68 /* If we are here then the new entry should be inserted just */
69 /* before the current value of "Entry". */
70 /* Create a new XAssoc and load it with new provided data. */
71 new_entry = (XAssoc *) malloc(sizeof(XAssoc));
72 new_entry->display = dpy;
73 new_entry->x_id = x_id;
74 new_entry->data = data;
76 /* Insert the new entry. */
77 emacs_insque((struct qelem *)new_entry, (struct qelem *)Entry->prev);