LibreOffice Module vcl (master)  1
list.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 /*[]---------------------------------------------------[]*/
21 /*| |*/
22 /*| list.c - bidirectional list class |*/
23 /*| |*/
24 /*| |*/
25 /*| Author: Alexander Gelfenbain |*/
26 /*[]---------------------------------------------------[]*/
27 
28 #include <assert.h>
29 #include <cstdlib>
30 
31 #include "list.h"
32 
33 /*- private data types */
34 struct lnode {
35  struct lnode *next;
36  struct lnode *prev;
37 
38  void *value;
39 
40 };
41 
42 struct list_ {
44  size_t aCount;
46 };
47 
48 /*- private methods */
49 
50 static lnode *newNode(void *el)
51 {
52  lnode *ptr = static_cast<lnode *>(std::malloc(sizeof(lnode)));
53  assert(ptr != nullptr);
54 
55  ptr->value = el;
56 
57  return ptr;
58 }
59 
60 static lnode *appendPrim(list pThis, void *el)
61 {
62  lnode *ptr = newNode(el);
63  lnode **flink, *blink;
64 
65  if (pThis->tail != nullptr) {
66  flink = &(pThis->tail->next);
67  blink = pThis->tail;
68  } else {
69  flink = &pThis->head;
70  blink = nullptr;
71  pThis->cptr = ptr; /*- list was empty - set current to this element */
72  }
73 
74  *flink = ptr;
75  pThis->tail = ptr;
76 
77  ptr->prev = blink;
78  ptr->next = nullptr;
79 
80  pThis->aCount++;
81  return ptr;
82 }
83 
84 /*- public methods */
85 list listNewEmpty() /*- default ctor */
86 {
87  list pThis = static_cast<list>(std::malloc(sizeof(struct list_)));
88  assert(pThis != nullptr);
89 
90  pThis->aCount = 0;
91  pThis->eDtor = nullptr;
92  pThis->head = pThis->tail = pThis->cptr = nullptr;
93 
94  return pThis;
95 }
96 
97 void listDispose(list pThis) /*- dtor */
98 {
99  assert(pThis != nullptr);
100  listClear(pThis);
101  std::free(pThis);
102 }
103 
105 {
106  assert(pThis != nullptr);
107  pThis->eDtor = f;
108 }
109 
110 /* calling this function on an empty list is a run-time error */
111 void *listCurrent(list pThis)
112 {
113  assert(pThis != nullptr);
114  assert(pThis->cptr != nullptr);
115  return pThis->cptr->value;
116 }
117 
118 int listCount(list pThis)
119 {
120  assert(pThis != nullptr);
121  return pThis->aCount;
122 }
123 
124 int listIsEmpty(list pThis)
125 {
126  assert(pThis != nullptr);
127  return pThis->aCount == 0;
128 }
129 
130 int listNext(list pThis)
131 {
132  return listSkipForward(pThis, 1);
133 }
134 
135 int listSkipForward(list pThis, int n)
136 {
137  int m = 0;
138  assert(pThis != nullptr);
139 
140  if (pThis->cptr == nullptr) return 0;
141 
142  while (n != 0) {
143  if (pThis->cptr->next == nullptr) break;
144  pThis->cptr = pThis->cptr->next;
145  n--;
146  m++;
147  }
148  return m;
149 }
150 
151 int listToFirst(list pThis)
152 {
153  assert(pThis != nullptr);
154 
155  if (pThis->cptr != pThis->head) {
156  pThis->cptr = pThis->head;
157  return 1;
158  }
159  return 0;
160 }
161 
162 int listToLast(list pThis)
163 {
164  assert(pThis != nullptr);
165 
166  if (pThis->cptr != pThis->tail) {
167  pThis->cptr = pThis->tail;
168  return 1;
169  }
170  return 0;
171 }
172 
173 list listAppend(list pThis, void *el)
174 {
175  assert(pThis != nullptr);
176 
177  appendPrim(pThis, el);
178  return pThis;
179 }
180 
182 {
183  lnode *ptr = nullptr;
184  if (pThis->cptr == nullptr) return pThis;
185 
186  if (pThis->cptr->next != nullptr) {
187  ptr = pThis->cptr->next;
188  pThis->cptr->next->prev = pThis->cptr->prev;
189  } else {
190  pThis->tail = pThis->cptr->prev;
191  }
192 
193  if (pThis->cptr->prev != nullptr) {
194  if (ptr == nullptr) ptr = pThis->cptr->prev;
195  pThis->cptr->prev->next = pThis->cptr->next;
196  } else {
197  pThis->head = pThis->cptr->next;
198  }
199 
200  if (pThis->eDtor) pThis->eDtor(pThis->cptr->value); /* call the dtor callback */
201 
202  std::free(pThis->cptr);
203  pThis->aCount--;
204  pThis->cptr = ptr;
205  return pThis;
206 }
207 
209 {
210  lnode *node = pThis->head, *ptr;
211 
212  while (node) {
213  ptr = node->next;
214  if (pThis->eDtor) pThis->eDtor(node->value); /* call the dtor callback */
215  std::free(node);
216  pThis->aCount--;
217  node = ptr;
218  }
219 
220  pThis->head = pThis->tail = pThis->cptr = nullptr;
221  assert(pThis->aCount == 0);
222  return pThis;
223 }
224 
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void listDispose(list pThis)
Definition: list.cxx:97
int listNext(list pThis)
Definition: list.cxx:130
int listCount(list pThis)
Definition: list.cxx:118
static lnode * appendPrim(list pThis, void *el)
Definition: list.cxx:60
list listNewEmpty()
Definition: list.cxx:85
int listToFirst(list pThis)
Definition: list.cxx:151
Definition: list.cxx:42
void(* list_destructor)(void *)
Definition: list.h:41
list listClear(list pThis)
Definition: list.cxx:208
list_destructor eDtor
Definition: list.cxx:45
void * value
Definition: list.cxx:38
int listSkipForward(list pThis, int n)
Definition: list.cxx:135
struct lnode * prev
Definition: list.cxx:36
int listToLast(list pThis)
Definition: list.cxx:162
list listAppend(list pThis, void *el)
Definition: list.cxx:173
int listIsEmpty(list pThis)
Definition: list.cxx:124
Definition: list.cxx:34
size_t aCount
Definition: list.cxx:44
list listRemove(list pThis)
Definition: list.cxx:181
void(* f)(TrueTypeTable *)
Definition: ttcr.cxx:466
void listSetElementDtor(list pThis, list_destructor f)
Definition: list.cxx:104
void * listCurrent(list pThis)
Definition: list.cxx:111
static lnode * newNode(void *el)
Definition: list.cxx:50
struct lnode * next
Definition: list.cxx:35
lnode * head
Definition: list.cxx:43
lnode * tail
Definition: list.cxx:43
lnode * cptr
Definition: list.cxx:43