summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/db.c104
-rw-r--r--src/common/db.h14
2 files changed, 118 insertions, 0 deletions
diff --git a/src/common/db.c b/src/common/db.c
index 65abecea7..ebb5b9a30 100644
--- a/src/common/db.c
+++ b/src/common/db.c
@@ -2342,3 +2342,107 @@ void db_final(void)
#endif /* DB_ENABLE_STATS */
}
+// Link DB System - jAthena
+void linkdb_insert( struct linkdb_node** head, void *key, void* data) {
+ struct linkdb_node *node;
+ if( head == NULL ) return ;
+ node = aMalloc( sizeof(struct linkdb_node) );
+ if( *head == NULL ) {
+ // first node
+ *head = node;
+ node->prev = NULL;
+ node->next = NULL;
+ } else {
+ // link nodes
+ node->next = *head;
+ node->prev = (*head)->prev;
+ (*head)->prev = node;
+ (*head) = node;
+ }
+ node->key = key;
+ node->data = data;
+}
+
+void* linkdb_search( struct linkdb_node** head, void *key) {
+ int n = 0;
+ struct linkdb_node *node;
+ if( head == NULL ) return NULL;
+ node = *head;
+ while( node ) {
+ if( node->key == key ) {
+ if( node->prev && n > 5 ) {
+ // 処理効率改善の為にheadに移動させる
+ if(node->prev) node->prev->next = node->next;
+ if(node->next) node->next->prev = node->prev;
+ node->next = *head;
+ node->prev = (*head)->prev;
+ (*head)->prev = node;
+ (*head) = node;
+ }
+ return node->data;
+ }
+ node = node->next;
+ n++;
+ }
+ return NULL;
+}
+
+void* linkdb_erase( struct linkdb_node** head, void *key) {
+ struct linkdb_node *node;
+ if( head == NULL ) return NULL;
+ node = *head;
+ while( node ) {
+ if( node->key == key ) {
+ void *data = node->data;
+ if( node->prev == NULL )
+ *head = node->next;
+ else
+ node->prev->next = node->next;
+ if( node->next )
+ node->next->prev = node->prev;
+ aFree( node );
+ return data;
+ }
+ node = node->next;
+ }
+ return NULL;
+}
+
+void linkdb_replace( struct linkdb_node** head, void *key, void *data ) {
+ int n = 0;
+ struct linkdb_node *node;
+ if( head == NULL ) return ;
+ node = *head;
+ while( node ) {
+ if( node->key == key ) {
+ if( node->prev && n > 5 ) {
+ // 処理効率改善の為にheadに移動させる
+ if(node->prev) node->prev->next = node->next;
+ if(node->next) node->next->prev = node->prev;
+ node->next = *head;
+ node->prev = (*head)->prev;
+ (*head)->prev = node;
+ (*head) = node;
+ }
+ node->data = data;
+ return ;
+ }
+ node = node->next;
+ n++;
+ }
+ // 見つからないので挿入
+ linkdb_insert( head, key, data );
+}
+
+void linkdb_final( struct linkdb_node** head ) {
+ struct linkdb_node *node, *node2;
+ if( head == NULL ) return ;
+ node = *head;
+ while( node ) {
+ node2 = node->next;
+ aFree( node );
+ node = node2;
+ }
+ *head = NULL;
+}
+
diff --git a/src/common/db.h b/src/common/db.h
index dcc583bfa..0b6fc08c5 100644
--- a/src/common/db.h
+++ b/src/common/db.h
@@ -731,4 +731,18 @@ void db_init(void);
*/
void db_final(void);
+// Link DB System - From jAthena
+struct linkdb_node {
+ struct linkdb_node *next;
+ struct linkdb_node *prev;
+ void *key;
+ void *data;
+};
+
+void linkdb_insert ( struct linkdb_node** head, void *key, void* data); // 重複を考慮しない
+void linkdb_replace( struct linkdb_node** head, void *key, void* data); // 重複を考慮する
+void* linkdb_search ( struct linkdb_node** head, void *key);
+void* linkdb_erase ( struct linkdb_node** head, void *key);
+void linkdb_final ( struct linkdb_node** head );
+
#endif