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