00001 /** 00002 * @file ccn/IndexSorter.h 00003 * 00004 * Part of the CCNx C Library. 00005 * 00006 * Copyright (C) 2011 Palo Alto Research Center, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or modify it 00009 * under the terms of the GNU Lesser General Public License version 2.1 00010 * as published by the Free Software Foundation. 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. You should have received 00015 * a copy of the GNU Lesser General Public License along with this library; 00016 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, 00017 * Fifth Floor, Boston, MA 02110-1301 USA. 00018 */ 00019 00020 #ifndef CCN_IndexSorter 00021 #define CCN_IndexSorter 00022 00023 #include <stdlib.h> 00024 #include <stdint.h> 00025 #include <sys/types.h> 00026 00027 /* 00028 IndexSorter is a simple priority queue that uses indexes and a sorting function 00029 to efficiently sort a sequence of objects. We note that the types of the keys 00030 and values are only known to the sorting function, and storage for the keys and 00031 values is provided externally to the IndexSorter. 00032 00033 As a special case, if base->sorter == NULL, this behaves as a stack. 00034 */ 00035 00036 typedef uintmax_t IndexSorter_Index; 00037 // The indexes need not be consecutive or even comparable, 00038 // but one index value should be reserved to denote the empty condition. 00039 00040 typedef struct IndexSorter_Struct *IndexSorter_Base; 00041 00042 typedef int 00043 IndexSorter_sorter(IndexSorter_Base base, 00044 IndexSorter_Index x, IndexSorter_Index y); 00045 // returns 00046 // < 0 if key(x) sorts before key(y) 00047 // = 0 if the keys are the same, 00048 // > 0 if key(x) sorts after key(y) 00049 00050 struct IndexSorter_Struct { 00051 IndexSorter_Index len; // # of indexes currently valid 00052 IndexSorter_Index lim; // the current storage limit for indexes 00053 IndexSorter_Index empty; // the empty index 00054 IndexSorter_sorter *sorter; // the sorting function 00055 void *client; // client data for the sorting function 00056 IndexSorter_Index *indexes; // the storage for the indexes 00057 }; 00058 00059 IndexSorter_Base 00060 IndexSorter_New(IndexSorter_Index lim, IndexSorter_Index empty); 00061 // create a new IndexSorter 00062 // intention is to have the caller provide client and sorter fields 00063 // after the base has been returned 00064 00065 void 00066 IndexSorter_Add(IndexSorter_Base base, IndexSorter_Index x); 00067 // add a new index 00068 00069 IndexSorter_Index 00070 IndexSorter_Rem(IndexSorter_Base base); 00071 // remove the "best" index (least key) 00072 // returns base->empty if the IndexSorter is empty 00073 00074 IndexSorter_Index 00075 IndexSorter_Best(IndexSorter_Base base); 00076 // returns the "best" index (least key) with no modification 00077 // returns base->empty if the IndexSorter is empty 00078 00079 void 00080 IndexSorter_Reset(IndexSorter_Base base); 00081 // resets the sorter to have no indexes (base->len = 0) 00082 00083 void 00084 IndexSorter_Free(IndexSorter_Base *basePtr); 00085 // frees the storage used for the IndexSorter 00086 00087 00088 #endif