home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / mksort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.8 KB  |  167 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. #include "mkutils.h"
  19. #include "mksort.h"
  20. #include "xp_qsort.h"
  21.  
  22. #define CHUNK_SIZE 400
  23.  
  24. #ifdef PROFILE
  25. #pragma profile on
  26. #endif
  27.  
  28.  
  29. MODULE_PRIVATE SortStruct *
  30. NET_SortInit (void)
  31. {
  32.  
  33.     SortStruct * sort_struct = XP_NEW(SortStruct);
  34.  
  35.     if(!sort_struct)
  36.     return(0);
  37.  
  38.     sort_struct->list = (void **) XP_ALLOC((sizeof(void *) * CHUNK_SIZE));
  39.  
  40.     if(!sort_struct->list)
  41.     return(0);
  42.  
  43.     sort_struct->cur_size = CHUNK_SIZE;
  44.     sort_struct->num_entries = 0;
  45.  
  46.     return(sort_struct);
  47. }
  48.  
  49. MODULE_PRIVATE Bool
  50. NET_SortAdd (SortStruct * sort_struct, void * add_object)
  51. {
  52.     if(!sort_struct)
  53.       {
  54.         TRACEMSG(("Trying to add to NULL sort_struct"));
  55.         return(0);
  56.       }
  57.  
  58.     if(sort_struct->cur_size == sort_struct->num_entries)
  59.       {  /* whoops the list was too small, expand it */
  60.  
  61.         sort_struct->cur_size += CHUNK_SIZE;
  62.         sort_struct->list = (void **) XP_REALLOC(sort_struct->list, 
  63.                                 (sizeof(void *) * sort_struct->cur_size));
  64.  
  65.         if(!sort_struct->list)
  66.             return(0);  /* very bad! */
  67.       }
  68.  
  69.     sort_struct->list[sort_struct->num_entries++] = add_object;
  70.  
  71.     return(1);
  72.  
  73. }
  74.  
  75. MODULE_PRIVATE Bool
  76. NET_SortInsert(SortStruct * sort_struct, void * insert_before, void * new_object)
  77. {
  78.     int i;
  79.  
  80.     if(sort_struct->cur_size == sort_struct->num_entries)
  81.       {  /* whoops the list was too small, expand it */
  82.  
  83.         sort_struct->cur_size += CHUNK_SIZE;
  84.         sort_struct->list = (void **) XP_REALLOC(sort_struct->list,
  85.                                 (sizeof(void *) * sort_struct->cur_size));
  86.  
  87.         if(!sort_struct->list)
  88.             return(0);  /* very bad! */
  89.       }
  90.  
  91.     /* find the insert_before object in the list
  92.      */
  93.     for(i=0; i < sort_struct->num_entries; i++)
  94.       {
  95.         if(sort_struct->list[i] == insert_before)
  96.             break;
  97.       }
  98.  
  99.     if(sort_struct->list[i] == insert_before)
  100.       {
  101.         void * tmp_ptr1;
  102.         void * tmp_ptr2;
  103.  
  104.         tmp_ptr2 = new_object;
  105.  
  106.         for(; i < sort_struct->num_entries; i++)
  107.           {
  108.             tmp_ptr1 = sort_struct->list[i];
  109.             sort_struct->list[i] = tmp_ptr2;
  110.             tmp_ptr2 = tmp_ptr1;
  111.           }
  112.  
  113.         sort_struct->list[i] = tmp_ptr2;
  114.         sort_struct->num_entries++;
  115.       }
  116.     else
  117.       {
  118.         return(0);
  119.       }
  120.  
  121.     return(1);
  122. }
  123.  
  124. MODULE_PRIVATE void
  125. NET_DoSort(SortStruct * sort_struct, int (*compar) (const void *, const void *))
  126. {
  127.     XP_QSORT(sort_struct->list, sort_struct->num_entries, sizeof(void *), compar);
  128.  
  129. /* unloads backwards :(
  130.  */
  131. MODULE_PRIVATE void *
  132. NET_SortUnloadNext(SortStruct * sort_struct)
  133. {
  134.      if(sort_struct->num_entries < 1)
  135.     return(0);
  136.  
  137.      return(sort_struct->list[--sort_struct->num_entries]);
  138. }
  139.  
  140. MODULE_PRIVATE void *
  141. NET_SortRetrieveNumber(SortStruct * sort_struct, int number)
  142. {
  143.      if(number >= sort_struct->num_entries)
  144.     return(0);
  145.  
  146.      return(sort_struct->list[number]);
  147. }
  148.  
  149. MODULE_PRIVATE int
  150. NET_SortCount(SortStruct * sort_struct)
  151. {
  152.     return(sort_struct->num_entries);
  153. }
  154.  
  155. MODULE_PRIVATE void 
  156. NET_SortFree(SortStruct * sort_struct)
  157. {
  158.     XP_FREE(sort_struct->list);
  159.     XP_FREE(sort_struct);
  160. }
  161.  
  162. #ifdef PROFILE
  163. #pragma profile off
  164. #endif
  165.  
  166.