dllist.c 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*-------------------------------------------------------------------------
 *
 * dllist.c--
 *    this is a simple doubly linked list implementation
 *    replaces the old simplelists stuff
 *    the elements of the lists are void*
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
12
 *    $Header: /cvsroot/pgsql/src/backend/lib/dllist.c,v 1.3 1996/11/06 08:27:11 scrappy Exp $
13 14 15 16
 *
 *-------------------------------------------------------------------------
 */

17
#include <postgres.h>
M
Marc G. Fournier 已提交
18

19
#include <lib/dllist.h>
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 204 205

Dllist*
DLNewList()
{
  Dllist* l;

  l = malloc(sizeof(Dllist));
  l->dll_head = 0;
  l->dll_tail = 0;

  return l;
}

 /* free up a list and all the nodes in it*/
void
DLFreeList(Dllist* l)
{
  Dlelem* curr;

  while ( (curr = DLRemHead(l)) != 0)
      free(curr);

  free(l);
}

Dlelem* 
DLNewElem(void* val)
{
 Dlelem* e;
 e = malloc(sizeof(Dlelem));
 e->dle_next = 0;
 e->dle_prev = 0;
 e->dle_val = val;
 e->dle_list = 0;
 return e;
}

void
DLFreeElem(Dlelem* e)
{
  free(e);
}

Dlelem*
DLGetHead(Dllist* l)
{
  return (l ? l->dll_head : 0);
}

/* get the value stored in the first element */
void*
DLGetHeadVal(Dllist* l)
{
  Dlelem* e = DLGetHead(l);
  
  return (e ? e->dle_val : 0);
}

Dlelem* 
DLGetTail(Dllist* l)
{
  return (l ? l->dll_tail : 0);
}

/* get the value stored in the first element */
void*
DLGetTailVal(Dllist* l)
{
  Dlelem* e = DLGetTail(l);
  
  return (e ? e->dle_val : 0);
}


Dlelem* 
DLGetPred(Dlelem* e) /* get predecessor */
{
  return (e ? e->dle_prev : 0);
}

Dlelem* 
DLGetSucc(Dlelem* e) /* get successor */
{
    return (e ? e->dle_next : 0);
}

void
DLRemove(Dlelem* e)
{
  Dllist* l;

  if (e->dle_prev)
    e->dle_prev->dle_next = e->dle_next;
  if (e->dle_next)
    e->dle_next->dle_prev = e->dle_prev;

  /* check to see if we're removing the head or tail */
  l = e->dle_list;
  if (e == l->dll_head)
    DLRemHead(l);
  if (e == l->dll_tail)
    DLRemTail(l);

}

void    
DLAddHead(Dllist* l, Dlelem* e)
{
  e->dle_list = l;

  if (l->dll_head)  {
    l->dll_head->dle_prev = e;
    e->dle_next = l->dll_head;
  }
  e->dle_prev = 0;
  l->dll_head = e;

  if (l->dll_tail == 0) /* if this is first element added */
    l->dll_tail = l->dll_head;
}

void
DLAddTail(Dllist* l, Dlelem* e)
{
  e->dle_list = l;

  if (l->dll_tail)   {
    l->dll_tail->dle_next = e;
    e->dle_prev = l->dll_tail;
  }
  e->dle_next = 0;
  l->dll_tail = e;

  if (l->dll_head == 0) /* if this is first element added */
    l->dll_head = l->dll_tail;
}

Dlelem* 
DLRemHead(Dllist* l) 
{
 /* remove and return the head */
  Dlelem* result;

  if (l->dll_head == 0)
    return 0;

  result = l->dll_head;
  if (l->dll_head->dle_next) {
    l->dll_head->dle_next->dle_prev = 0;
  }

  l->dll_head = l->dll_head->dle_next;

  result->dle_next = 0;
  result->dle_list = 0;
    
  if (result == l->dll_tail) /* if the head is also the tail */
    l->dll_tail = 0;

  return result;
}

Dlelem* 
DLRemTail(Dllist* l)
{
 /* remove and return the tail */
  Dlelem* result;

  if (l->dll_tail == 0 )
    return 0;

  result = l->dll_tail;
  if (l->dll_tail->dle_prev) {
    l->dll_tail->dle_prev->dle_next = 0;
  }
    l->dll_tail = l->dll_tail->dle_prev;

  result->dle_prev = 0;
  result->dle_list = 0;

  if (result == l->dll_head) /* if the tail is also the head */
    l->dll_head = 0;

  return result;
}