2016-07-20 20:26:57 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CSSNodeList.h"
|
|
|
|
|
|
|
|
struct CSSNodeList {
|
2016-08-02 15:06:55 +00:00
|
|
|
uint32_t capacity;
|
|
|
|
uint32_t count;
|
2016-07-20 20:26:57 +00:00
|
|
|
void **items;
|
|
|
|
};
|
|
|
|
|
2016-08-22 13:58:13 +00:00
|
|
|
CSSNodeListRef CSSNodeListNew(const uint32_t initialCapacity) {
|
|
|
|
const CSSNodeListRef list = malloc(sizeof(struct CSSNodeList));
|
2016-08-02 15:07:11 +00:00
|
|
|
CSS_ASSERT(list != NULL, "Could not allocate memory for list");
|
2016-07-20 20:26:57 +00:00
|
|
|
|
|
|
|
list->capacity = initialCapacity;
|
|
|
|
list->count = 0;
|
2016-08-04 15:20:11 +00:00
|
|
|
list->items = malloc(sizeof(void *) * list->capacity);
|
2016-08-02 15:07:11 +00:00
|
|
|
CSS_ASSERT(list->items != NULL, "Could not allocate memory for items");
|
2016-07-20 20:26:57 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2016-08-22 13:58:13 +00:00
|
|
|
void CSSNodeListFree(const CSSNodeListRef list) {
|
2016-08-15 16:15:02 +00:00
|
|
|
free(list->items);
|
|
|
|
free(list);
|
2016-08-08 11:47:45 +00:00
|
|
|
}
|
2016-07-20 20:26:57 +00:00
|
|
|
|
2016-08-22 13:58:13 +00:00
|
|
|
uint32_t CSSNodeListCount(const CSSNodeListRef list) {
|
2016-08-15 16:15:02 +00:00
|
|
|
return list->count;
|
|
|
|
}
|
2016-07-20 20:26:57 +00:00
|
|
|
|
2016-08-22 13:58:13 +00:00
|
|
|
void CSSNodeListAdd(const CSSNodeListRef list, const CSSNodeRef node) {
|
2016-07-20 20:26:57 +00:00
|
|
|
CSSNodeListInsert(list, node, list->count);
|
|
|
|
}
|
|
|
|
|
2016-08-22 13:58:13 +00:00
|
|
|
void CSSNodeListInsert(const CSSNodeListRef list, const CSSNodeRef node, const uint32_t index) {
|
2016-07-20 20:26:57 +00:00
|
|
|
if (list->count == list->capacity) {
|
|
|
|
list->capacity *= 2;
|
2016-08-04 15:20:11 +00:00
|
|
|
list->items = realloc(list->items, sizeof(void *) * list->capacity);
|
2016-08-02 15:07:11 +00:00
|
|
|
CSS_ASSERT(list->items != NULL, "Could not extend allocation for items");
|
2016-07-20 20:26:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 15:06:55 +00:00
|
|
|
for (uint32_t i = list->count; i > index; i--) {
|
2016-07-20 20:26:57 +00:00
|
|
|
list->items[i] = list->items[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
list->count++;
|
|
|
|
list->items[index] = node;
|
|
|
|
}
|
|
|
|
|
2016-08-22 13:58:13 +00:00
|
|
|
CSSNodeRef CSSNodeListRemove(const CSSNodeListRef list, const uint32_t index) {
|
|
|
|
const CSSNodeRef removed = list->items[index];
|
2016-07-20 20:26:57 +00:00
|
|
|
list->items[index] = NULL;
|
|
|
|
|
2016-08-02 15:06:55 +00:00
|
|
|
for (uint32_t i = index; i < list->count - 1; i++) {
|
2016-07-20 20:26:57 +00:00
|
|
|
list->items[i] = list->items[i + 1];
|
|
|
|
list->items[i + 1] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->count--;
|
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2016-08-22 13:58:13 +00:00
|
|
|
CSSNodeRef CSSNodeListDelete(const CSSNodeListRef list, const CSSNodeRef node) {
|
2016-08-02 15:06:55 +00:00
|
|
|
for (uint32_t i = 0; i < list->count; i++) {
|
2016-07-20 20:26:57 +00:00
|
|
|
if (list->items[i] == node) {
|
|
|
|
return CSSNodeListRemove(list, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-22 13:58:13 +00:00
|
|
|
CSSNodeRef CSSNodeListGet(const CSSNodeListRef list, const uint32_t index) {
|
2016-08-15 16:15:02 +00:00
|
|
|
return list->items[index];
|
|
|
|
}
|