File: depth_multi_queue.h

package info (click to toggle)
freecell-solver 3.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,864 kB
  • ctags: 3,658
  • sloc: ansic: 34,721; perl: 12,320; xml: 5,999; python: 1,149; sh: 965; ruby: 347; cpp: 304; makefile: 151
file content (203 lines) | stat: -rw-r--r-- 5,665 bytes parent folder | download
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
/* Copyright (c) 2013 Shlomi Fish
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
/*
 * depth_multi_queue.h - header file for the depth-based / depth-tracking
 * multi-queue which can handle more than one starting point.
 */

#ifndef FC_SOLVE__DEPTH_MULTI_QUEUE_H
#define FC_SOLVE__DEPTH_MULTI_QUEUE_H

#ifdef __cplusplus
extern "C" {
#endif

#include <string.h>
#include <limits.h>
#include <stdio.h>
#include <unistd.h>

#include "config.h"
#include "state.h"
#include "inline.h"
#include "bool.h"
#include "alloc_wrap.h"

#include "offloading_queue.h"

typedef struct
{
    int num_items_per_page;
    const char * offload_dir_path;
    long num_inserted, num_items_in_queue, num_extracted;
    long min_depth, max_depth, max_depth_margin;
    /*
     * page_to_write_to, page_for_backup and page_to_read_from always
     * point to the two "pages" below, but they can be swapped and
     * page_for_backup may be NULL.
     */
     fcs_offloading_queue_t * queues_by_depth;
     long next_queue_id;
} fcs_depth_multi_queue_t;

#define DEPTH_Q_GROW_BY 32

static GCC_INLINE void fcs_depth_multi_queue__new_queue(
    fcs_depth_multi_queue_t * queue,
    fcs_offloading_queue_t * q
)
{
    fcs_offloading_queue__init(
        q,
        queue->num_items_per_page,
        queue->offload_dir_path,
        (queue->next_queue_id++)
    );
}

static GCC_INLINE void fcs_depth_multi_queue__insert(
    fcs_depth_multi_queue_t * const queue,
    const int depth,
    const fcs_offloading_queue_item_t * const item
)
{
    while (depth > queue->max_depth)
    {
        queue->max_depth++;
        if (queue->max_depth == queue->max_depth_margin)
        {
            queue->max_depth_margin += DEPTH_Q_GROW_BY;
            queue->queues_by_depth =
                SREALLOC(
                    queue->queues_by_depth,
                    queue->max_depth_margin
                );
        }
        fcs_depth_multi_queue__new_queue(
            queue,
            &(queue->queues_by_depth[queue->max_depth - queue->min_depth])
        );
    }

    fcs_offloading_queue__insert(
        &(queue->queues_by_depth[depth - queue->min_depth]),
        item
    );

    queue->num_inserted++;
    queue->num_items_in_queue++;
}

static GCC_INLINE void fcs_depth_multi_queue__init(
    fcs_depth_multi_queue_t * const queue,
    const int num_items_per_page,
    const char * offload_dir_path,
    const int first_depth,
    const fcs_offloading_queue_item_t * const first_item
    )
{
    queue->num_items_per_page = num_items_per_page;
    queue->offload_dir_path = offload_dir_path;
    queue->num_inserted = queue->num_items_in_queue = queue->num_extracted = 0;
    queue->next_queue_id = 0;
    queue->min_depth = first_depth;
    queue->max_depth_margin = first_depth + DEPTH_Q_GROW_BY;

    queue->queues_by_depth = SMALLOC(
        queue->queues_by_depth,
        queue->max_depth_margin - queue->min_depth + 1
    );
    fcs_depth_multi_queue__new_queue(
        queue,
        &(queue->queues_by_depth[0])
    );
    queue->max_depth = first_depth;

    fcs_depth_multi_queue__insert(
        queue,
        first_depth,
        first_item
    );
}

static GCC_INLINE void fcs_depth_multi_queue__destroy(
    fcs_depth_multi_queue_t * queue
    )
{
    int limit = queue->max_depth - queue->min_depth;
    if (queue->queues_by_depth == NULL)
    {
        return;
    }
    for (int i=0 ; i <= limit ; i++)
    {
        fcs_offloading_queue__destroy(&(queue->queues_by_depth[i]));
    }
    free ( queue->queues_by_depth );
    queue->queues_by_depth = NULL;
}


static GCC_INLINE fcs_bool_t fcs_depth_multi_queue__extract(
    fcs_depth_multi_queue_t * queue,
    int * return_depth,
    fcs_offloading_queue_item_t * return_item
)
{
    if (queue->num_items_in_queue == 0)
    {
        return FALSE;
    }

    while (queue->queues_by_depth[0].num_items_in_queue == 0)
    {
        typeof(queue->queues_by_depth[0]) save_queue = queue->queues_by_depth[0];
        memmove(
            queue->queues_by_depth,
            queue->queues_by_depth+1,
            sizeof(queue->queues_by_depth[0]) * (queue->max_depth - queue->min_depth)
        );
        queue->queues_by_depth[queue->max_depth - queue->min_depth] = save_queue;
        queue->max_depth++;
        queue->min_depth++;
        queue->max_depth_margin++;
    }

    *return_depth = queue->min_depth;
    fcs_offloading_queue__extract(
        &(queue->queues_by_depth[0]),
        return_item
    );

    queue->num_items_in_queue--;
    queue->num_extracted++;

    return TRUE;
}

#ifdef __cplusplus
}
#endif

#endif  /* FC_SOLVE__DEPTH_MULTI_QUEUE_H */